We are going to share a Java program sum of N numbers. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a Java program sum of N numbers.
Copy the below Java program and execute it with the help of Javac compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class sumOfnNumber { 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); } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.