Computer Applications

Write a program to compute and display factorials of numbers between p and q where p > 0, q > 0, and p > q.

Java

Java Nested for Loops

2 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 p: ");
        int p = in.nextInt();
        System.out.print("Enter q: ");
        int q = in.nextInt();

        if (p > q && p > 0 && q > 0) {
            for (int i = q; i <= p; 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");
        }

    }
}

Output

Answered By

1 Like


Related Questions