You are here: Home / Topics / Get Sum of n numbers using For Loop in Java

Get Sum of n numbers using For Loop in Java

Filed under: Java on 2023-08-18 06:36:56

This program will print the sum of n numbers. 

// sum of numbers using command line

class  ArguSum
{
   public static void main( String args[ ] )
   {
 int  s = 0, len;
 len = args.length;
 
 for( int i=0 ; i<len ; i++ )
 {
  int  x = Integer.parseInt( args[ i ] );
  s = s + x;
 }
 System.out.println( " Sum of Command-Line Arguments ::: " + s );
  }
}


Output:

D:\Java Examples\java ArguSum.java 1 2 3 4 5 6 7 8 9 10
Sum of Command-Line Arguments ::: 55

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.