Calculate Factorial of an Integer with Command Line Argument in Java

In this post, we will learn how to calculate factorial of an integer with Command Line Arguments in Java. We will take input from the user on the Command Prompt. I hope you all aware of how to calculate factorial of an integer number, and you are ready after setting your Java classpath.

Calculate Factorial of an Integer with Command Line Argument in Java

  1. Example Data
  2. Program
  3. Key Concepts
  4. Output
  5. Explanation of Java Code and output
  6. Convert the String content to int/float data type

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

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

  • Wrapper Class.
  • String to Integer Conversion.
  • Incorporate Validation.

Output

Calculate Factorial of an Integer with Command Line Argument in Java

Explanation of the Java Code & Output

In Java, there are 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 part of the activities reserved for Objects. It becomes possible by using the Wrapper Class.

Wrapper Class is a wrapper around a primitive data types. It is available for every primitive data type in Java. It 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 merely 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 that is passed through Command-Line is stored in a String array and passed to the main() method. Therefore each value in the String array is String. But for arithmetical manipulation, those String values need to be an integer. It 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 or 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 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 provide 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. It is how to calculate factorial using command-line arguments in Java.

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

About the author

Avatar photo

Mary James

1 Comment

Click here to post a comment