Latest :

Java Program Sum Of N Numbers | 4 Simple Ways

Java program to calculate the 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 the 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

[wp_ad_camp_3]

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, let’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 adds 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 another method using recursion: A recursion is a function call itself. Here is the sample program to print the sum of N numbers in Java.

Output:

 

3. Using Static Method

Another example program using the static method

Output:
 

4. Calculate Sum Of N Numbers Using While Loop

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

Output:
x

Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...