Java Program to find Factorial of a Number using While Loop.

Java program to find factorial of any number
import java.util.*;
class ranjan1 
{
  public static void main(String arts[])
  {
    int n,m;
    long f=1;
    Scanner rd=new Scanner(System.in);
    System.out.print("Enter any number:");
    n=m=rd.nextInt();
    if(n<0)
    {
      System.out.println("Factorial of negative number cannot be determined.");
    }
    else
    {
      while(n>0)
      {				
        f=f*n;
	    n--; 
      }
      System.out.println("Factorial of "+m+" is = "+f);				
    }
  }
}

Output

Enter any number : 5
Factorial of 5 is = 120

Post a Comment

0 Comments