The factorial of a positive integer n is equal to 1*2*3*…n. You will learn to calculate the factorial of a number using for loop in this example. This program takes a positive integer from the user and computes factorial using for loop.

// factorial a number
import java.util.Scanner;
class factorial 
{
	public static void main(String[] args) 
	{
		System.out.println("Enter the number:");
		Scanner s=new Scanner(System.in);
		int a=s.nextInt();
		int b=a,sum=1;
		for(int i=1;i<=a;i++)
		{
		sum=sum*b;
		b--;
		}
		System.out.println(sum);
	}
}
(Visited 90 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *