Home / Programs / A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number. Example: Consider the number 59.Sum of digits = 5 + 9 = 14Product of digits = 5 * 9 = 45Sum of the sum of digits and product of digits = 14 + 45 = 59 Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".
Programming Example

A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.

Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59

Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".

👁 197 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

import java.util.Scanner;

public class RAnsariSpecialNumber {
    // Method to check if a number is a special 2-digit number
    public void checkNumber() {
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a 2-digit number: ");
        int orgNum = in.nextInt();
        
        // Check if the input is a 2-digit number
        if (orgNum < 10 || orgNum > 99) {
            System.out.println("Invalid input, please enter a 2-digit number");
            return; // Exit the method early
        }
        
        int num = orgNum;
        int digitSum = 0, digitProduct = 1;
        
        // Calculate the sum and product of the digits
        while (num != 0) {
            int digit = num % 10;
            num /= 10;
            digitSum += digit;
            digitProduct *= digit;
        }
        
        // Check if the sum + product equals the original number
        if ((digitSum + digitProduct) == orgNum)
            System.out.println(orgNum + " is a special 2-digit number");
        else
            System.out.println(orgNum + " is not a special 2-digit number");
        
        // Close the Scanner to prevent resource leaks
        in.close();
    }

    // Main method - Entry point of the program
    public static void main(String[] args) {
        // Create an object of the class and call the checkNumber method
        RAnsariSpecialNumber specialNumber = new RAnsariSpecialNumber();
        specialNumber.checkNumber();
    }
}

Output

Enter a 2-digit number: 59
59 is a special 2-digit number
Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.