KnowledgeBoat Logo

Computer Applications

Write a program to calculate and display the factorials of all the numbers between 'm' and 'n' (where m<n, m>0, n>0).

[Hint: factorial of 5 means: 5!=5*4*3*2*1]

Java

Java Nested for Loops

48 Likes

Answer

import java.util.Scanner;

public class KboatFactRange
{
    public static void main(String args[]) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter m: ");
        int m = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();

        if (m < n && m > 0 && n > 0) {
            for (int i = m; i <= n; i++) {
                long fact = 1;
                for (int j = 1; j <= i; j++)
                    fact *= j;
                System.out.println("Factorial of " + i + " = " + fact);
            }
        }
        else {
            System.out.println("Invalid Input");
        }

    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to calculate and display the factorials of all the numbers between 'm' and 'n' (where m n, m>0, n>0). [Hint: factorial of 5 means: 5!=5*4*3*2*1]

Answered By

18 Likes


Related Questions