Wednesday, April 17, 2013

Factorial Example in Java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FactorialEx {


public static void main(String[] args) throws NumberFormatException,
IOException {

InputStreamReader input = new InputStreamReader(System.in);
BufferedReader bu = new BufferedReader(input);
System.out.println("Enter Integer value:");
int n = Integer.parseInt(bu.readLine());
int fact = 1;
for (int i = n; i >= 1; i--) {
fact = fact * i;
}
System.out.println("Factorial of " + n + " is : " + fact);
}

}

/*
Output:
Enter Integer value:
5
Factorial of 5 is : 120

*/

No comments:

Post a Comment