News

# Calculate Factorial of an Integer with Command Line Argument in Java

Hope our previous discussion helped you to understand how to give input to a Java Program through Command Line. Here we will calculate the factorial of an integer number in Java, taking input from the user as command line argument. I think you all aware of how to calculate factorial of an integer number, and you are ready after setting your Java classpath.

Suppose we want to calculate factorial of 5 then, the result will be 1 x 2 x 3 x 4 x 5 = 120. Therefore factorial of 5 is 120. Now we will see how to achieve the same.

## Program

```class CalculateFactorial {
public static void main(String args[]) {
int number;
int fact = 1;
int argsLength = args.length;
if(argsLength==1) {
number = Integer.parseInt(args[0]);
for(int count=1;count<=number;count++) {
fact = fact*count;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
else if(argsLength>1) {
System.out.println("More than one number is entered...");
}
else {
System.out.println("No arguments entered...");
}
}
}```

## Key Concepts

1. Wrapper Class.
2. String to Integer Conversion.
3. Incorporate Validation.

## Explanation of the Java Code & Output

In Java there is so many primitive data types like in C, C++ like int, short, long, float, double, char, boolean, byte.

As we know that Java is a pure object-oriented language and everything in Java is object. So, these data types cannot participate in any object manipulations, such as being returned from a method as object or being added to collections of objects. Java has come up with a mechanism to wrap the primitive data types in an object so that these can be included in activities reserved for objects. This is accomplished by using Wrapper Class.

Wrapper Class is a wrapper around a primitive data types. There is a wrapper class for every primitive data type in Java. This class encapsulates a single value for the primitive data type. e.g. Wrapper class for int is Integer, for float is Float. Remember that the primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int, which maps to Integer.

Here one important concept is unlike C, C++ String is a class in Java rather than a data type. The argument those are passed through Command Line are stored in a String array and passed to main() method. Therefore each value in the String array is String. But for arithmetical manipulation those String values has to be converted to integer. This accomplished using parseInt() method of Integer wrapper class which takes String input and returns the primitive data type int.

We might want to convert the String content to int/float data type.

Then:

int number= Integer.parseInt(“12”);

float num = Float.parseFloat(“12.45”);

But if you want to convert the String value to int which cannot actually be converted to int, then it will throw an exception.

Like –

int number = Integer.parseInt(“world”);   // cannot be done because it will throw an exception cause “world” is not a number.

Now if no argument is passed then it will show that no arguments passed. It has to take only one input, if more than one input is given then definitely factorial will not be calculated. It will show more than one input has been given. Depending upon the length of the String array it can be achieved. So now you should be somewhat master in working with command line arguments for simple Java programs. This is how to calculate factorial of an integer with command line argument in Java.

Next we will learn how to create objects of a class in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.