1. Write a program to input a 2-digit number and print
the sum and product of the digits. in java
Answers
Answer:
import java.util.Scanner;
public class KboatSpecialNumber
{
public void checkNumber() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a 2 digit number: ");
int orgNum = in.nextInt();
int num = orgNum;
int digit1 = num % 10;
num /= 10;
/*
* If num becomes 0 after extracting first digit
* it means that user entered a single digit number.
* We should check for such invalid inputs.
*/
if (num == 0) {
System.out.println("Invalid input. Entered number is not a 2 digit number");
System.exit(0);
}
int digit2 = num % 10;
num /= 10;
/*
* If num does not become 0 after extracting second digit
* it means that entered number has 3 or more digits.
* We should check for such invalid inputs.
*/
if (num != 0) {
System.out.println("Invalid input. Entered number is not a 2 digit number");
System.exit(0);
}
int digitSum = digit1 + digit2;
int digitProduct = digit1 * digit2;
if ((digitSum + digitProduct) == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");
}
}
Explanation: