In this page you can learn sum of n numbers.We have given a java program for sum of n numbers.
If we give the n value it will sum upto the n value.
PROGRAM:
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,sum=0;
n=sc.nextInt();
for(i=0;i<=n;i++)
{
sum+=i;
}
System.out.println("Sum of n number is "+sum);
}
}
OUTPUT:
5
Sum of n number is 15
Post a Comment