Pages

Java Program to Find Factorial

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
5! = 5  \times  4  \times  3  \times  2  \times  1 = 120.  \
The value of 0! is 1, according to the convention for an empty product


 import java.util.Scanner;
  public class Factorial {
  public static void main(String[] args) {
    int num  ,fact=1;
    System.out.println("enter number:");
    Scanner sc=new Scanner(System.in);

    num=sc.nextInt();
    if(num<0){
    System.out.println("only positive number !");
   }
   else{

    for(int i=1;i<=num;i++)
    {
     fact=fact*i;
    }
    System.out.println("the factorial of "+num + " is " +fact);
    }
   }
 }

Output:
=======

 enter number: 3
 the factorial of 3 is 6

No comments:

Post a Comment