Java Program Sum Of N Numbers | 4 Simple Ways
Beginners Programs :

Java Program Sum Of N Numbers | 4 Simple Ways

Java program to calculate sum of N numbers using arrays, recursion, static method, using while loop. Here is the complete Java program with sample outputs. You can learn more tutorials here and Java interview questions for beginners. With the following program you can even print sum of two numbers or three numbers up to N numbers.

  • How to calculate?

Just summing of two numbers or three numbers or up to N numbers.

Sum Of N Numbers Program

1. Using Arrays

Here is the sample program with output sum of two numbers program or three numbers. check it out

How this program works: we are using arrays to store the values first. From a user input point of view, lest’s assume you need to sum five numbers. Say 5: Now the next step is to enter those numbers in a series order. Once it was done, the program automatically add all your two numbers or three numbers like up to N numbers.

check out the sample output so that you will get an Idea:

Output:
 

2. Using Recursion

Here is the another method using recursion: A recursion is a function call itself. Here is the sample program to print sum of N numbers in Java.

Output:

 

3. Using Static Method

Another example program using static method

Output:
 

4. Calculate Sum Of N Numbers Using While Loop

While Loop : The Loop is used to execute a set a statements a as long as condition is true.

Output: