Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 9

Conditional Constructs in Java

Class 9 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Programs - Special Two-Digit Number


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


import java.util.Scanner;

 

public class SpecialTwoDigitNumber {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter a two-digit number: ");

        int num = sc.nextInt();

 

        // Extract digits

        int tens = num / 10;

        int units = num % 10;

 

        // Calculate sum and product of digits

        int sum = tens + units;

        int product = tens * units;

 

        // Check special condition

        if (sum + product == num) {

            System.out.println("Special 2-digit number");

        } else {

            System.out.println("Not a special two-digit number");

        }

 

        sc.close();

    }

}

Output

Output 1 :

SAMPLE INPUT : Enter a two-digit number: 59 SAMPLE OUTPUT : Special 2-digit number

Output 2 :

SAMPLE INPUT : Enter a two-digit number: 23 SAMPLE OUTPUT : Not a special two-digit number

Explanation

1. Input

  • The program accepts a two-digit number from the user.

2. Digit Extraction

         int tens = num / 10;

         int units = num % 10;

  • Tens digit is obtained using division.
  • Units digit is obtained using modulus operator.

3. Sum & Product Calculation

          int sum = tens + units;

          int product = tens * units;

4. Special Number Check

         if (sum + product == num)

  • If (sum of digits + product of digits) equals the original number, it is a special two-digit number.