ICSE Class 10 Computer Applications
Question 18 of 22
Nested for loops — Question 18
Back to all questions 18
Question Question 7
Write a program to compute and display factorials of numbers between p and q where p > 0, q > 0, and p > q.
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
