codescracker


java

Java Program to Add n Numbers



« Previous Program Next Program »


Add n Numbers

To add n numbers in Java Programming, you have to ask to the user to enter the value of n (how many number he/she want to enter ?), then ask to enter n (required amount of) numbers to perform the addition of all the numbers and display the addition result on the output screen.

Java Programming Code to Add n Numbers

Following Java Program first ask to the user to enter the value of n and then enter the n Number to add them:

Following Java Program add n Numbers entered by the user:

Java Programming Example Code:
/* Java Program Example - Add n Numbers */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int i, n, sum=0, num;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("How many Number You want to Enter to Add them ? ");
        n = scan.nextInt();
		
        System.out.print("Enter " +n+ " numbers : ");
        for(i=0; i<n; i++)
        {
            num = scan.nextInt();
            sum = sum + num;
        }
		
        System.out.print("Sum of all " +n+ " numbers is " +sum);
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program add n numbers

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test