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
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, then display the message "Special two—digit number" otherwise, display the message "Not a special two-digit number".
Java Programming Language (Article) (Program)
178
Given Input:
Expected Output:
Program:
import java.util.Scanner;
public class RAnsariSpecialNumber {
// Method to check if a number is a special 2-digit number
public void checkNumber() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a 2-digit number: ");
int orgNum = in.nextInt();
// Check if the input is a 2-digit number
if (orgNum < 10 || orgNum > 99) {
System.out.println("Invalid input, please enter a 2-digit number");
return; // Exit the method early
}
int num = orgNum;
int digitSum = 0, digitProduct = 1;
// Calculate the sum and product of the digits
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
}
// Check if the sum + product equals the original number
if ((digitSum + digitProduct) == orgNum)
System.out.println(orgNum + " is a special 2-digit number");
else
System.out.println(orgNum + " is not a special 2-digit number");
// Close the Scanner to prevent resource leaks
in.close();
}
// Main method - Entry point of the program
public static void main(String[] args) {
// Create an object of the class and call the checkNumber method
RAnsariSpecialNumber specialNumber = new RAnsariSpecialNumber();
specialNumber.checkNumber();
}
}
Output:
Enter a 2-digit number: 59
59 is a special 2-digit number
Press any key to continue . . .
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
Program:
import java.util.Scanner; public class RAnsariSpecialNumber { // Method to check if a number is a special 2-digit number public void checkNumber() { Scanner in = new Scanner(System.in); System.out.print("Enter a 2-digit number: "); int orgNum = in.nextInt(); // Check if the input is a 2-digit number if (orgNum < 10 || orgNum > 99) { System.out.println("Invalid input, please enter a 2-digit number"); return; // Exit the method early } int num = orgNum; int digitSum = 0, digitProduct = 1; // Calculate the sum and product of the digits while (num != 0) { int digit = num % 10; num /= 10; digitSum += digit; digitProduct *= digit; } // Check if the sum + product equals the original number if ((digitSum + digitProduct) == orgNum) System.out.println(orgNum + " is a special 2-digit number"); else System.out.println(orgNum + " is not a special 2-digit number"); // Close the Scanner to prevent resource leaks in.close(); } // Main method - Entry point of the program public static void main(String[] args) { // Create an object of the class and call the checkNumber method RAnsariSpecialNumber specialNumber = new RAnsariSpecialNumber(); specialNumber.checkNumber(); } }
Output:
Enter a 2-digit number: 59 59 is a special 2-digit number Press any key to continue . . .
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.