Results 1 to 14 of 14
- 04-16-2011, 01:50 AM #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); } } }
- 04-16-2011, 01:52 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Use the % operator on the index i.
[Edit] 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 #3
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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)
This runs infinitely and only prints one digit. I also tried int output = palidrone % 10; but that didn't work either.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);
I'll try adding a palindrome variable and counter to my for loop and see if that works.
- 04-16-2011, 02:16 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Count the palindromes as you are doing, but use a if statement. Something like
The condition in the if statement both increments the palindrome counter and checks it.Java Code:if(++palindrome % 10 == 0) { System.out.println(i); }
- 04-16-2011, 02:23 AM #5
Member
- Join Date
- Apr 2011
- Location
- Canada!
- 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 :)
You can read on arrayLists here:Java Code:for (int z = 0; z <myList.size(); z+=10){ System.out.println(myList.get(0)); }
ArrayList (Java 2 Platform SE v1.4.2)
Hint, your arraylist will be ArrayList<Integer> type :D
- 04-16-2011, 02:25 AM #6
Member
- Join Date
- Apr 2011
- Location
- Canada!
- 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)
- 04-16-2011, 02:35 AM #7
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 04-16-2011, 02:38 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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 #9
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
- 04-16-2011, 02:49 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
[Edit] it was. 9*10*10. Or something.Last edited by pbrockway2; 04-16-2011 at 02:55 AM.
- 04-16-2011, 02:52 AM #11
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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 beforeLast edited by jim01; 04-16-2011 at 03:01 AM. Reason: Whoops!
- 04-16-2011, 02:54 AM #12
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 04-16-2011, 02:55 AM #13
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
- 04-16-2011, 03:00 AM #14
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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.
Similar Threads
-
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
Print Variable From One Class to Output on another.
By Lyricid in forum New To JavaReplies: 8Last Post: 02-27-2010, 01:56 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
how to print output on same line in 'while loop'?
By acidblue in forum New To JavaReplies: 5Last Post: 12-13-2007, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks