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 factorial of an integer with Command Line Argument 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.

Calculate Factorial of an Integer with Command Line Argument in Java


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.


Calculate Factorial of an Integer with Command Line Argument in Java

Explanation of the Java Code & Output

In Java, there are 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. Everything in Java is an object. So, these data types cannot participate in any object manipulations i.e. being returned from a method as an 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 a 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 is 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 need to be an integer. This is done using parseInt() method of Integer wrapper class which takes String input and returns the primitive data type int.

Convert the String content to int/float data type.


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

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

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

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 sent then it will show that no arguments passed. It can take only one input. If more than one input is given then definitely factorial will not be calculated. It will give an error saying “more than one input has been given”.

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 a command line argument in Java.

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.