# How to Print every tenth integer in my output

• 04-16-2011, 01:50 AM
jim01
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.

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);             }     } }```
• 04-16-2011, 01:52 AM
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.
• 04-16-2011, 02:11 AM
jim01
Quote:

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)

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.
• 04-16-2011, 02:16 AM
pbrockway2
Count the palindromes as you are doing, but use a if statement. Something like

Code:

```if(++palindrome % 10 == 0) {     System.out.println(i); }```
The condition in the if statement both increments the palindrome counter and checks it.
• 04-16-2011, 02:23 AM
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 :)

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
• 04-16-2011, 02:25 AM
Maximus-EVG
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)
• 04-16-2011, 02:35 AM
jim01
Quote:

Originally Posted by pbrockway2
Count the palindromes as you are doing, but use a if statement. Something like

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.
• 04-16-2011, 02:38 AM
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.
• 04-16-2011, 02:47 AM
ojn
Quote:

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.
• 04-16-2011, 02:49 AM
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.
• 04-16-2011, 02:52 AM
jim01
Quote:

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
• 04-16-2011, 02:54 AM
jim01
Quote:

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 :)

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.
• 04-16-2011, 02:55 AM
jim01
Quote:

Originally Posted by ojn
Nah, 6-12 months is enough for most people. Then you'll have beautiful teeth.

LOL! Nice.
• 04-16-2011, 03:00 AM
jim01
Quote:

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.