Tech Number in Java

In this section, we will explore the concept of tech numbers and develop a Java program to identify them.

What is a Tech Number?

A tech number is defined as a number that has an even number of digits and can be divided exactly into two equal parts from the middle. Once the number is divided equally, the two parts are summed, and the square of the sum is calculated. If the result is equal to the original number, then it is considered a tech number. For instance, 3025 is an example of a tech number.

Tech Number Example

Let’s take an example and check whether 2025 and 1312 are tech numbers or not.

Steps to Find Tech Number

To determine if a number is a tech number, the base condition is that it must have an even number of digits. If this condition is not met, the code will not proceed further. Here are the steps involved:

  • Read or initialize the number (num).
  • Calculate the number of digits in the given number (num).
  • If the number of digits is not even, then the number (num) is not a tech number.
  • Otherwise, split the given number into two equal parts (num1 and num2), ensuring that each part has an equal number of digits.
  • Add the two numbers (num1 + num2) and store the result in a variable.
  • Calculate the square of the sum and store it in a variable called “square”.
  • Compare the original number (num) with the square of the sum. If they are equal, print “Tech Number”; otherwise, print “Not a Tech Number”.

Tech Number Java Program

TechNumberExample1.java

import java.util.Scanner;  
public class TechNumberExample  
{  
public static void main(String args[])  
{  
int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0;  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number to check: ");  
//reads an integer from the user  
n = sc.nextInt();  
//assign the value of n into num  
num = n;  
//the loop executes until the condition returns false  
while (num > 0)  
{  
//incerements the variable digits by 1      
digits++;  
//removes the last digit of the given number  
num = num / 10;  
}  
//check if the given number has an even number of digits or not  
if (digits % 2 == 0)  
{  
num = n;  
//determines the first half of the given number  
firstHalf = num % (int) Math.pow(10, digits / 2);  
//determines the second half of the given number  
secondHalf = num / (int) Math.pow(10, digits / 2);  
//calculate the square of both the numbers  
squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf);  
//compares the square of the sum with the given number  
if (n == squareOfSum)  
{  
System.out.println(n+" is a tech number.");  
}  
else  
{  
System.out.println(n+" is not a tech number.");  
}  
}  
else  
{  
System.out.println(n+ " is not a tech number.");  
}  
}  
}

Output 1:

Enter a number to check: 2025
2025 is a tech Number.

Output 2:

Enter a number to check: 9181
9181 is not a tech number.

TechNumberExample2.java

public class TechNumberExample1  
{  
public static void main(String args[])   
{  
System.out.println("Tech numbers between 1 to 1000000 are: ");      
for (int i = 1; i <= 1000000; i++)   
{  
//determines the last digit of the given number  
int p1 = i % 100;  
//removes the last digit of the number  
int p2 = i / 100;  
//add the two parts i.e. p1 and p2  
int sum = p1 + p2;  
//square the sum that we get from the above statement and compare the square with the given number  
if (i == (sum * sum))  
//prints the tech number  
System.out.println(i);  
}  
}  
}

Output:

Tech numbers between 1 to 1000000 are: 
1
2025
3025
9801
10000

TechNumberExample3.java

import java.util.*;  
public class TechNumberExample2  
{  
public static void main(String args[])  
{  
int count=0;      
Scanner sc = new Scanner( System.in );  
System.out.print("Enter the number to check: ");  
//reads an integer from the user  
int n=sc.nextInt();  
//loop counts the number of digits of the given number  
while(n>0)  
{  
count++;  
n=n/10;  
}  
//check if the given number has even number of digits or not  
if(count%2==0)  
System.out.println("The given number is a tech number.");  
else  
System.out.print("The given number is not a tech number.");  
}  
}

Output:

Enter the number to check: 3025
The given number is a tech number.

We hope that the provided details about the Tech Number in Java are helpful to you. For more updates, please follow tutorials.freshersnow.com.