-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hello5.java
46 lines (46 loc) · 1.24 KB
/
Hello5.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;
public class TechNumberExample2
{
public static void main(String args[])
{
int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: ");
//reads an integer from the user
n = sc.nextInt();
//assign the value of n into num
num = n;
//the loop executes until the condition returns false
while (num > 0)
{
//incerements the variable digits by 1
digits++;
//removes the last digit of the given number
num = num / 10;
}
//check if the given number has an even number of digits or not
if (digits % 2 == 0)
{
num = n;
//determines the first half of the given number
firstHalf = num % (int) Math.pow(10, digits / 2);
//determines the second half of the given number
secondHalf = num / (int) Math.pow(10, digits / 2);
//calculate the square of both the numbers
squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf);
//compares the square of the sum with the given number
if (n == squareOfSum)
{
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.");
}
}
}