Special Two-Digit Number Program in Java and Python
ICSE 2014 solution to check whether a two-digit number is a special two-digit number using digit sum and digit product.
Question:
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:
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.
Example 1
Example 2
Algorithm:
Step 1: Start.
Step 2: Create a Scanner object to accept input from the user.
Step 3: Accept an integer and store it in n.
Step 4: If n < 10 or n > 99, display an invalid input message and go to Step 11.
Step 5: Find the tens digit as first = n / 10.
Step 6: Find the units digit as last = n % 10.
Step 7: Calculate the sum of digits as sum = first + last.
Step 8: Calculate the product of digits as pro = first * last.
Step 9: If sum + pro is equal to n, display that the number is a Special Two-Digit Number.
Step 10: Otherwise, display that the number is not a Special Two-Digit Number.
Step 11: Stop.
Explanation:
A special two-digit number is checked using only its two digits. Since the question is specifically about a two-digit number, the program first validates the input. Any number less than 10 has only one digit, and any number greater than 99 has more than two digits. Such values do not satisfy the basic condition of the question, so the program prints an invalid input message and does not try to perform the special number test.
For a valid two-digit number, the tens digit and units digit are extracted separately. Integer division by 10 gives the first digit. For example, 79 / 10 gives 7 in integer arithmetic. The modulus operator gives the remainder after division by 10, so 79 % 10 gives 9. These two operations are commonly used in number programs where digits must be separated without converting the number into a string.
After separating the digits, the program calculates two values. The first value is the sum of the digits, found by adding the tens digit and the units digit. The second value is the product of the digits, found by multiplying the same two digits. These two results are then added together. If this final value is equal to the original number, the number is special. For example, for 79, the digit sum is 7 + 9 = 16 and the digit product is 7 * 9 = 63. Their total is 79, so 79 is a special two-digit number.
If the total is not equal to the original number, the number does not satisfy the definition. The program uses an if statement to compare sum + pro with n and prints the suitable message. This keeps the logic simple and directly matched to the definition given in the question.
Java Program:
/**
* The class Special_Q5_ICSE2014 inputs a two-digit number and
* checks whether it is a Special Two-Digit Number or not.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ICSE 2014 Question 5
*/
import java.util.Scanner;
class Special_Q5_ICSE2014
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a 2 digit number : ");
int n = sc.nextInt();
if(n < 10 || n > 99)
{
System.out.println("Invalid Input! Number should have 2 digits only.");
}
else
{
// Extract the tens and units digits.
int first = n / 10;
int last = n % 10;
// Calculate sum and product of the two digits.
int sum = first + last;
int pro = first * last;
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.");
}
}
}
}Equivalent Python Program:
# Program to check whether a number is a Special Two-Digit Number.
n = int(input("Enter a 2 digit number : "))
if n < 10 or n > 99:
print("Invalid Input! Number should have 2 digits only.")
else:
# Extract the tens and units digits.
first = n // 10
last = n % 10
# Calculate sum and product of the two digits.
digit_sum = first + last
digit_product = first * last
if digit_sum + digit_product == n:
print("Output : The number", n, "is a Special Two-Digit Number.")
else:
print("Output : The number is Not a Special Two-Digit Number.")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.