Factorial of a number using Java



Write a program in Java to find the factorial of a number


Program:





class
Sk03


{


  public static void main(String args[])


  {


  short n= 5;


  int fact=1;


  for(int i=1;i<=n;i++)


  {


      fact=fact*i;


  }


  System.out.println("The factorial of
"+n+" is "+fact);


}


}





Compile:


D:\>javac
Sk03.java





Run:


D:\>java
Sk03





Output:


The
factorial of 5 is 120
















0 Comments