Program to find sum of n natural number in java.
import java.util.*;
public class sumofnumbers {
public static void main(String[] args) {
int sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of terms=");
int n=sc.nextInt();
for(int i=1; i<=n; i++){
sum=sum+i;
}
System.out.println("the sum of " +n+" natural numbers is ="+sum);
}
}
■》Output->
enter the value of terms=>10
the sum of 10 natural numbers is =>55
Comments
Post a Comment