Q:

Java program to find addition of N integer numbers

0

Java program to find addition of N integer numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to convert Decimal to Binary... >>
<< Java program to addition of one dimensional and tw...