Factorial of a given number

Factorial of a number is the product of all positive descending integers. Factorial of n is denoted by n!.
For example,

3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1  = 24
5! = 5 * 4 * 3 * 2 * 1 = 120

Here you have to get the number from command line argument. The logic to find the factorial of a number is given below.

PROGRAM
class Factorial {

 public static void main(String[] args) {

  int fact=1,no;
  no = Integer.parseInt(args[0]);
  
  System.out.print("Factorial of "+no+" is ");
  
  while(no > 0) {
   
   fact = fact * no;
   no--;
  }
  System.out.println(fact);
 }
}
OUTPUT
C:\>javac Factorial.java
C:\>java Factorial 4
Factorial of 4 is 24

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative