wap to print tech number in java.
Answers
public class TechNumber {
public static boolean isTechNumber(int number) {
String numberString = String.valueOf(number);
String leftHalfString = numberString.substring(0, numberString.length() / 2);
String rightHalfString = numberString.substring(numberString.length() / 2);
int leftHalf = Integer.valueOf(leftHalfString);
int rightHalf = Integer.valueOf(rightHalfString);
int squareOfSum = (int) Math.pow(leftHalf + rightHalf, 2);
if (number == squareOfSum) {
return true;
} else {
return false;
}
}
public static void printTechNumbers() {
for (int i = 1000; i <= 9999; i++) {
boolean techNumber = isTechNumber(i);
if (techNumber == true) {
System.out.println(i);
}
}
}
public static void main(String[] args) {
printTechNumbers();
}
}
Answer:
import java.util.Scanner;
class TechNumber
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int num,fPart,sPart,sumOfParts,tens,sq;
System.out.print("Enter a number : ");
num=in.nextInt();
int noDig=Integer.toString(num).length();
if(noDig%2!=0)
{
System.out.println(" There are "+noDig+" number of digits which is odd,hence,not tech number ");
System.exit(0);
}
tens = (int)Math.pow(10,noDig/2);
fPart = num/tens;
sPart = num%tens;
sumOfParts = fPart+sPart;
sq=sumOfParts*sumOfParts;
if(sq==num)
System.out.println(num+" is a tech number");
else
System.out.println(num+" is not a tech number");
}
}
Explanation: