Results 1 to 11 of 11
 12192010, 07:38 PM #1Member
 Join Date
 Dec 2010
 Posts
 20
 Rep Power
 0
Program  Print row of 5 even numbers, 5 rows, last num 50
Hi guys, I'm new to Java and have an AP comp sci class. I have an assignment to use "for" loops to complete a goal.
We're just on the basics right now so please try to show me the simplest way of how I'm supposed to put this program together.
Basically, it needs to print out 5 rows of even numbers. The result has to be like this:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
Edit: It needs to use for loops and they can be nested.
I know it's probably an easy logic to think of, but my brain is functioning properly and I've tried a 100 different combinations of for loops that didn't' work. And that has made me really frustrated that I can't figure out this simple looking problem.
I would really appreciate if you guys could help me solve this.Last edited by An Alien; 12202010 at 01:37 PM. Reason: Add something

We're not supposed to do your homework for you here, but we can help you by answering questions for you and help you debug. Let's see what you've tried first, as this way we can see what you know, what you don't know, what needs clarification, etc.
Best of luck, and welcome to the forum!
 12192010, 07:45 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,821
 Blog Entries
 7
 Rep Power
 21
Think of a little grid of size 5x5 where x goes from left to right 0,1,2,3 and 4 and y is running down over the same numbers 0,1,2,3 and 4. The leftmost number in each row is 10*y+2 (check that) and each number to the right increments the previous number with 2 so each number in each row is 10*y+2+2*x (also check that). Creating two nested loops where both x and y iterate over the numbers 0,1,2,3 and 4 isn't rocket science.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 12192010, 08:36 PM #4Member
 Join Date
 Dec 2010
 Posts
 20
 Rep Power
 0
I just tried a million other combinations that didn't work. So, I'm doing it step by step now.
So far, I've got this:
for (int x = 2; x <= 50; x+=2){
System.out.print(x + " ");
}
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
But it's a single line D:

The solution to that is to do as Jos suggested: don't use one for loop but two for loops, one nested inside of the other. Your System.out.println can go after the inner loop.
 12192010, 08:53 PM #6Member
 Join Date
 Dec 2010
 Posts
 20
 Rep Power
 0
I'm sorry, I just don't understand the way Jos worded it. I feel so stupid right now.
Another failed combination:
for (int x = 1; x <= 5; x++){
for (int y = 1; y <=5; y++){
System.out.print(x+y + " ");
}
System.out.println(" ");
}
 12202010, 12:48 AM #7Member
 Join Date
 Dec 2010
 Posts
 57
 Rep Power
 0
Java Code:for (int x = 1; x <= 5; x++) { for (int y = 1; y <=5; y++) { System.out.print(x+y + " "); } System.out.println(" "); }
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
Now, you have the right idea with this code, but your error is in your x and y variables. You are declaring your x and y variables inside the two for loops. Now, if you think about the logic of your code, it is going through the for loop with the x variable 5 times, and each time it loops it is also going through the for loop with the y variable.
One of the issues is that you declare the y int variable each time the second for loops starts up, so y is set back to 1 every time the first for loop (with the x variable) completes a loop and gets back to going through the loop again.
You can use your y variable to help with the outputting, but try considering using your x to calculate one part of the number you want to output, y to calculate another part, and then adding the two values together to get the number you want.Last edited by Fortu; 12202010 at 12:52 AM.
 12202010, 03:34 AM #8Member
 Join Date
 Dec 2010
 Posts
 20
 Rep Power
 0
 12202010, 08:49 AM #9
Another way to crack the problem is to use 1 loop. Then use the modular operator to check for factors of 5. This would make the maths you are computing more readable. Something like
Java Code:int y; for (int x = 1; x <= 25; x++) { y = x * 2; System.out.print(y+" "); if (x % 5 == 0) System.out.println(""); }
Good luck on your way to the top of the class soldier, at least you made an effort.
 12202010, 09:06 AM #10
Another way to crack the problem is to use 1 loop. Then use the modular operator to check for factors of 5. This would make the maths you are computing more readable. Something like
Java Code:int y; for (int x = 1; x <= 25; x++) { y = x * 2; System.out.print(y+" "); if (x % 5 == 0) System.out.println(""); }
An alternative would be to do away with the y variable such as
Java Code:for (int x = 1; x <= 25; x++) { System.out.print( x*2 + " "); if (x % 5 == 0) System.out.println(""); }
 12202010, 01:47 PM #11Member
 Join Date
 Dec 2010
 Posts
 20
 Rep Power
 0
Similar Threads

using backtracking to print all possible orders of numbers
By yotamoo in forum New To JavaReplies: 6Last Post: 12082010, 01:39 AM 
print random numbers without repetition
By princess.blue in forum New To JavaReplies: 3Last Post: 02042010, 10:37 AM 
how to multiply numbers in rows and print it next to it
By racewithferrari in forum New To JavaReplies: 1Last Post: 01162010, 07:24 PM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01152009, 08:22 AM
Bookmarks