Computer Science, asked by spardha85, 1 year ago

write a java program to check whether the given number is tech number or not ....

Answers

Answered by alankrit38677
6

Answer: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();

}

}

Explanation: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();

}

}

Answered by naadhinarayan
1

Answer:

import java.util.Scanner ;

public class bruh2

{

   public static void main()

   {

       Scanner sc = new Scanner(System.in) ;

       System.out.println("Enter a number") ;

       int Opt = sc.nextInt();

       

       int r1 = Opt ;

       int r2 = 0 ;

       int r3 = 0 ;

       int r4 = 0 ;

       

       r2 = Opt % 100 ;

       r4 = Opt / 100 ;

       

       r3 = r3 + (int)(Math.pow(r2 , 2) + 2 * r2 * r4 + Math.pow(r4 , 2)) ;

           //

       System.out.println(r3) ;

       

       if(r3 == r1)

       {

           System.out.println("The number is a tech number") ;

       }

       else

       {

           System.out.println("The number is not a tech number") ;

       }

   }

}

Explanation:

for example the number is 3025

(30 + 25) ^ 2 = 3025

Use algebraic identity - (a + b)^2 = a^2 + 2ab + b^2

I have used the splitting of the digit thing but with the base as 100

Hope u understand

Similar questions