Find factorial of a number

Algorithm

Input	An integer. 	   
Output	Factorial of given number.	   
complexity O(n)	 

Factorial(num)
1   if (num=0 or num=1) then
2   fact = 1;
3    else
4   for i 1 to n  
5      fact=fact*i;  
6   print fact    

Algorithm Description

Factorial of a number is multiplying the numbers from 1,2,3…n where n is the number whose factorial is to be found out. There are two methods called recursive and iterative.In recursive method program calls the factorial function again and again until the terminal condition is reached. In the iterative program it keeps on multiplying the next number with the factorial calculated till now until it reaches the number for which factorial is to be calculated.

C Code

//Program to find factorial of number(1 - 12)
#include< stdio.h>
#include< conio.h>

int main()
{
	int n,i;
	long fact=1;
	printf("Enter number ");
	scanf("%d",&n);
	for(i=n;i> 1;i--)
	{
		fact=fact*i;
	}
	printf("factorial is %ld",fact);
}

JAVA Code

import java.io.*;
class factorial{
	 int fact(int x)
	{
		int result;
		if(x==1)
			return 1;
		result=x*fact(x-1);
		return result;
	}
}
class fact_rec
{
	public static void main(String args[]) throws Exception
	{
		factorial f = new factorial();
		int x;
		BufferedReader cin= new BufferedReader (new InputStreamReader(System.in));
		System.out.println("enter the  number ");
		x= Integer.parseInt(cin.readLine());
		if(x==0)
			System.out.println("the factorial of "+x+" is 0");
		else	
			System.out.println("the factorial of "+x+" is "+f.fact(x));
	}
}