KnowledgeBoat Logo

Computer Applications

Define a class to accept a number and check whether it is an FDS Number or not. A number is called an FDS Number if the sum of the factorials of its digits equals the number itself.

Example 1:

Input: 145
Output: FDS Number [1! + 4! + 5! = 1 + 24 + 120 = 145]

Example 2:

Input: 123
Output: Not an FDS Number [1! + 2! + 3! = 1 + 2 + 6 ≠ 123]

import java.util.Scanner;

class KboatFDSNum {
    
    static int fact(int d) {
        int f = 1;
        _______(1)_________ {
            _______(2)_________
        }
        _______(3)_________
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = in.nextInt();

        int t = num, sum = 0;
        
        _______(4)_________ {
            _______(5)_________
            _______(6)_________
            _______(7)_________
        }
        
        _______(8)_________ {
            _______(9)_________
        } else {
            _______(10)_________
        }
    }
}

Java Iterative Stmts

3 Likes

Answer

  1. for (int i = 1; i <= d; i++)
  2. f *= i;
  3. return f;
  4. while (t > 0)
  5. int d = t % 10;
  6. sum += fact(d);
  7. t /= 10;
  8. if (sum == num)
  9. System.out.println(num + " is an FDS Number.");
  10. System.out.println(num + " is not an FDS Number.");

Explanation

import java.util.Scanner;

class KboatFDSNum {
    
    static int fact(int d) {
        int f = 1;
        for (int i = 1; i <= d; i++) {
            f *= i;
        }
        return f;
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = in.nextInt();

        int t = num, sum = 0;
        
        while (t > 0) {
            int d = t % 10;
            sum += fact(d);
            t /= 10;
        }
        
        if (sum == num) {
            System.out.println(num + " is an FDS Number.");
        } else {
            System.out.println(num + " is not an FDS Number.");
        }
    }
}

Variable Description Table

Function: fact

VariableData TypePurpose
dintRepresents the digit whose factorial is to be calculated.
fintUsed to store the factorial of the digit d, initialized to 1.

Function: main

VariableData TypePurpose
inScannerAn instance of Scanner to read input from the user.
numintThe number input by the user to check if it is an FDS number.
tintA temporary variable used to hold the value of num during processing.
sumintUsed to store the sum of factorials of digits of the number.
dintUsed inside the while loop to hold the current digit extracted from t.

Program Explanation

Let's go through the Java program step by step to understand how it works:

1. Import Scanner Class:

The program begins by importing the Scanner class, which is part of the java.util package. This class is used to read user input from the keyboard.

import java.util.Scanner;

2. Class Declaration:

The class KboatFDSNum is declared. This class contains the main method and a static method for calculating factorials.

class KboatFDSNum {

3. Factorial Method (fact):

A static method fact(int d) is defined to compute the factorial of a given integer d.

  • The method initializes a local variable f to 1.
  • It then uses a for loop to multiply f by every integer from 1 to d.
  • Finally, it returns the computed factorial.
static int fact(int d) {
    int f = 1;
    for (int i = 1; i <= d; i++) {
        f *= i;
    }
    return f;
}

4. Main Method:

The main method is the entry point of the program and is responsible for the execution of the FDS Number check.

public static void main(String args[]) {

5. User Input:

An instance of the Scanner class is created to read input from the user. The program prompts the user to enter a number and stores this input in the variable num.

Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();

6. Initialization for Checking:

Two variables t and sum are initialized. t is set to the value of num to preserve the original number, while sum is initialized to 0 to accumulate the sum of the factorials of the digits.

int t = num, sum = 0;

7. Sum of Factorials of Digits:

A while loop is used to extract each digit of the number t, compute its factorial using the fact method, and add the factorial to sum. The loop continues until all digits of the number t have been processed (t > 0).

while (t > 0) {
    int d = t % 10;  // Extract the last digit
    sum += fact(d);  // Add factorial of the digit to sum
    t /= 10;         // Remove the last digit from t
}

8. Check and Output Results:

After the loop, the program checks if the sum of the factorials of the digits (sum) is equal to the original number (num). If they are equal, it prints that the number is an FDS Number. Otherwise, it prints that the number is not an FDS Number.

if (sum == num) {
    System.out.println(num + " is an FDS Number.");
} else {
    System.out.println(num + " is not an FDS Number.");
}

Output

Define a class to accept a number and check whether it is an FDS Number or not. A number is called an FDS Number if the sum of the factorials of its digits equals the number itself. Practice Test ICSE Computer Applications Class 10
Define a class to accept a number and check whether it is an FDS Number or not. A number is called an FDS Number if the sum of the factorials of its digits equals the number itself. Practice Test ICSE Computer Applications Class 10

Answered By

1 Like


Related Questions