Q:

Java program to sum of N numbers

belongs to collection: Simple Formula Based Java Programs

0

Java program to sum of N numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
class sumofnnumbers
{
                    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);                  
                    }
}

Result:

Enter how many numbers you want sum

5

Enter the 5 numbers Enter  number  1:

5

Enter  number  2:

6

Enter  number  3:

7

Enter  number  4:

8

Enter  number  5:

9

sum of 5 numbers is =35

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Program to find factorial of number... >>
<< Java program to check vowel or consonant...