Special Two Digit Number in Java | ICSE Computer

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 its 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, output the message “Special 2-digit number” otherwise, output the message “Not a special 2-digit number”.

import java.util.Scanner;
class Special2DigitNumber
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = in.nextInt();
        if(n>9 && n<100) //if the number is of 2 digits
        {
            int l = n%10;
            int f = n/10;
            
            int s = f+l;//sum
            int p = f*l;//product
            
            if(n == (s+p))
                System.out.println("Special 2 digit no.");
            else
                System.out.println("Not a Special 2 digit no.");
        }
        else
        {
            System.out.println("Number is not of 2 digits");
        }
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *