Menu

Read a number from the user and Display sum of even numbers below n;


import java.util.*;
class Sum_of_even_numbers_upto_n{
public static void main(String args[]){

System.out.print("Upto which number, you want to add add even numbers?");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int sum=0;

if(n>=1)
{
for(int i=1; i<=n;i++)
{

if(i%2==0)
sum=sum+i;
}
}

System.out.println("The sum of even numbers upto " +n+" is "+sum);


}}