Write a java program for sum of n natural number using user input :-
import java.util.Scanner;
public class SumNaturalNumber {
public static void main(String[] args) {
System.out.println("Enter Natural Number");
int n=sc.nextInt();
int sum=0;
int t=n;
while(n>0) {
sum=sum+n;
n--;
}
System.out.println("Sum of first "+t+" is "+sum);
}
static Scanner sc=new Scanner(System.in);
}
Comments
Post a Comment