Tuesday, December 13, 2011

Factorial program without recursion in java

import java.util.*;
import java.io.*;
class fact
{
public static void main(String args[])throws Exception
{
int i,fact=1;
System.out.println("Enter a number");
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(k.readLine());
for(i=1;i<=n;i++)
fact=fact*i;
System.out.println("factorial of "+n+" is:"+fact);
}
}

Output:
enter a number
5
factorial of 5 is:120