- 1Java Interview Program Title
- 2Java Interview Program - Check Whether a Number is Armstrong
- 3Java Interview Program - Check Whether a Number is an Ugly Number
- 4Java Interview Program - Check Whether a Number is a Sunny Number in Java
- 5Java Interview Program - Check Whether a Number is a Harshad (Niven) Number
- 6Check whether a number is a Hamming number in Java
- 7Check Whether a Number is a Spy Number in Java
- 8Check whether a Number is a Smith Number in Java
- 9Check Whether a Number is a Disarium Number in Java
- 10Check Whether a Number is a Lychrel Number Candidate in Java
- 11Check Whether a Number is a Tech Number in Java
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:
- Convert the number to a string to check the number of digits.
- If the number has an odd number of digits, it cannot be a tech number.
- Split the number into two equal halves.
- Convert both halves to integers and add them.
- 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.
Next Topic ⮕Convert Decimal to Binary in Java
Comments
Loading comments...