A tech number has an even number of digits. If the number is split in two halves, then the square of the sum of these halves is equal to the number itself. Write a program to generate and print all four-digit tech numbers. Example: consider the number 3025 Square of sum of the halves of 3025 = (30 + 25)^2 = (55)^2 = 3025, so 3025 is a tech number.
Topic: Generating tech numbers
Answer
class TechNumber { public static void main(String args[]) { System.out.println("Four digit tech numbers are :");
for(int i = 1000; i <= 9999; i++) { int first = i / 100; int second = i % 100; int sq = (first + second) * (first + second);
if(sq == i) System.out.print(i + " "); } } }
A generating question rather than a checking one — the loop runs over every four-digit number and prints those that satisfy the rule.
Splitting a four-digit number into halves is done with a single division and a single remainder: i / 100 gives the first two digits (3025 / 100 = 30) i % 100 gives the last two digits (3025 % 100 = 25) That pairing of / and % with the same divisor is the standard way to split a number, and it is worth memorising.
Then square the sum and compare it with the original. Use (a+b)*(a+b) rather than Math.pow(), which returns a double and would need a cast before the == comparison.
No input is needed, so no Scanner — the program simply searches the range 1000 to 9999.
The output is 2025, 3025 and 9801. Check 9801: 98 + 01 = 99, and 99^2 = 9801. The paper’s own example, 3025, appears among them.
i int - loop variable running over all four-digit numbers first int - first half of the number (i / 100) second int - second half of the number (i % 100) sq int - square of the sum of the two halves