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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.