How To Check Whether a Number is Tech Number or Not in Java?


A number is said to be a tech number, when an even digit number is divided into exactly two parts and the square value of the sum of those two numbers is equal to the original number.

For more clarification, we take a number which has even digits. After that, divide it into two parts and add two parts. After getting the sum value, we calculate the square of it. Now we compare both the calculated square value and the original/input number. If both are same then we can say that the input number is a tech number otherwise not.

Some examples of tech numbers are: 2025, 3025, 9801, ... etc.

In this article, we will see how to check if a number is a Tech number by using Java programming language.

To show you some instances

Instance-1

Input number is 2025.

Let’s check it by using the logic of Tech number.

Number of digits available in 2025 is 4, which is even.

By dividing the number into two parts = 20 and 25.

Add 20 and 25 = 20 + 25 = 45

Find the square of the 45 = (45)^2 = 2025

As we notice here both the calculated square value and original number are the same.

Hence, 2025 is a tech number.

Instance-2

Input number is 3025.

Let’s check it by using the logic of Tech number.

Number of digits available in 3025 is 4, which is even.

By dividing the number into two parts = 30 and 25.

Add 20 and 25 = 30 + 25 = 55

Find the square of the 55 = (55)^2 = 3025

As we notice here both the calculated square value and original number are the same.

Hence, 3025 is a tech number.

Instance-3

Input number is 4020.

Let’s check it by using the logic of Tech number.

Number of digits available in 4020 is 4, which is even.

By dividing the number into two parts = 40 and 20.

Add 40 and 20 = 40 + 20 = 60

Find the square of the 60 = (60)^2 = 3600

As we notice here both the calculated square value and original number are not the same.

Hence, 4020 is not a tech number.

Algorithm

Step-1 - Get the input number by static input method.

Step-2 - Calculate the number of digits in the original number.

Step-3 - If the number of digits is even then divide it into two parts and store those two numbers in two different variables.

Step-4 - Then find the sum of those two numbers and after that calculate the square of that sum value.

Step-5 - If the both the calculated square value and the input number, then the input number is called as tech number else not.

Syntax

To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method -

double power = Math.pow (inputValue,2)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare a variable and initialize it with an even digit positive number and then by using the algorithm we can check whether the number is a Tech number or not.

Example

public class Main {
   public static void main(String[] args) {
      
      //declaring the variables
      int inputNumber, temp, firstNumber, secondNumber, count = 0,res = 0;
      
      //Declare and initialize the original input number
      inputNumber = 3025;
      
      //store the original number into a temporary variable
      temp = inputNumber;

      //find the number of digits available in input number
      while (temp > 0) {
         count++;
         temp = temp / 10;
      }

      
      //check whether the number of digits is ever or not
      
      //if even number of digits are present then proceed to check Tech number
      if (count % 2 == 0) {
         temp = inputNumber;
         
         //find the first part and store it into a variable
         firstNumber = temp % (int) Math.pow(10, count / 2);
         
         //find the second part and store it into a variable
         secondNumber = temp / (int) Math.pow(10, count / 2);
         
         //calculate the square of sum of those two parts of input number
         res = (int) Math.pow((firstNumber + secondNumber), 2);
         if (inputNumber == res) {
            
            //if the original number is equal to calculated value
            System.out.println(inputNumber+" is a Tech Number.");
         }
         else {
            
            //if the original number is not equal to calculated value
            System.out.println(inputNumber+" is not a Tech Number.");
         }
      }
      else {
         
         //if the input number has odd number of digits
         System.out.println(inputNumber+" is not a Tech Number.");
      }
   }
}

Output

3025 is a Tech Number.

Approach-2: By User Defined Method

In this approach, we declare a variable and initialize it with a positive number and call a user defined method by passing this number as parameter, then by using the algorithm we can check whether the number is a Tech number or not.

Example

public class Main {
   
   //main method
   public static void main(String[] args) {
      
      //delare a variable and initialize a value to it
      int inputNumber= 3025;
      
      //call the user defined method to check the tech number
      if (checkTech(inputNumber)) {
         System.out.println(inputNumber+" is a Tech Number.");
      }
      else {
         System.out.println(inputNumber+" is not a Tech Number.");
      }
   }

   //user defined method for checking of Tech Number
   public static boolean checkTech(int n) {
      
      //declaring the variables
      int temp, firstNumber, secondNumber, count = 0,res = 0;
      
      //store the original number into a temporary variable
      temp = n;
      
      //loop to find the number of digits available in input number
      while (temp > 0) {
         count++;
         temp = temp / 10;
      }
      
      //check whether the number of digits is ever or not
      if (count % 2 == 0) {
         temp = n;
         
         //find the first part and store it into a variable
         firstNumber = temp % (int) Math.pow(10, count / 2);
         
         //find the second part and store it into a variable
         secondNumber = temp / (int) Math.pow(10, count / 2);
         
         //calculate the square of sum of those two parts of input number
         res = (int) Math.pow((firstNumber + secondNumber), 2);
         if (n == res) {
            
            //if the original number is equal to calculated value
            return true;
         }
         else {
            
            //if the original number is not equal to calculated value
            return false;
         }
      }
      else {
         
         //if the input number has odd number of digits
         return false;
      }
   }
}

Output

3025 is a Tech Number.

In this article, we explored how to check a number whether it is a Tech number or not in Java by using different approaches.

Updated on: 09-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements