Factorial Program in Java

Sharing is healthy !!

Hey guys!! Let’s move ahead and learn factorial program in java using loop, that is, for loop.

Earlier you learnt above mentioned program using recursion and also its definition. Here in this post scanner class is used and imported from java.util package. So, let’s see code implementation,

example: Factorial Program in Java

Also Read – Java Calculate Percentage

import java.util.Scanner;

public class Factorial_Program {

public static void main(String[] args) {

int x;
int y;
int factorial = 1;

System.out.println("Please enter an integer to find its factorial: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();

if(x < 0){
System.out.println("Only positive numbers should be entered");
for(y = 1;y <= x;y++) // For loop starts here
factorial = factorial * y;
System.out.println("Factorial of " + x + " is = " + factorial);


Factorial Program in Java

Sharing is healthy !!