Java program to sum of N numbers


Introduction

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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

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

java techstudy.org

     





Related Topics




Popular Posts