Programming Examples

Write a program to input an integer and find its factorial.


Write a program to input an integer and find its factorial. Factorial of a number is the product of all natural numbers till that number. For example factorial of 5 is 120 since 1×2×3×4×5=120.

Solution

class Factorial
{
	public static void main(Strign arr[])
	{
		Scanner sc=new Scanner(System.in);
		int i,f=1,n;
		System.out.println("Enter a number:");
		n=sc.nextInt();
		for(i=1;i<=n;i++)
		{
		f=f*i;
		}
		System.out.println("Factorial="+f);
	}
}
Output

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad