Tech Number in Java

In this section, we will learn what is tech number and how can we find tech numbers through a Java program.

Tech Number

A number is called a tech number if the given number has an even number of digits and the number can be divided exactly into two parts from the middle. After equally dividing the number, sum up the numbers and find the square of the sum. If we get the number itself as square, the given number is a tech number, else, not a tech number. For example, 3025 is a tech number.

Tech Number Example

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

Tech Number in Java

Steps to Find Tech number

The base condition to check the tech number is that the number must contain an even number of digits. If so, we will move to the next step, else will not execute the code further. Let’s see the steps.

  • Read or initialize a number (num).
  • Find the number of digits of the given number (num).
  • If the number of digits is not even, the number (num) is not even.
  • Else, split the given number into two parts (num1 and num2), equally. Note that each part must contain an equal number of digits.
  • Sum up the numbers (num1+num2) and store the result in a variable
  • Find the square of the variable sum and store it in a variable square.
  • Compare the num with the square of the sum if they are equal print Tech Number, else print Not a Tech Number.

Tech Number Java Program

Let’s create a Java program and check whether the number is tech or not.

TechNumberExample1.java

  1. import java.util.Scanner;  
  2. public class TechNumberExample2  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0;  
  7. Scanner sc = new Scanner(System.in);  
  8. System.out.print(“Enter a number to check: “);  
  9. //reads an integer from the user  
  10. n = sc.nextInt();  
  11. //assign the value of n into num  
  12. num = n;  
  13. //the loop executes until the condition returns false  
  14. while (num > 0)  
  15. {  
  16. //incerements the variable digits by 1      
  17. digits++;  
  18. //removes the last digit of the given number  
  19. num = num / 10;  
  20. }  
  21. //check if the given number has an even number of digits or not  
  22. if (digits % 2 == 0)  
  23. {  
  24. num = n;  
  25. //determines the first half of the given number  
  26. firstHalf = num % (int) Math.pow(10, digits / 2);  
  27. //determines the second half of the given number  
  28. secondHalf = num / (int) Math.pow(10, digits / 2);  
  29. //calculate the square of both the numbers  
  30. squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf);  
  31. //compares the square of the sum with the given number  
  32. if (n == squareOfSum)  
  33. {  
  34. System.out.println(n+” is a tech number.”);  
  35. }  
  36. else  
  37. {  
  38. System.out.println(n+” is not a tech number.”);  
  39. }  
  40. }  
  41. else  
  42. {  
  43. System.out.println(n+ ” is not a tech number.”);  
  44. }  
  45. }  
  46. }  

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.

Let’s find all the tech numbers between a specified range.

TechNumberExample2.java

  1. public class TechNumberExample2  
  2. {  
  3. public static void main(String args[])   
  4. {  
  5. System.out.println(“Tech numbers between 1 to 1000000 are: “);      
  6. for (int i = 1; i <= 1000000; i++)   
  7. {  
  8. //determines the last digit of the given number  
  9. int p1 = i % 100;  
  10. //removes the last digit of the number  
  11. int p2 = i / 100;  
  12. //add the two parts i.e. p1 and p2  
  13. int sum = p1 + p2;  
  14. //square the sum that we get from the above statement and compare the square with the given number  
  15. if (i == (sum * sum))  
  16. //prints the tech number  
  17. System.out.println(i);  
  18. }  
  19. }  
  20. }  

Output:

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

Let’s see another logic for the same.

TechNumberExample3.java

  1. import java.util.*;  
  2. public class TechNumberExample3  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int count=0;      
  7. Scanner sc = new Scanner( System.in );  
  8. System.out.print(“Enter the number to check: “);  
  9. //reads an integer from the user  
  10. int n=sc.nextInt();  
  11. //loop counts the number of digits of the given number  
  12. while(n>0)  
  13. {  
  14. count++;  
  15. n=n/10;  
  16. }  
  17. //check if the given number has even number of digits or not  
  18. if(count%2==0)  
  19. System.out.println(“The given number is a tech number.”);  
  20. else  
  21. System.out.print(“The given number is not a tech number.”);  
  22. }  
  23. }  

Output:

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

Leave a Comment