Monday, 3 August 2015

S3P1

Write a Java Program to find Factorial of a Given Number using Command Line Argument.


 class S3P1_cmd_factorial  
 {  
      public static void main(String args[])  
      {  
           int n = Integer.parseInt(args[0]), fact=1;  
           for(int i=n; i>1; i--)  
           {  
                fact = fact*i;  
           }  
           System.out.println("\nFactorial of "+n+" is "+fact);  
      }  
 }  

Output:

   
 E:\Java>javac S3P1_cmd_factorial.java  
   
 E:\Java>java S3P1_cmd_factorial 5  
   
 Factorial of 5 is 120  
   
 E:\Java>  
   

No comments:

Post a Comment