43. JAVA Program to find Sum of N Natural Numbers.
How does this program work?
- In this program your going to learn about how to find Sum of n natural numbers using JAVA.
Here is the code
// Sum of n Natural numbers
import java.util.Scanner;public class Sum
{
public static void main(String args[] )
{
int sum = 0,num;
System.out.print("Enter number:\n ");
Scanner obj = new Scanner( System.in);
num = obj.nextInt();
for ( int i = 0; i < num; i++ )
{
sum = sum +i;
}
System.out.println("Sum of natural numbers is : "+sum);
}
}