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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; class sum { public static void main(String arg[]) { int n,sum=0; Scanner sc=new Scanner(System.in); System.out.println("enter how many numbers you want sum"); n=sc.nextInt(); int a[]=new int[n]; System.out.println("enter the "+n+" numbers "); for(int i=0;i<n;i++) { System.out.println("enter number "+(i+1)+":"); a[i]=sc.nextInt(); } for(int i=0;i<n;i++) { sum+=a[i]; } System.out.println("sum of "+n+" numbers is ="+sum); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
enter how many numbers you want sum 5 enter the 5 numbers enter number 1: 32 enter number 2: 12 enter number 3: 43 enter number 4: 212 enter number 5: 23 sum of 5 numbers is =322 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.Scanner; class sum { public static void main(String arg[]) { int n; int s=0,a[]; Scanner sc=new Scanner(System.in); System.out.println("Enter how many numbers you want sum"); n=sc.nextInt(); a=new int[n]; System.out.println("enter the "+n+" numbers "); for(int i=0;i<n;i++) { System.out.println("enter number "+(i+1)+":"); a[i]=sc.nextInt(); } s=sum.sumofnum(a,n-1,s); System.out.println("sum is ="+s); } static int sumofnum(int a[],int n,int s1) { if(n<0) return s1; else { s1+=a[n]; return sum.sumofnum(a,n-1,s1); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter how many numbers you want sum 4 enter the 4 numbers enter number 1: 100 enter number 2: 200 enter number 3: 300 enter number 4: 400 sum is =1000 |
3. Using Static Method
Another example program using the static method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Scanner; class sum { public static void main(String arg[]) { int n; int s=0,a[]; Scanner sc=new Scanner(System.in); System.out.println("enter how many numbers you want sum"); n=sc.nextInt(); a=new int[n]; System.out.println("enter the "+n+" numbers "); for(int i=0;i<n;i++) { System.out.println("enter number "+(i+1)+":"); a[i]=sc.nextInt(); } s=sum.sumofnum(a,n); System.out.println("sum is ="+s); } static int sumofnum(int a[],int n) { int s1=0; for(int i=0;i<n;i++) { s1+=a[i]; } return s1; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
enter how many numbers you want sum 4 enter the 4 numbers enter number 1: 12345 enter number 2: 02468 enter number 3: 13579 enter number 4: 235711 sum is =264103 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class sum { public static void main(String arg[]) { int n,sum=0,i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter how many numbers you want sum"); n=sc.nextInt(); int a[]=new int[n]; System.out.println("Enter the "+n+" numbers "); while(i<n) { System.out.println("Enter number "+(i+1)+":"); a[i]=sc.nextInt(); sum+=a[i]; i++; } System.out.println("sum is ="+sum); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter how many numbers you want sum 4 Enter the 4 numbers Enter number 1: 12 Enter number 2: 12 Enter number 3: 13 Enter number 4: 13 sum is =50 |