Skip to main content

Tech Number Program

Check whether a given number is a "Tech Number" or not. A Tech Number is a number that satisfies a specific condition: the sum of its first half and second half digits, when squared, results in the original number itself. The program takes an integer as input and checks whether it is a Tech Number or not.

Explanation

Let's understand the concept of a Tech Number with the help of some examples:

  1. Example: Number 81

    • First half: 8, Second half: 1
    • Sum of first and second half: 8 + 1 = 9
    • Square of the sum: 9 * 9 = 81
    • Result: The number is a Tech Number.
  2. Example: Number 3025

    • First half: 30, Second half: 25
    • Sum of first and second half: 30 + 25 = 55
    • Square of the sum: 55 * 55 = 3025
    • Result: The number is a Tech Number.
  3. Example: Number 91

    • First half: 9, Second half: 1
    • Sum of first and second half: 9 + 1 = 10
    • Square of the sum: 10 * 10 = 100
    • Result: The number is not a Tech Number.
  4. Example: Number 9801

    • First half: 98, Second half: 01
    • Sum of first and second half: 98 + 1 = 99
    • Square of the sum: 99 * 99 = 9801
    • Result: The number is a Tech Number.

Standard Pseudocode

The pseudocode for the Tech Number Program can be represented as follows:

function isTechNo(number):
    length = 0
    temp = number
    lhs = 0
    rhs = 0
    result = 0

    // Count the length of the number of digits
    while temp != 0:
        temp = temp / 10
        length = length + 1

    // Check if the number of digits is even
    if length % 2 == 0:
        // Get the first half part
        lhs = number / (10 ^ (length / 2))
        // Get the second half part
        rhs = number % (10 ^ (length / 2))
        
        // Check if the number is a Tech Number
        if (lhs + rhs) * (lhs + rhs) == number:
            result = 1

    // Print the result
    if result == 1:
        print(number, " is a tech number")
    else:
        print(number, " is not a tech number")

Algorithm

  1. Start the isTechNo function with an integer parameter number.
  2. Initialize variables length, temp, lhs, rhs, and result to zero.
  3. Use a loop to count the number of digits in the input number and store it in length.
  4. Check if the length is even. If not, the number cannot be a Tech Number, so exit the function and print the result accordingly.
  5. If the length is even, calculate the first half lhs and the second half rhs of the number.
  6. Check if the sum of lhs and rhs, when squared, is equal to the original number. If true, set result to 1, indicating that the number is a Tech Number.
  7. Print the result based on the value of result.

Resultant Output Explanation: For each test case provided in the main function, the isTechNo function is called to determine if the given number is a Tech Number or not. The output of the program is as follows:

  1. isTechNo(81);: The number 81 is a Tech Number because 8 + 1 = 9, and 9 * 9 = 81. The program correctly outputs that 81 is a tech number.

  2. isTechNo(3025);: The number 3025 is a Tech Number because 30 + 25 = 55, and 55 * 55 = 3025. The program correctly outputs that 3025 is a tech number.

  3. isTechNo(91);: The number 91 is not a Tech Number because 9 + 1 = 10, and 10 * 10 = 100, not equal to the original number 91. The program correctly outputs that 91 is not a tech number.

  4. isTechNo(9801);: The number 9801 is a Tech Number because 98 + 1 = 99, and 99 * 99 = 9801. The program correctly outputs that 9801 is a tech number.

Code Solution

Here given code implementation process.

