45. JAVA Program to find Sum of n Even Numbers.
How does this program work?
- In this program your going to learn about how to find Sum of n even numbers using Java.
Here is the code
//Java Program to Calculate Sum of Even Numbers
import java.util.Scanner;public class Even
{
public static void main(String[] args )
{
int number, i, evensum = 0;
System.out.println("Enter Number : ");
Scanner obj = new Scanner( System.in);
number = obj.nextInt();
for ( i = 1; i <= number; i++ )
{
if ( i % 2 == 0 )
{
System.out.print(i+" " );
evensum = evensum + i;
}
}
System.out.println("\n The Sum of Even Numbers upto " + number + " = " + evensum);
}
}