Home Java Programming Language / 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".

👁 205 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

📌 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();
    }
}

                        

🖥 Program Output

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

                            
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.