Sem 5‎ > ‎JAVA LAB‎ > ‎

P2: WAP to calculate factorial of a number using command line arguments.

posted Aug 19, 2012, 10:10 AM by Neil Mathew   [ updated Aug 19, 2012, 10:11 AM ]

INPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class FactorialOf
{
        public static void main(String []args)
        {
 
        if( args.length != 1 )
        {
        System.out.println(" Only one Variable required. ");
        return;
        }
 
 
        int n= Integer.parseInt(args[0]);
 
        System.out.print("\n Factorial of "+n+" is: ");
 
        int f=1;
        for(int i=1; i<=n; i++)
        {
        f*=i;
        }
 
        System.out.println(f);
 
        }
}
 



OUTPUT:

 



C:\Java 1.6\jdk1.6.0_30\bin>javac src/FactorialOf.java

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 7

 

 Factorial of 7 is: 5040

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 3

 

 Factorial of 3 is: 6

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 9

 

 Factorial of 9 is: 362880

 

C:\Java 1.6\jdk1.6.0_30\bin>java FactorialOf 9 2

 Only one Variable required.

 

Comments