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:-

2 comments:

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

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete