Share your repls and programming experiences

← Back to all posts
Print the sum of the natural numbers program in java using for loop
RaghunathThilag (1)

Print the sum of the natural numbers program in java using for loop. In a Java program, find the sum of n numbers. It will print the addition of n numbers in the java programming language for beginners, as well as the addition of a user-entered number. The result or answer we get when we add two or more numbers or words is known as sum.

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i, n, sum=0, num;
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers you want to add");
n = scan.nextInt();
System.out.print("Enter " +n+ " number");
for(i=0; i<n; i++)
{
num = scan.nextInt();
sum = sum + num;
}
System.out.print("Sum of all " +n+ " numbers are " +sum);
}
}

Output
How many numbers you want to add5
Enter 5 number
45
56
78
34
89
Sum of all 5 numbers are 302

Link: https://inlarn.com/print-the-sum-of-the-natural-numbers-program-in-java-using-for-loop/