Sunday, January 29, 2012

JAVA Programming : Factorial

JAVA Factorial 


Factorial is a mathematical operation Factorial of a positive number n is denoted by n! and defined as the product of all numbers from 1 to the n. The syntax of factorial of number n is:


  n! = n*(n-1)*(n-2)*(n-3)......2*1  


For Example:-


       7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040       



Note:- Factorial of zero is 1 (i.e. 0! = 1).

import java.util.Scanner;
public class Factorial {
  public int fact(int n){
    int fact =1;
    for (int i =; i <= n; i++) {
      fact=fact*i;
    }
    return fact;
  }

  public static void main(String[] args) {
    Factorial f = new Factorial();
    System.out.println("Enter a Nu4mber");
    Scanner scan = new Scanner(System.in);
    int num = scan.nextInt();
    int result = f.fact(num);
    System.out.println("Facotial of "+num+" is "+result);
  }
}

Output of Program:-

1 comment:

  1. How did you embed your java applets on Blogger?

    ReplyDelete