# Thread: How to Print every tenth integer in my output

1. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## How to Print every tenth integer in my output

I have created a program that finds all five-digit palindromes (A palindrome is a number which has the same digit from either end such as 12921). Right now I have all of the palindromes printing to the output. Since the output is so large, I want to only print every tenth palindrome but cannot figure out how to do it.
Here's what I have.

Java Code:
```public class ProgrammingProblemOne {

public static void main(String[] args){

for(int i = 10000; i <= 100000; i++){

//create variable first digit in order to isolate the first digit
int firstDigit = i / 10000;

//Create variables a and secondDigit in order to isolate the second digit
int a = i / 1000;
int secondDigit = a % 10;

//Create variables b and thirdDigit in order to isolate the third digit
int b = i / 100;
int thirdDigit = b % 10;

//Create variables c and fourthDigit in order to isolate the fourth digit
int c = i / 10;
int fourthDigit = c % 10;

////Create variable fifthDigit in order to isolate the fifth digit
int fifthDigit = i % 10;

//check to see if number is a palindrome
if(firstDigit == fifthDigit && secondDigit == fourthDigit)
System.out.println("the five digit palidrome numbers are: " + i);
}
}
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Use the % operator on the index i.

 Whoops! Maintain a second counter that you increment every time you get a palendrome. And use % with that counter to determine whether you print or not.

3. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by pbrockway2
Use the % operator on the index i.

 Whoops! Maintain a second counter that you increment every time you get a palendrome. And use % with that counter to determine whether you print or not.
I had a feeling that % would be involved somehow. I tried something like this but I didn't know what I was doing. Here's what I tried (don't laugh too hard)

Java Code:
```if(firstDigit == fifthDigit && secondDigit == fourthDigit)
//count the palidrones
palidrone++;
int output = i % 10;

System.out.println("the five digit palidrome numbers are: " + output);```
This runs infinitely and only prints one digit. I also tried int output = palidrone % 10; but that didn't work either.

I'll try adding a palindrome variable and counter to my for loop and see if that works.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Count the palindromes as you are doing, but use a if statement. Something like

Java Code:
```if(++palindrome % 10 == 0) {
System.out.println(i);
}```
The condition in the if statement both increments the palindrome counter and checks it.

5. Member
Join Date
Apr 2011
Location
Posts
30
Rep Power
0
Have you thought of adding all your palindromes to an arrayList as they are running, and after you've collected them, have something like this :)

Java Code:
```for (int z = 0; z <myList.size(); z+=10){
System.out.println(myList.get(0));
}```
You can read on arrayLists here:
ArrayList (Java 2 Platform SE v1.4.2)

Hint, your arraylist will be ArrayList<Integer> type :D

6. Member
Join Date
Apr 2011
Location
Posts
30
Rep Power
0
Almost forgot! If you like palindromes so much, you might want to read on this very useful datastructure, which will allow you to check an arbitrary number for palindromes very easily :D
Stack (Java 2 Platform SE 5.0)

7. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by pbrockway2
Count the palindromes as you are doing, but use a if statement. Something like

Java Code:
```if(++palindrome % 10 == 0) {
System.out.println(i);
}```
The condition in the if statement both increments the palindrome counter and checks it.
Cool, the first time I've been able to use a nested if statement. Something I noticed when using if statements inside of loops is that you cannot use the braces you would normally use with if statements.

Thank you very much for your help.

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
You're welcome.

With respect to braces: it is a good idea to always use braces. Every time. Even when the block of code has only a single line. Otherwise you *will* get tripped up once the statements nest and, more so, when there are "else" bits involved.

9. Senior Member
Join Date
Sep 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
Originally Posted by pbrockway2
With respect to braces: it is a good idea to always use braces.
Nah, 6-12 months is enough for most people. Then you'll have beautiful teeth.

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
I meant to ask: is there some reason why you are looking at every int over the range and making the computer "pick them apart"?

An alternative would be to just generate the numbers with palendromic decimal representation. They all look like xyzyx where x, y, z vary over a suitable range. You could nested for loops to do this. If my counting isn't awry there will be 9*9*9 such numbers.

 it was. 9*10*10. Or something.
Last edited by pbrockway2; 04-16-2011 at 03:55 AM.

11. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by pbrockway2
You're welcome.

With respect to braces: it is a good idea to always use braces. Every time. Even when the block of code has only a single line. Otherwise you *will* get tripped up once the statements nest and, more so, when there are "else" bits involved.
That's what I thought but when I tried to use the braces that normally go along with the if statement within the for loop I get all kinds of compile problems. Once I get rid of the if statement braces and only keep the for loop braces the program works.

I retried the braces and I was able to get the program to work. I must have screwed them up somehow before
Last edited by jim01; 04-16-2011 at 04:01 AM. Reason: Whoops!

12. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by Maximus-EVG
Have you thought of adding all your palindromes to an arrayList as they are running, and after you've collected them, have something like this :)

Java Code:
```for (int z = 0; z <myList.size(); z+=10){
System.out.println(myList.get(0));
}```
You can read on arrayLists here:
ArrayList (Java 2 Platform SE v1.4.2)

Hint, your arraylist will be ArrayList<Integer> type :D
I've been hearing a lot about arrays lately but we don't get to that chapter until Monday. They sounds like they will make life much easier. Right now it's just loops though.

13. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by ojn
Nah, 6-12 months is enough for most people. Then you'll have beautiful teeth.
LOL! Nice.

14. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by pbrockway2
I meant to ask: is there some reason why you are looking at every int over the range and making the computer "pick them apart"?

An alternative would be to just generate the numbers with palendromic decimal representation. They all look like xyzyx where x, y, z vary over a suitable range. You could nested for loops to do this. If my counting isn't awry there will be 9*9*9 such numbers.

 it was. 9*10*10. Or something.
Last week we did branching and one of the programs was to write a program that inputs a 5-digit integer from the keyboard and prints whether or not the
input number is a palindrome or not. It seemed like the easiest way to write this weeks program was to tweak last weeks.

I didn't realize that nested for loops would do the same job. I guess I need to go back and see how that works.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•