KnowledgeBoat Logo

Computer Applications

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
Total 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, display the message "Special 2 - digit number" otherwise, display the message "Not a special two-digit number".

Java

Java Conditional Stmts

ICSE 2014

45 Likes

Answer

import java.util.Scanner;

public class KboatSpecialNumber
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a 2 digit number: ");
        int orgNum = in.nextInt();
        
        if (orgNum < 10 || orgNum > 99) {
            System.out.println("Invalid input. Entered number is not a 2 digit number");
            System.exit(0);
        }
        
        int num = orgNum;
        
        int digit1 = num % 10;
        int digit2 = num / 10;
        num /= 10;
        
        int digitSum = digit1 + digit2;
        int digitProduct = digit1 * digit2;
        int grandSum = digitSum + digitProduct;
        
        if (grandSum == orgNum)
            System.out.println("Special 2-digit number");
        else
            System.out.println("Not a special 2-digit number");
            
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of 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 Total 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, display the message "Special 2 - digit number" otherwise, display the message "Not a special two-digit number".

Answered By

25 Likes


Related Questions