-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathtrimorphic_number.java
79 lines (68 loc) · 1.55 KB
/
trimorphic_number.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* A trimorphic number is whose cube ends with the number itself.
* Example:
* 49*49*49 = 117649
* which ends with 49
* This code checks whehter the input number is trimorphic or not.
*/
import java.util.*;
class trimorphic_number
{
void main()
{
//driver function
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number: ");
int number = scanner.nextInt();
scanner.nextLine();
if(is_trimorphic(number) == true)
{
System.out.println(number+" is a Trimorphic number!");
}
else
{
System.out.println(number+" is not a Trimorphic number!");
}
}
boolean is_trimorphic(int number)
{
//this function checks whether number is trimorphic or not
int cube = number*number*number;
if((cube % Math.pow(10,no_of_digits(number))) == number)
{
return true;
}
else
{
return false;
}
}
int no_of_digits(int number)
{
//this function counts the number of digits
int count = 0;
while(number!=0)
{
count++;
number/=10;
}
return count;
}
}
/*
* Test Cases-
*
* 1.
* Enter number:
* 24
* 24 is a Trimorphic number!
*
* 2.
* Enter number:
* 21
* 21 is not a Trimorphic number!
*
* Time Complexity: O(n)
* where n is the number of digits in the number
* Space Complexity: O(1)
*/