# Thread: Program - Print row of 5 even numbers, 5 rows, last num 50

1. Member
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; 12-20-2010 at 01:37 PM. Reason: Add something

2. Originally Posted by An Alien
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.
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!

3. Originally Posted by An Alien
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.
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,

Jos

4. Member
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 + " ");

}
And this prints out:
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:

5. 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.

6. Member
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(" ");
}

7. Member
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(" ");
}```
Alright, let's see.. As far as I see, I don't know why this wouldn't work-Ahh wait. You should be getting the output:

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; 12-20-2010 at 12:52 AM.

8. Member
Join Date
Dec 2010
Posts
20
Rep Power
0
Originally Posted by Fortu
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.
I know I have to use the x and y to calculate. I just can't think of how I should calculate those numbers.

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("");

}```
Note the print as well as the println method.
Good luck on your way to the top of the class soldier, at least you made an effort.

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("");

}```
Note the print as well as the println method.
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("");

}```

11. Member
Join Date
Dec 2010
Posts
20
Rep Power
0
Wow, it works. I've never used the remainder operator before so I never thought of doing it that way. Thanks

#### Posting Permissions

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