In the previous article, we have discussed Java Program to Check Sunny Number
In this article we are going to understand what Tech number is and how we can check whether a number is Tech number or not in Java with examples.
Program to Check Tech Number
Tech numbers are numbers having even number of digits and when the digits are divided into two halves, and the square of the sum of two halves is equal to the number itself.
Example :
2025: 20+25= 45, (45)2=2025 Tech number
10000: 100+000=100, (100)2=10000 Tech number
63: 6+3=9, (9)2 =81 Not a Tech number
In the above examples the numbers 2025 and 10000 are Tech numbers as the square of their halves add upto the numbers. However 63 is not a Tech number.
Let’s see different ways to check Tech number.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Approach :
- Enter/Declare a number and store it.
- We check whether the number has even digits, if not it can’t be a tech number.
- We break the number into two halves and add them. Then the sum is squared.
- If the squared number is equal to the entered number, then the number is said to be a Tech number.
Method-1: Java Program to Check Tech Number By Using Static Value
import java.util.Scanner; public class TechNumber { public static void main(String args[]) { //A number declared int num = 2025; //numberOfDig() method called to get the number of digits in the number int digits = numberOfDig(num); //Checks whether the number has even number of digts // and whether the square of the sum of its halves are equal to the number itself if((digits%2==0) && (Math.pow((sumofHalves(num,digits)),2)==num)) { System.out.println(num+" is a Tech Number"); } else { System.out.println(num+" is Not a Tech Number"); } } //Function that returns the sum of halves static int sumofHalves(int num,int digits) { int firstHalf = 0, secondHalf=0; firstHalf = num%(int)Math.pow(10,digits/2); secondHalf = num/(int)Math.pow(10,digits/2); return (firstHalf+secondHalf); } //method that returns the number of digits static int numberOfDig(int num) { int digits = 0; while (num > 0) { digits++; num = num / 10; } return digits; } }
Output: 2025 is a Tech Number
Method-2: Java Program to Check Tech Number By User Input Value
import java.util.Scanner; public class TechNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //numberOfDig() method called to get the number of digits in the number int digits = numberOfDig(num); //Checks whether the number has even number of digts // and whether the square of the sum of its halves are equal to the number itself if((digits%2==0) && (Math.pow((sumofHalves(num,digits)),2)==num)) { System.out.println(num+" is a Tech Number"); } else { System.out.println(num+" is Not a Tech Number"); } } //Function that returns the sum of halves static int sumofHalves(int num,int digits) { int firstHalf = 0, secondHalf=0; firstHalf = num%(int)Math.pow(10,digits/2); secondHalf = num/(int)Math.pow(10,digits/2); return (firstHalf+secondHalf); } //Function that returns the number of digits static int numberOfDig(int num) { int digits = 0; while (num > 0) { digits++; num = num / 10; } return digits; } }
Output: Case-1 Enter a number : 2025 2025 is a Tech Number Case-2 Enter a number : 202 202 is a Tech Number
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: