KnowledgeBoat Logo

Computer Applications

Write a program to print factorial of a given number.

Java

Java Iterative Stmts

15 Likes

Answer

import java.util.Scanner;

public class KboatFactorial
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = in.nextInt();
        
        long f = 1;
        
        for (int i = 1; i <= n; i++) { 
            f *= i;
        }
        
        System.out.println("Factorial of " + n 
                            + " = " + f);
                
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to print factorial of a given number.

Answered By

6 Likes


Related Questions