Sum Of Natural Numbers DoWhile Loop

 

  • Sum Of Natural 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

{

sum=sum+x;

x++;

}while(x<=n);

System.out.println(sum);

}



}



output:

Enter n value

5

15

No comments:

Post a Comment