sum in command line argument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nemish
    New Member
    • Nov 2019
    • 1

    sum in command line argument

    Code:
    class N2
    {
      public static void main(String[] args){
    	  
    	  int a,b,c;
    	 
    	  int  sum=0;
    	 
    	  for(int i=0;i<=args.length;i++)
    	  {	  
    		  {
    			  
    			  sum+=i;
    			  
    		  }
    		  
    	      System.out.println(sum);
    	 }
     
    }
    }
    Last edited by gits; Nov 29 '19, 08:27 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    do you have a question regarding the code you posted?

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      First of all, You have to post the question regarding the code
      But as per your code, I think you want to sum the a,b, and c variable in which value comes from the command line argument
      So you should run the code by the following command in which the parameters are passed at run time

      javac N2.java
      java N2 4 5 6

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        First, OP didn't even use the declared vars. Second, the program won't work unless the values are parsed as int. Third, the output statement to print the sum is in the wrong block.

        Comment

        Working...