A tech number has even number of digits. If the number is split in two equal halves, then the square of sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.
Answers
public class TechNumber {
static boolean isTech(int number) {
return Math.pow(number / 100, 2) + Math.pow(number % 100, 2) = = number;
}
public static void main(String[ ] args) {
for (int i = 1000; i < 10000; i++)
if (isTech(i))
System.out.println(i);
}
}
Write a program to generate and print all four digit tech number.
import java.util.*;
class TechNumber
{
static int CountDigits(int n)
{
int c=0;
while(n!=0)
{
c++;
n/=10;
}
return c;
}
static boolean isTech(int n, int digits)
{
if(digits%2!=0)
return false;
else
{
digits/=2;
int x=(int)(n/Math.pow(10, digits);
int y=(int)(n%Math.pow(10, digits);
int a=x+y;
return (a==n);
}
}
public static void main(String s[])
{
for(int i=1000;i<=10000;i++)
{
int n=i;
int d=CountDigits(n);
if(isTech(n, d))
System.out.print(n+" ");
} // end of for loop.
}// end of main method.
} // end of class.