Program For Factorial Numbers

Posted by Somesh Shinde On Tuesday, 28 June 2016 0 comments
Program For Factorial Numbers


Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1


prodnprod
prod*n
155
5420
20360
602120
1201120

ProgramOutput
//Note: Scanner class work with JDK1.5 or above

import java.util.*;
class Factorial
{
 public static void main(String args[])
 {  
  int n, i, prod=1;
  Scanner scan= new Scanner(System.in);
  System.out.println("Please Enter a No.");
  n=scan.nextInt();
  for(i=n;i>=1;i--)
  {
   prod*=i; //prod=prod*i;
  }
  System.out.println("Factorial of " + n + " is  " + prod);
 }
}
      
Please Enter a No.: 5
Factorial of 5 is 120
    

0 comments:

Post a Comment