// C program
// Tech Number Program 
#include <stdio.h>
#include <math.h>
// Determine that given number is tech number or not
void isTechNo(int number)
{
	// Define some auxiliary variable
	int length = 0;
	int temp = number;
	int lhs = 0;
	int rhs = 0;
	int result = 0;
	// Count the length of number of digits
	while (temp != 0)
	{
		temp = temp / 10;
		length++;
	}
	if ((length % 2) == 0)
	{
		// Get first half part
		lhs = number / ((int) pow(10, length / 2));
		// Get second half part
		rhs = number % ((int) pow(10, length / 2));
      
		if (((lhs + rhs) * (lhs + rhs)) == number)
		{
			result = 1;
		}
	}
	if (result == 1)
	{
		// When number is tech number
		printf(" %d is tech number \n", number);
	}
	else
	{
		printf(" %d is not tech number \n", number);
	}
}
int main()
{
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	isTechNo(9801);
 
	return 0;
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
/*
  Java Program for
  Tech Number  
*/
class TechNo
{
	// Determine that given number is tech number or not
	public void isTechNo(int number)
	{
		// Define some auxiliary variable
		int length = 0;
		int temp = number;
		int lhs = 0;
		int rhs = 0;
		int result = 0;
		// Count the length of number of digits
		while (temp != 0)
		{
			temp = temp / 10;
			length++;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = number / ((int) Math.pow(10, length / 2));
			// Get second half part
			rhs = number % ((int) Math.pow(10, length / 2));
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			System.out.println(" " + number + " is tech number");
		}
		else
		{
			System.out.println(" " + number + " is not tech number");
		}
	}
	public static void main(String[] args)
	{
		TechNo task = new TechNo();
		// Test Case
		// number 81
		// 8+1 = 9
		// 9*9 = 81
		// Output : Yes
		task.isTechNo(81);
		// number 3025
		// 30 + 25 = 55
		// 55*55 = 3025
		// Output : Yes
		task.isTechNo(3025);
		// number 91
		// 9 + 1 = 10
		// 10*10 = 100
		// Output : No
		task.isTechNo(91);
		// number 9801
		// 98 + 1 = 99
		// 99*99 = 9801
		// Output : Yes
		task.isTechNo(9801);
	}
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
/*
  C++ Program for
  Tech Number  
*/
class TechNo
{
	public:
		// Determine that given number is tech number or not
		void isTechNo(int number)
		{
			// Define some auxiliary variable
			int length = 0;
			int temp = number;
			int lhs = 0;
			int rhs = 0;
			int result = 0;
			// Count the length of number of digits
			while (temp != 0)
			{
				temp = temp / 10;
				length++;
			}
			if ((length % 2) == 0)
			{
				// Get first half part
				lhs = number / ((int) pow(10, length / 2));
				// Get second half part
				rhs = number % ((int) pow(10, length / 2));
				if (((lhs + rhs) *(lhs + rhs)) == number)
				{
					result = 1;
				}
			}
			if (result == 1)
			{
				// When number is tech number
				cout << " " << number << " is tech number" << endl;
			}
			else
			{
				cout << " " << number << " is not tech number" << endl;
			}
		}
};
int main()
{
	TechNo *task = new TechNo();
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	task->isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	task->isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	task->isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	task->isTechNo(9801);
	return 0;
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
// Include namespace system
using System;
/*
  Csharp Program for
  Tech Number  
*/
public class TechNo
{
	// Determine that given number is tech number or not
	public void isTechNo(int number)
	{
		// Define some auxiliary variable
		int length = 0;
		int temp = number;
		int lhs = 0;
		int rhs = 0;
		int result = 0;
		// Count the length of number of digits
		while (temp != 0)
		{
			temp = temp / 10;
			length++;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = number / ((int) Math.Pow(10, length / 2));
			// Get second half part
			rhs = number % ((int) Math.Pow(10, length / 2));
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			Console.WriteLine(" " + number + " is tech number");
		}
		else
		{
			Console.WriteLine(" " + number + " is not tech number");
		}
	}
	public static void Main(String[] args)
	{
		TechNo task = new TechNo();
		// Test Case
		// number 81
		// 8+1 = 9
		// 9*9 = 81
		// Output : Yes
		task.isTechNo(81);
		// number 3025
		// 30 + 25 = 55
		// 55*55 = 3025
		// Output : Yes
		task.isTechNo(3025);
		// number 91
		// 9 + 1 = 10
		// 10*10 = 100
		// Output : No
		task.isTechNo(91);
		// number 9801
		// 98 + 1 = 99
		// 99*99 = 9801
		// Output : Yes
		task.isTechNo(9801);
	}
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
<?php
/*
  Php Program for
  Tech Number  
*/
class TechNo
{
	// Determine that given number is tech number or not
	public	function isTechNo($number)
	{
		// Define some auxiliary variable
		$length = 0;
		$temp = $number;
		$lhs = 0;
		$rhs = 0;
		$result = 0;
		// Count the length of number of digits
		while ($temp != 0)
		{
			$temp = (int)($temp / 10);
			$length++;
		}
		if (($length % 2) == 0)
		{
			// Get first half part
			$lhs = (int)($number / ((int) pow(10, (int)($length / 2))));
			// Get second half part
			$rhs = $number % ((int) pow(10, (int)($length / 2)));
			if ((($lhs + $rhs) * ($lhs + $rhs)) == $number)
			{
				$result = 1;
			}
		}
		if ($result == 1)
		{
			// When number is tech number
			echo " ".$number.
			" is tech number".
			"\n";
		}
		else
		{
			echo " ".$number.
			" is not tech number".
			"\n";
		}
	}
}

function main()
{
	$task = new TechNo();
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	$task->isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	$task->isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	$task->isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	$task->isTechNo(9801);
}
main();

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
/*
  Node JS Program for
  Tech Number  
*/
class TechNo
{
	// Determine that given number is tech number or not
	isTechNo(number)
	{
		// Define some auxiliary variable
		var length = 0;
		var temp = number;
		var lhs = 0;
		var rhs = 0;
		var result = 0;
		// Count the length of number of digits
		while (temp != 0)
		{
			temp = parseInt(temp / 10);
			length++;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = parseInt(number / ((int) Math.pow(10, parseInt(length / 2))));
			// Get second half part
			rhs = number % ((int) Math.pow(10, parseInt(length / 2)));
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			console.log(" " + number + " is tech number");
		}
		else
		{
			console.log(" " + number + " is not tech number");
		}
	}
}

function main()
{
	var task = new TechNo();
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	task.isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	task.isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	task.isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	task.isTechNo(9801);
}
main();

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
import math
#  Python 3 Program for
#  Tech Number  
class TechNo :
	#  Determine that given number is tech number or not
	def isTechNo(self, number) :
		length = 0
		temp = number
		lhs = 0
		rhs = 0
		result = 0
		#  Count the length of number of digits
		while (temp != 0) :
			temp = int(temp / 10)
			length += 1
		
		if ((length % 2) == 0) :
			#  Get first half part
			lhs = int(number / ((10 ** int(length / 2))))
			#  Get second half part
			rhs = number % ((10 ** int(length / 2)))
			if (((lhs + rhs) * (lhs + rhs)) == number) :
				result = 1
			
		
		if (result == 1) :
			#  When number is tech number
			print(" ", number ," is tech number")
		else :
			print(" ", number ," is not tech number")
		
	

def main() :
	task = TechNo()
	#  Test Case
	#  number 81
	#  8+1 = 9
	#  9*9 = 81
	#  Output : Yes
	task.isTechNo(81)
	#  number 3025
	#  30 + 25 = 55
	#  55*55 = 3025
	#  Output : Yes
	task.isTechNo(3025)
	#  number 91
	#  9 + 1 = 10
	#  10*10 = 100
	#  Output : No
	task.isTechNo(91)
	#  number 9801
	#  98 + 1 = 99
	#  99*99 = 9801
	#  Output : Yes
	task.isTechNo(9801)

if __name__ == "__main__": main()

input

  81  is tech number
  3025  is tech number
  91  is not tech number
  9801  is tech number
#  Ruby Program for
#  Tech Number  
class TechNo 
	#  Determine that given number is tech number or not
	def isTechNo(number) 
		#  Define some auxiliary variable
		length = 0
		temp = number
		lhs = 0
		rhs = 0
		result = 0
		#  Count the length of number of digits
		while (temp != 0) 
			temp = temp / 10
			length += 1
		end

		if ((length % 2) == 0) 
			#  Get first half part
			lhs = number / ( 10 ** (length / 2))
			#  Get second half part
			rhs = number % ( 10 ** (length / 2))
			if (((lhs + rhs) * (lhs + rhs)) == number) 
				result = 1
			end

		end

		if (result == 1) 
			#  When number is tech number
			print(" ", number ," is tech number", "\n")
		else 
			print(" ", number ," is not tech number", "\n")
		end

	end

end

def main() 
	task = TechNo.new()
	#  Test Case
	#  number 81
	#  8+1 = 9
	#  9*9 = 81
	#  Output : Yes
	task.isTechNo(81)
	#  number 3025
	#  30 + 25 = 55
	#  55*55 = 3025
	#  Output : Yes
	task.isTechNo(3025)
	#  number 91
	#  9 + 1 = 10
	#  10*10 = 100
	#  Output : No
	task.isTechNo(91)
	#  number 9801
	#  98 + 1 = 99
	#  99*99 = 9801
	#  Output : Yes
	task.isTechNo(9801)
end

main()

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
/*
  Scala Program for
  Tech Number  
*/
class TechNo()
{
	// Determine that given number is tech number or not
	def isTechNo(number: Int): Unit = {
		// Define some auxiliary variable
		var length: Int = 0;
		var temp: Int = number;
		var lhs: Int = 0;
		var rhs: Int = 0;
		var result: Int = 0;
		// Count the length of number of digits
		while (temp != 0)
		{
			temp = (temp / 10).toInt;
			length += 1;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = (number / ( Math.pow(10, (length / 2).toInt))).toInt;
			// Get second half part
			rhs = number % ( Math.pow(10, (length / 2).toInt)).toInt;
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			println(" " + number + " is tech number");
		}
		else
		{
			println(" " + number + " is not tech number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TechNo = new TechNo();
		// Test Case
		// number 81
		// 8+1 = 9
		// 9*9 = 81
		// Output : Yes
		task.isTechNo(81);
		// number 3025
		// 30 + 25 = 55
		// 55*55 = 3025
		// Output : Yes
		task.isTechNo(3025);
		// number 91
		// 9 + 1 = 10
		// 10*10 = 100
		// Output : No
		task.isTechNo(91);
		// number 9801
		// 98 + 1 = 99
		// 99*99 = 9801
		// Output : Yes
		task.isTechNo(9801);
	}
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number
import Foundation;
/*
  Swift 4 Program for
  Tech Number  
*/
class TechNo
{
	// Determine that given number is tech number or not
	func isTechNo(_ number: Int)
	{
		// Define some auxiliary variable
		var length: Int = 0;
		var temp: Int = number;
		var lhs: Int = 0;
		var rhs: Int = 0;
		var result: Int = 0;
		// Count the length of number of digits
		while (temp  != 0)
		{
			temp = temp / 10;
			length += 1;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = number / (Int(pow(10.0, Double(length / 2))));
			// Get second half part
			rhs = number % (Int(pow(10.0, Double(length / 2))));
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			print(" ", number, " is tech number");
		}
		else
		{
			print(" ", number, " is not tech number");
		}
	}
}
func main()
{
	let task: TechNo = TechNo();
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	task.isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	task.isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	task.isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	task.isTechNo(9801);
}
main();

input

  81  is tech number
  3025  is tech number
  91  is not tech number
  9801  is tech number
/*
  Kotlin Program for
  Tech Number  
*/
class TechNo
{
	// Determine that given number is tech number or not
	fun isTechNo(number: Int): Unit
	{
		// Define some auxiliary variable
		var length: Int = 0;
		var temp: Int = number;
		var lhs: Int ;
		var rhs: Int ;
		var result: Int = 0;
		while (temp != 0)
		{
			temp = temp / 10;
			length += 1;
		}
		if ((length % 2) == 0)
		{
			// Get first half part
			lhs = number / (Math.pow(10.0, (length / 2).toDouble())).toInt();
			// Get second half part
			rhs = number % (Math.pow(10.0, (length / 2).toDouble())).toInt();
			if (((lhs + rhs) * (lhs + rhs)) == number)
			{
				result = 1;
			}
		}
		if (result == 1)
		{
			// When number is tech number
			println(" " + number + " is tech number");
		}
		else
		{
			println(" " + number + " is not tech number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TechNo = TechNo();
	// Test Case
	// number 81
	// 8+1 = 9
	// 9*9 = 81
	// Output : Yes
	task.isTechNo(81);
	// number 3025
	// 30 + 25 = 55
	// 55*55 = 3025
	// Output : Yes
	task.isTechNo(3025);
	// number 91
	// 9 + 1 = 10
	// 10*10 = 100
	// Output : No
	task.isTechNo(91);
	// number 9801
	// 98 + 1 = 99
	// 99*99 = 9801
	// Output : Yes
	task.isTechNo(9801);
}

input

 81 is tech number
 3025 is tech number
 91 is not tech number
 9801 is tech number

Time Complexity

The time complexity of the Tech Number Program is primarily determined by the length of the input number. Let's analyze the key operations:

  1. Counting the number of digits in the input number takes O(log N) time, where N is the value of the number.
  2. Calculating the first and second halves of the number takes O(log N) time as well.
  3. The sum and square operations are constant-time operations, so they do not significantly contribute to the overall complexity.

Hence, the overall time complexity of the Tech Number Program is O(log N), where N is the input number. This is considered an efficient algorithm since it operates in logarithmic time.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment