Wednesday, 19 August 2015

Find out factorial of Number.

package com.JavaInterview.Programs;

import java.util.*;

/**
 * Find Out Factorial of a number
 * 
 * 
 */
public class Factorial {

 public void findFactorial(int n) {
  int fact = 1;
  if (n < 0) {
   System.out.println("The Number should be non-negative");
  } else {
   for (int i = 1; i <= n; i++) {
    fact = fact * i;
   }
  }
  System.out.println("factorial Of Number  " + n + " is : " + fact);
 }

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);
  System.out.println("Please enter the number :");
  int num = input.nextInt();

  Factorial f = new Factorial();
  f.findFactorial(num);

 }

}

Output :
Please enter the number :
6
factorial Of Number  6 is : 720


No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...