Special two-digit number

A special two-digit number is such that when the sum of the 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 x 9 = 45
Sum of the 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-two digit number” otherwise, output the message “Not a special two-digit number”.
import java.io.*;
class Special_Q5_ICSE2014
    {
    public static void main(String args[])throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter a 2 digit number : ");
            int n = Integer.parseInt(br.readLine());
             
            int first, last, sum, pro;
            if(n<10 || n>99) //Checking whether entered number is 2 digit or not
                System.out.println("Invalid Input! Number should have 2 digits only.");
            else
                {
                    first = n/10; //Finding the first digit
                    last = n%10; //Finding the last digit
                    sum = first + last; //Finding the sum of the digits
                    pro = first * last; //Finding the product of the digits
                     
                    if((sum + pro) == n)
                    {
                      System.out.println("Output : The number "+n+" is a Special Two-Digit Number.");
                    }
                    else
                    {
                      System.out.println("Output : The number is Not a Special Two-Digit Number.");
                    }
                }
        }
    }

Output:

1. Enter a 2 digit number : 79
Output : The number 79 is a Special Two-Digit Number.
2. Enter a 2 digit number : 47
Output : The number is Not a Special Two-Digit Number.

No comments:

Post a Comment