Sum Of Even Numbers DoWhile Loop

  Sum Of Even Numbers DoWhile Loop 


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println(sum);

}



}


Output:

Enter n value

5

6


No comments:

Post a Comment