Wednesday 27 January 2016

Write a program in java input a number and check the number is special two-digit number or not.


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
    {
    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