BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Tech Number Program in Java

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check whether a given number is Tech Number or not. We will also write a program to find all the tech numbers in a given range.

What is Tech Number?

A number is called Tech number, if it has an even number of digits, and when we split the number into two halves then the square of the sum of those halves is equal to the number itself.

For example, 2025 is a Tech number.

To verify whether 2025 is a Tech number, we have split the number in two halves (20 & 25) and then calculated the square of the sum of two halves. It resulted in the number itself, thus we can say that it’s a Tech number.

2025 = (20+25)^2
     = (45)^2
     = 2025

Steps that we will use in the Program to check for Tech Number

  1. Take the number from the user and store it into a variable using Scanner class.
  2. Check the number of digits in the input number, if the number of digits is not even, the input number is not a Tech number else continue with the next step.
  3. Split the number in two halves, num1 and num2.
  4. Find the sum of the squares of two halves (num1+num2)^2, if it is equal to the number itself, return “Tech Number” else return “Not a Tech Number”

Tech Number Program in Java

Here we are using the steps that we have mentioned above. We took the input number from the user and assigned it to number n and then we copied the value of n to a new variable num. This is because we are going to perform some operations on num and we want to retain the original number for comparison later.

In the next step we calculated the number of digits in the input number using while loop. If number of digits are even we continued to the next step else we returned “Not a Tech number”.

In the next step we split the number in two halves firstHalf and secondHalf. We then calculated the sum of squares of these two halves.

If the sum is equal to the number, the number is a Tech number, else not a Tech number.

import java.util.Scanner;
public class JavaExample
{
    public static void main(String args[])
    {
        int n, num, firstHalf, secondHalf, numberOfDigits=0, squareOfTwoHalves;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number: ");
        //Store the input number into variable num
        n = scan.nextInt();
        //copying the value of n into a new variable num
        num = n;

        //Here we are finding the number of digits in the input number
        while (num > 0)
        {
            numberOfDigits++;
            num = num / 10;
        }
        //If the number of digits are not even return "Not a Tech Number"
        if (numberOfDigits % 2 == 0)
        {
            num = n;
            //Splitting the number in two halves
            firstHalf = num % (int) Math.pow(10, numberOfDigits / 2);
            secondHalf = num / (int) Math.pow(10, numberOfDigits / 2);
            //calculate the sum of the square of two halves
            squareOfTwoHalves = (firstHalf + secondHalf) * (firstHalf + secondHalf);
            //compare the result with the number itself.
            if (n == squareOfTwoHalves)
            {
                System.out.println(n +" is a Tech number.");
            }
            else
            {
                System.out.println(n +" is not a Tech number.");
            }
        }
        else
        {
            System.out.println(n + " is not a Tech number.");
        }
    }
}

Output 1: When user enters number 2025

Enter the number: 2025
2025 is a Tech number.

Output 2: When user enters number 1237

Enter the number: 1237
1237 is not a Tech number.

Program to find all the Tech Numbers in a given range

Here we are writing a program to find all the tech numbers in a given range. The user enters the starting and ending range and program displays all the numbers between input ranges.

import java.util.Scanner;

public class JavaExample {

    public static void main(String[] args) {

        int startRange, endRange;

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter starting range: ");
        startRange = scan.nextInt();
        System.out.print("Enter ending range: ");
        endRange = scan.nextInt();
        scan.close();

        System.out.println("All tech numbers from "+
                startRange+" to "+ endRange+" are: ");

        /* We are looping through all the numbers in the
         * given range and checking whether the number is Tech
         * number or not, if Tech then we are displaying it.
         */
        for(int i=startRange; i<=endRange; i++) {
            if (isTechNumber(i))
                System.out.print(i + " ");
        }
    }
    //User defined function to check for Tech number
    private static boolean isTechNumber(int num) {

        int n = num;
        int count = 0;
        int firstHalf;
        int lastHalf;
        int squareOfTwoHalves;

        // Here we are calculating number of digits
        while(n != 0) {
            n /= 10;
            count++;
        }

        // If number of digits are not even return false
        if(count%2!=0) return false;

        // Splitting the number into two halves
        firstHalf = num / (int)Math.pow(10, count/2);
        lastHalf = num % (int)Math.pow(10, count/2);

        // calculate square of sum of two halves
        squareOfTwoHalves = (firstHalf + lastHalf)*(firstHalf + lastHalf);

        if(squareOfTwoHalves == num)
            return true;
        else
            return false;
    }
}

Output: When user enters 1 as starting range and 100 as ending range. Program displayed Tech numbers from 1 to 100.

Enter starting range: 1
Enter ending range: 100
All tech numbers from 1 to 100 are: 
81 

To learn java from scratch refer my Java tutorial.

Related Java Examples

  1. Java Program to break integer into digits
  2. Java String split() method with examples
  3. Java Program to check Sunny number
  4. Java Program to check Peterson Number
❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap