hey there everyone, am a new guy in Java programming, i want you guys to help me on how to write a program that can print out [B]even numbers[/B] below 61?

and

I want also to write a Java program that prints my name(first and second name on different lines.) thank you.

Java Newbie or not no one will or should help you on this forum unless you show that you have at least attempted to complete the program yourself.
There are plenty of tutorials out there that demonstrate how to do both those things.

well Katana24 thanx for that stern caution. well i came up with the following program to print out even no. below 61 but am getting some problems that is why i need your help.

public Class even_no{
public static void main(String[]args)
{
int x=2;
for(x=1;x<61;x+=2)
{
System.out.println(x);
}
}
}

Edited 5 Years Ago by Bennychrystal: n/a

I meant no disrespect by it just alittle advice. This may help:

- Research the modulus symbol in java: %

It can be used to determine whether a number is even, with the number 2, or odd based on its output, for example 5 % 2 would return 1 meaning its odd whereas if say would substitute in 4 ( 4 % 2 ) would return 0.

As regards to your program, this will do it:

int total = 0;
		
		for( int i = 0; i < 61; i++ )
		{
			if( i % 2 == 0 )
			{
				total = total + i;
			}
		}
		System.out.println( total );

It gave me 930, is that right, I haven't manually worked it out :D
Hope this helps.

Katana, actually his method is more efficient. You check every number to see if its even; he simply outputs all the even numbers -- that is, he has 1/2 as many loops.

Also, he was looking to print them, not sum them.

So:

for (int i = 2; i < 61; i += 2) 
    		System.out.print(i + " ");

Output: 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 52 54 56 58 60

Edited 5 Years Ago by ztini: n/a

This article has been dead for over six months. Start a new discussion instead.