Factorial Example : Java

This example returns the factorial results of a given number entered by user. To print factorial number, we take the help of for loop and we need 2 variable and 1 constant value. Here is the source code to calculate factorial of a given number :

import java.io.*;
class Factorial{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number : ");
int a= Integer.parseInt(object.readLine());
int b= 1;
System.out.println("Factorial : " +a+ ":");
for (int i= 1; i<=a; i++){
b=b*i;
}
System.out.println(fact);
}
catch (Exception e){}
}
}


1 comments:

david santos said...

Hello, Hem Rai!
I loved this post and this blog.
Have a nice day