Tech Number in JAVA
Tech Number
Tech Number : A Tech Number has even number of digits. If the number is split into two equal halves. then the square of the sum of these halves is equal to the number itself.
Example:
Input : 3025
Total Digits :
4
1st Half :
30
2nd Half :
25
Sum :
30 + 25 = 55
Square :
55 X 55 = 3025
Output :
It is a Tech Number
Input :
2025
Total Digits :
4
1st Half :
20
2nd Half :
25
Sum :
20 + 25 = 45
Square :
45 X 45 = 2025
Output :
It is a Tech Number
Input : 104
Total Digits : 3
Output : It is not a
Tech Number
Algorithm and Program given below :-
Algorithm :-
- Input number as a string
- Store the length of the string in a variable
- Convert the input number as Integer
- Check if the digits are even or not
- If digits are not even Print “It is not a Tech Number”
- If the digits are even then
- Divide the length by two
- Store two halves in two String Variable
- Now convert the two String variables into Integer Variables
- Calculate the sum of two halves
- Store the square of the sum of two halves
- Compare the square with the original number
- If it is equal Print “It is a Tech Number”
- Otherwise Print “ It is not a Tech Number”
- Compile and Run the Program
Program :-
import java.io.*;
import java.util.*;
class Tech_Number
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n,l,a,b,sum,sq,div;
String str,str1,str2;
System.out.println("Enter Number");
str= in.nextLine();//input number as a string
l=str.length();//calculating length of string
n= Integer.parseInt(str);//converting input number as integer
if(l%2==0)
{
div= l/2;
str1= str.substring(0,div);//storing 1st half as string
str2= str.substring(div,l);//storing 2nd half as string
a= Integer.parseInt(str1);//converting 1st half into integer
b= Integer.parseInt(str2);//converting 2nd half into integer
sum= a+b; //sum of the two halves
sq= sum*sum; // square of sum of two halves
if(n==sq)
{
System.out.println("It is a Tech Number : "+n);
}
else
{
System.out.println("It is not a Tech Number : "+n);
}
}
else
{
System.out.println("It is not a Tech Number : "+n);
}
}// end of main method
}// end of class
To Understand the Program in detail Watch the video :- -
Video Link of this Program : https://youtu.be/JbEC8NtLyoY
Follow me on Instagram
All the Best :)
Keep Learning :)
Comments
Post a Comment