public class Main { public static void main(String args[]) { int x=1,n=5,sum=0; while(x<=n) { sum=sum+x; x++; } System.out.print(sum); } }
15
public class Main { public static void main(String args[]) { int x=1,n,sum=0; n=Integer.parseInt(args[0]); while(x<=n) { sum=sum+x; x++; } System.out.print(sum); } }
15
import java.util.*; public class Main { 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(); while(x<=n) { sum=sum+x; x++; } System.out.print(sum); } }
Enter n value : 5
15