C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » Java solved programs » Core java programs

Java program to find addition of N integer numbers






Learn: How to write a java program to add N numbers in java? Here we will learn add N number program in java?
Submitted by Preeti Jain, on March 11, 2018

Given N integer numbers (user will read numbers one by one) and we have to find sum/addition of all given numbers.

/* import scanner class because we are 
using scanner class in our program */

import java.util.Scanner;

class AddNNumberClass{
	public static void main(String[] args){
		/* nums variable is used for how many numbers we want */
		/* number variable is used to store input numbers */
		/* sum variable is used to store sum of all the given numbers */

		int nums,number,sum = 0;
		Scanner sc = new Scanner(System.in);

		/* display message */
		System.out.println("Enter number in digits");

		/* Accept input from user */  
		nums = sc.nextInt();

		/* Loop executes till nums */
		for(int i=1; i<=nums ; ++i){
			System.out.println("Enter Number :" + i);
			/* Accept nums times input counting from 1 */ 
			number = sc.nextInt();
			/* add number to the sum and store it in sum */
			sum = sum + number;
		}

		/* Print sum of all the given N numbers */
		System.out.println("Sum of given numbers are : " + sum);
	}
}

Output

D:\Java Articles>java AddNNumberClass
Enter number in digits
5
Enter Number :1
100
Enter Number :2
200
Enter Number :3
300
Enter Number :4
400
Enter Number :5
500
Sum of given numbers are : 1500






You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions