Write Java program to Find addition of N integer numbers


This Java program takes input from the user to add a series of numbers. The program uses a for loop to iterate through the specified number of inputs and add each number to a running total.

  • The program begins by creating a Scanner object to read input from the user. It then prompts the user to enter the limit or number of inputs to be added. The program stores the input in the variable n.
  • The program uses a for loop to iterate n times. In each iteration, the program prompts the user to enter the next number to be added and reads the input using the nextInt() method of the Scanner object. The program then adds the number to a running total stored in the variable sum.
  • After the loop has completed, the program prints the total of the numbers entered by the user using System.out.println().

Overall, this program is a simple way to add a series of numbers in Java using a for loop. However, it assumes that the user will always enter valid integers, and it does not perform any error handling or input validation. For a more robust program, it may be necessary to include additional code to handle errors or invalid input.

Source Code

import java.util.Scanner;
class Add_Numbers
{
	public static void main(String[] args)
	{
		int n,num,sum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the Limit :");
		n = sc.nextInt();
 
		for(int i=1; i<=n ; ++i)
		{
			System.out.printf("Enter Number %d :",i);
			num = sc.nextInt();
			sum = sum + num;
		}
		System.out.println("Sum of given Numbers : " + sum);
	}
}

Output

Enter the Limit :3
Enter Number 1 :23
Enter Number 2 :18
Enter Number 3 :9
Sum of given Numbers : 50

Example Programs