Java program to check Special 2 Digit Number

 


Introduction


This program helps to check whether it is special two Digit number or not. Special Two Digit Number are number for which sum of it’s  sum of it’s digits and product of it’s digits is equal to the original number.

OR we can say “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.” as per Question.

Example:

Suppose we have two Digit Number as 59

Then it is having first digit as 5 and second Digit as 9.

Please follow steps to check Special two Digit Number –

  • Calculate the sum of two digit and that will be 5+9 = 14
  • We have to calculate product of two digit and that will be 5*9 = 45
  • Calculate the sum of sum and product as calculate in above steps and that will be 14+45 = 59
  • And total calculated sum is equal to original number so we can say it is Special Two Digit Number.

Other Examples are: 19,29,69,79,….

 


Flowchart


Please find sample flowchart diagram for this program as given below –

 

 


Program(Code)


package com.kw.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This program was question on ICSE exam. 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.
 *
 * @author dsahu1
 *
 */
public class KWSumOfTwoDigitNumberAndProductEqualToOriginalNum {

    public static void main(String[] args) throws NumberFormatException,
            IOException {
        // Instantiate BufferedReader Object to get Input Number
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a 2 digit number : ");
        int num = Integer.parseInt(br.readLine());

        int firstDigit;
        int lastDigit;
        int sum;
        int product;
        // Validate number between 10 to 99
        if (num < 10 || num > 99) {
            System.out
                    .println("Please enter correct 2 Digit Number between 10 to 99!");
        }

        else {
            // Retrieve first Digit
            firstDigit = num / 10;
            // Retrieve last Digit
            lastDigit = num % 10;
            // Calculate sum of the Digit
            sum = firstDigit + lastDigit;
            // Calculate product of the Digit
            product = firstDigit * lastDigit;
            // Check Special Two Digit Number
            if ((sum + product) == num) {
                System.out.println("Result : The number " + num
                        + " is a Special Two-Digit Number.");
            } else {
                System.out
                        .println("Result : The number is Not a Special Two-Digit Number.");
            }
        }

    }
}

 

 


Output


Scenario 1:

Enter a 2 digit number : 29
Result : The number 29 is a Special Two-Digit Number.

—————————

Enter a 2 digit number : 99
Result : The number 99 is a Special Two-Digit Number.

—————————

Scenario 2:

Enter a 2 digit number : 18
Result : The number is Not a Special Two-Digit Number.

—————————

Scenario 3:

Enter a 2 digit number : 9
Please enter correct 2 Digit Number between 10 to 99!


Video Explanation


This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.

 

Please Find Video below –

 

2 thoughts on “Java program to check Special 2 Digit Number”

  1. Pretty portion of content. I just stumbled upon your web site and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I will be subscribing in your augment or even I fulfillment you access constantly quickly. dddcaedgcaakdkad

Leave a Reply

Your email address will not be published. Required fields are marked *