Tuesday 8 August 2017

Program for sum of n natural numbers

import java.util.*;
public class SumOfNaturalNumbers{

public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter number to find sum of n natural numbers");
int sum=0;
int n=s.nextInt();
for(int i=0;i<=n;i++)
{
sum=sum+i;
}

System.out.println("here sum of  "+n+"  numbers");
System.out.println(sum);

}

}


OUTPUT:

Enter number to find sum of n natural numbers
10
here sum of  10  numbers
55

No comments:

Post a Comment