In this tutorial you will learn about the Program to check Tech Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Tech Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Tech Number.
A number is called a Tech number if an even number digit split into two halves(1/2) then square of the sum of those halves(1/2) is equal to the original number.
Example :->3025 So split 3025 into two halves
- 30.
- 25.
then = (30) +( 25)
= 55 - =55*55.
- =3025
So => 3025 is a tech number.
Program to check Tech Number in Java
In this program we would create a java program to find given number is a Tech number or not .First will take the input from the user and check whether it is Tech number or not. If it’s not then the program will show the output as it’s not a tech number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// java program to find given number is a tech number. import java.util.Scanner; public class Tech_num { private static boolean Tech(int num) { // declare variables int no = num; // temprory store number int ct = 0; // store digits of a number int fH = 0; // first half of the number int lH = 0; // last half of the number int sum = 0; // counting digits while(no != 0) { no /= 10; ct++; } // if number of digits is not even // then number is not tech number if(ct%2!=0) return false; // calculate halves fH = num / (int)Math.pow(10, ct/2); lH = num % (int)Math.pow(10, ct/2); // calculate sum of halves sum = fH + lH; // check number is equal to // square of sum or not if(sum*sum == num) return true; return false; } public static void main(String[] args) { int number = 0; // declare variable and take value from user at run time. Scanner scan = new Scanner(System.in); System.out.print("Enter a four digit number "); number = scan.nextInt(); // check the number is tech number or not if(Tech(number)) System.out.println(number+" is a" + " tech number"); else System.out.println(number+" is not a" + " tech number"); } } |
Output
Tech Number
Not a Tech Number.
In the above program, we have first declared and initialized a set variables required in the program.
- num,numberand no = it will hold entered number.
- fH and Lh = it will hold first half and last half of a number.
- ct= finding digit of a number.
- sum= sum of digit.
After that we take a number from user and find given number is a Tech number or not
after taking values we will pass number to function Tech() where we check multiple conditions as shown in image below.
check entered number is a four digit number or not. if given number is a four digit nmber then we break number into two half first two in hF variable and second two in lF variable
and these two in variable sum where we find square of their addition.
and then compare square of sum equals to the given number
then number is a Tech number otherwise it is not a tech number.