Check Whether a Number is a Tech Number in Java

Topic

A Tech Number is a number with an even number of digits. When it is split into two equal halves and the sum of those halves is squared, it should be equal to the original number.

For example, 2025 is a Tech Number because:

  • It has 4 digits → even.
  • Split into two halves: 20 and 25
  • Sum = 20 + 25 = 45
  • 452 = 2025 → which is the original number.

This program involves working with String operations, number manipulation, and mathematical calculations in Java.

Examples

Input: 2025
Output: Tech Number
Explanation: 20 + 25 = 45, 45 * 45 = 2025

Input: 3025
Output: Tech Number
Explanation: 30 + 25 = 55, 55 * 55 = 3025

Input: 1314
Output: Not a Tech Number
Explanation: 13 + 14 = 27, 27 * 27 = 729 ≠ 1314

Input: 123
Output: Not a Tech Number
Explanation: Odd number of digits

Interviewer Expectations

The interviewer wants to assess your understanding of:

  • Basic number operations and splitting logic
  • Use of strings to manipulate digits
  • Handling edge cases like odd-digit numbers
  • Mathematical reasoning and control flow

Approach

Follow these steps:

  1. Convert the number to a string to check the number of digits.
  2. If the number has an odd number of digits, it cannot be a tech number.
  3. Split the number into two equal halves.
  4. Convert both halves to integers and add them.
  5. Square the sum and compare it to the original number.

Dry Run

Input: 3025

  • Number of digits: 4 → even
  • Split: 30 and 25
  • Sum = 30 + 25 = 55
  • 55 * 55 = 3025 → matches original

Java Program

public class TechNumberCheck {
  public static void main(String[] args) {
    int number = 3025;
    String str = String.valueOf(number);

    if (str.length() % 2 != 0) {
      System.out.println("Not a Tech Number");
      return;
    }

    int mid = str.length() / 2;
    int firstHalf = Integer.parseInt(str.substring(0, mid));
    int secondHalf = Integer.parseInt(str.substring(mid));

    int sum = firstHalf + secondHalf;
    int square = sum * sum;

    if (square == number) {
      System.out.println("Tech Number");
    } else {
      System.out.println("Not a Tech Number");
    }
  }
}
Tech Number

Possible Followup Questions with Answers

Q1. What if the number has an odd number of digits?

By definition, a Tech Number must have an even number of digits. If the number has an odd number of digits, it is immediately not a Tech Number.

Q2. Can this logic be applied to very large numbers?

Yes, but you may need to use BigInteger if the number exceeds the range of int or long.

Q3. How to make the program accept user input?

Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();

Q4. Can we solve this without converting to a string?

Yes, by counting digits manually and using arithmetic operations to split the number. But string-based splitting is simpler for beginners.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...