Java Program to find the Sum of N numbers - The Coding Shala

Home >> Java Programs >> Find Sum of N numbers

 In this post, we will learn how to find the Sum of N numbers in Java.

Java Program to Find the Sum of N numbers

Write a Java Program to Find the Sum of N numbers, take all the numbers as input using Scanner, and print the sum of n numbers.

Example 1:
Input: elements: 1,2,3,4,5
Output: 15

Java Program: 

// Java program to find Sum of N numbers

import java.util.Scanner;

public class Main {
	public static void main(String[] args) { 
		Scanner sc = new Scanner(;
		System.out.println("How many numbers are there?");
		int num = sc.nextInt();
		double sum = 0;
		for(int i = 0; i < num; i++) {
			System.out.println("Enter " + (i+1) + " element: ");
			int n = sc.nextInt();
			sum = sum + n;
		System.out.print("Sum of " + num + " numbers is: " + sum);


How many numbers are there?
Enter 1 element: 
Enter 2 element: 
Enter 3 element: 
Enter 4 element: 
Enter 5 element: 
Sum of 5 numbers is: 15.0

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala