Check whether a number is Tech Number or not
Last Updated :
18 Jul, 2024
Given a number, the task is to check if it is a Tech number or not.
Note: A Tech number is a number that has an even number of digits and if the number is split into two equal halves(From the middle), then the square of the sum of these halves is equal to the number itself.
Examples:
Input: n = 81
Output: True
Explanation: 8 + 1 = 9 and 92 = 81
Input: n = 2025
Output: True
Explanation: 20 + 25 = 45 and 452 = 2025
Input: n = 1521
Output: False
Explanation: 15 + 21 = 36 and 362 = 1296 which is not equal to 1521
Approach: Let us understand how to check whether a number is a tech number or not.
- Check if the number of digits is even. If it's not even, the number cannot be a Tech number.
- Split the number into two equal halves and fin
- Compare the squared sum with the original number. If they are equal, then the number is a tech number; otherwise, it's not.
Below is the implementation of the above approach:
C++14
// C++ program to check if a
// number is Tech number or not
#include <bits/stdc++.h>
using namespace std;
// Function to count the number
// of digits in a number
int countDigits(int n)
{
int count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
// Returns true if n is a tech number, else false
bool istech(int n)
{
// Count the number of digits
int count = countDigits(n);
// Return false if digits are odd
if (count % 2 != 0)
return false;
// Split the number in two equal
// halves and store their sum
int half = count / 2;
int first_half = n / pow(10, half);
int second_half = n % int(pow(10, half));
int sum = first_half + second_half;
// Check if the square of the sum
// is equal to the original number
if ((sum * sum) == n)
return true;
else
return false;
}
// Driver code
int main()
{
if (istech(81))
cout << "True" << endl;
else
cout << "False" << endl;
if (istech(2025))
cout << "True" << endl;
else
cout << "False" << endl;
if (istech(1521))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Java
// Java program to check if a
// number is Tech number or not
public class TechNumber {
// Function to count the
// number of digits in a number
static int countDigits(int n)
{
int count = 0;
while (n > 0) {
n /= 10;
count++;
}
return count;
}
// Returns true if n is a tech
// number, else false
static boolean isTech(int n)
{
// Count the number of digits
int count = countDigits(n);
// Return false if digits are odd
if (count % 2 != 0)
return false;
// Split the number in two
// equal halve and store their sum
int half = count / 2;
int first_half = n / (int)Math.pow(10, half);
int second_half = n % (int)Math.pow(10, half);
int sum = first_half + second_half;
// Check if the square of the sum
// is equal to the original number
if ((sum * sum) == n)
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
if (isTech(81))
System.out.println("True");
else
System.out.println("False");
if (isTech(2025))
System.out.println("True");
else
System.out.println("False");
if (isTech(1521))
System.out.println("True");
else
System.out.println("False");
}
}
// This code is contributed by
// Abhishek Kumar
Python
import math
# Function to count the number of digits in a number
def count_digits(n):
count = 0
while n > 0:
n //= 10
count += 1
return count
# Returns true if n is a tech number, else false
def is_tech(n):
# Count the number of digits
count = count_digits(n)
# Return false if digits are odd
if count % 2 != 0:
return False
# Split the number in two equal halves and store their sum
half = count // 2
first_half = n // 10 ** half
second_half = n % (10 ** half)
sum_val = first_half + second_half
# Check if the square of the sum is equal to the original number
if sum_val ** 2 == n:
return True
else:
return False
# Driver code
if is_tech(81):
print("True")
else:
print("False")
if is_tech(2025):
print("True")
else:
print("False")
if is_tech(1521):
print("True")
else:
print("False")
C#
using System;
namespace TechNumberExample
{
class Program
{
// Function to count the number of digits in a number
static int CountDigits(int n)
{
int count = 0;
while (n > 0)
{
n /= 10;
count++;
}
return count;
}
// Function to check if a number is a Tech number
static bool IsTech(int n)
{
int count = CountDigits(n);
// Return false if the number of digits is odd
if (count % 2 != 0)
return false;
int half = count / 2;
int firstHalf = n / (int)Math.Pow(10, half);
int secondHalf = n % (int)Math.Pow(10, half);
int sum = firstHalf + secondHalf;
// Check if the square of the sum is equal to the original number
return (sum * sum) == n;
}
static void Main(string[] args)
{
// Test cases
Console.WriteLine(IsTech(81) ? "True" : "False");
Console.WriteLine(IsTech(2025) ? "True" : "False");
Console.WriteLine(IsTech(1521) ? "True" : "False");
}
}
}
JavaScript
// Javascript program to check if
//a number is Tech number or not
// Function to count the number
//of digits in a number
function countDigits(n)
{
let count = 0;
while (n > 0)
{
n = Math.floor(n / 10);
count++;
}
return count;
}
// Returns true if n is a tech
//number, else false
function isTech(n)
{
// Count the number of digits
const count = countDigits(n);
// Return false if digits are odd
if (count % 2 !== 0)
return false;
// Split the number in two equal halves
//and store their sum
const half = Math.floor(count / 2);
const first_half = Math.floor(n / 10 ** half);
const second_half = n % 10 ** half;
const sum = first_half + second_half;
// Check if the square of the sum
//is equal to the original number
if (sum * sum === n)
return true;
else
return false;
}
// Driver code
if (isTech(81))
console.log("True");
else
console.log("False");
if (isTech(2025))
console.log("True");
else
console.log("False");
if (isTech(1521))
console.log("True");
else
console.log("False");
// This code is contributed by
// Abhishek Kumar
Time Complexity: O(log n)
Auxiliary Space: O(1)
Similar Reads
Check Whether a number is Duck Number or not A Duck number is a positive number which has zeroes present in it, For example 3210, 8050896, 70709 are all Duck numbers. Please note that a numbers with only leading 0s is not considered as Duck Number. For example, numbers like 035 or 0012 are not considered as Duck Numbers. A number like 01203 is
8 min read
Check whether a given number N is a Nude Number or not Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output
4 min read
Check whether a given number N is a Nude Number or not Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output
4 min read
Check if a number is a Krishnamurthy Number or not A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.For example, 145 is the sum of the factorial of each digit.1! + 4! + 5! = 1 + 24 + 120 = 145 Examples: Input : 145Output : YESExplanation: 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to input,
10 min read
Program to check for a Valid IMEI Number International Mobile Equipment Identity (IMEI) is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone, but can also be displayed on-screen on most phones by entering *#06# on the dialpad, or al
6 min read
Check if a number is in given base or not Given a number as a string and a base, check if the given number is in the given base or not. Examples: Input : str = "1010", base = 2 Output : Yes Input : str = "1015", base = 2 Output : No Input : str = "AF87", base = 16 Output : Yes The idea is to one by one check if all digits are in the given b
10 min read