Tech Number Program in Java

In this post we will develop a Java program to check the given number is a tech number or not? And we will also find all tech numbers in the given range.

A number which has an even number of digits, and when the number of digits split into two halves then the square of the sum of those halves is equal to the same number, is called Tech number.

Example of Tech number:-
2025
Number of digits = 4 (even)
Split 2025 into two halves, then
first half = 20
second half = 25
sum of the halves = 20+25 = 45
square of the sum of the halves = 45*45 = 2025

Hence 2025 is a tech number. Another examples of tech number are:- 81, 2025, 3025, 9801, 494209, 998001

Procedure

The procedure to develop a program to check number is a tech number of not,

1) Take a number
2) Find the number of digits in the given number
3) If the number of digits is not even then the number is not a tech number
4) Calculate the first half and second half of the number
5) Find the sum of both halves
6) calculate the square of the sum value
7) if the number is equal to the sum then it is a tech number else it is not a tech number.

Java program to check number is tech number or not

import java.util.Scanner;

public class TechNumber {

   private static boolean isTech(int number) {

      // declare variables
      int n = number; // temp variable to store number
      int count = 0; // store number of digits
      int firstHalf = 0; // first half of the number
      int lastHalf = 0; // last half of the number
      int sum = 0;

      // count number of digits
      while(n != 0) {
         n /= 10;
	 count++;
      }

      // if number of digits is not even
      // then number is not tech number
      if(count%2!=0) return false;

      // calculate halves
      firstHalf = number / (int)Math.pow(10, count/2);
      lastHalf = number % (int)Math.pow(10, count/2);

      // calculate sum of halves 
      sum = firstHalf + lastHalf;

      // check number is equal to
      // square of sum or not
      if(sum*sum == number) 
            return true;
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is tech number or not
      if(isTech(number))
      System.out.println(number+" is a"
		    + " tech number");
      else
      System.out.println(number+" is not a"
	      	  + " tech number");

      // close Scanner class object
      scan.close();
   }
}

Output for the different test-cases:-

Enter an integer number:: 2025
2025 is a tech number

Enter an integer number:: 2000
2000 is not a tech number

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java program to find all tech number in a given range

Using loops we can find all tech numbers in the given range. The below program demonstrates it.

import java.util.Scanner;

public class TechNumberInRange {

   private static boolean isTech(int number) {

      // declare variables
      int n = number; // temp variable to store number
      int count = 0; // store number of digits
      int firstHalf = 0; // first half of the number
      int lastHalf = 0; // last half of the number
      int sum = 0;

      // count number of digits
      while(n != 0) {
	 n /= 10;
	 count++;
      }

      // if number of digits is not even
      // then number is not tech number
      if(count%2!=0) return false;

      // calculate halves
      firstHalf = number / (int)Math.pow(10, count/2);
      lastHalf = number % (int)Math.pow(10, count/2);

      // calculate sum of halves 
      sum = firstHalf + lastHalf;

      // check number is equal to
      // square of sum or not
      if(sum*sum == number) 
		return true;
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int minRange = 0, maxRange = 0;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read inputs
      System.out.print("Enter min value of range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max value of range:: ");
      maxRange = scan.nextInt();

      // check number 
      System.out.println("The tech numbers from "+
		minRange+" to "+ maxRange+" are:: ");

      for(int i=minRange; i<=maxRange; i++) {
         if(isTech(i))
	 System.out.print(i+" ");
      }

      // close Scanner class object
      scan.close();
   }
}

Output for the different test-cases:-

Enter min value of range:: 1
Enter max value of range:: 100
The tech numbers from 1 to 100 are::
81

Enter min value of range:: 100
Enter max value of range:: 10000
The tech numbers from 100 to 10000 are::
2025 3025 9801

Enter min value of range:: 10000
Enter max value of range:: 1000000
The tech numbers from 10000 to 1000000 are::
494209 998001

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply