-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumbers.java
More file actions
126 lines (122 loc) · 3.77 KB
/
Copy pathNumbers.java
File metadata and controls
126 lines (122 loc) · 3.77 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.Scanner;
public class Numbers {
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
n = sc.nextInt();
prime();
palindrome();
armstrong();
harshad();
spy();
perfect();
neon();
factorial(); }
static void prime() {
if (n <= 1) {
System.out.println("The number is neither prime nor composite.");
return; }
int c = 2;
while (c * c <= n) {
if (n % c == 0) {
System.out.println("The number is not prime.");
return; }
c += 1; }
System.out.println("The number is prime.");
}
static void palindrome() {
int x = n;
int rev = 0, r;
while (x > 0) {
r = x % 10;
rev = rev * 10 + r; //new variable method
x /= 10;
}
if (rev == n) {
System.out.println("The number is palindrome.");
} else {
System.out.println("The number is not palindrome."); }
} //same when reversed
static void armstrong() {
int x = n;
int y = n;
int r;
int c = 0;
int sum = 0;
while (x > 0) {
r = x % 10;
x /= 10;
c++; }
while (y > 0) {
r = y % 10;
sum += Math.pow(r, c);
y /= 10; }
if (sum == n) {
System.out.println("The number is an armstrong number.");
} else {
System.out.println("The number is not an armstrong number."); }
} //sum of each digit raised to the power equal to the number of digits
static void harshad() {
int y = n;
int r;
int sum = 0;
while (y > 0) {
r = y % 10;
sum += r;
y /= 10; }
if (n % sum == 0) {
System.out.println("The number is a Harshad number.");
} else {
System.out.println("The number is not a Harshad number."); }
} //number divisible by the sum of its digits
static void spy() {
int y = n;
int r;
int p = 1;
int sum = 0;
while (y > 0) {
r = y % 10;
sum += r;
p *= r;
y /= 10; }
if (p == sum) {
System.out.println("The number is a spy number.");
} else {
System.out.println("The number is not a spy number."); }
} //the product and sum of the digits are equal
static void perfect() {
int sum = 0;
int x = n;
System.out.print("The divisors of this number are: ");
for (int i = 1; i <= n; i++) {
if (x % i == 0) {
System.out.print(i + " ");
sum += i; }}
int y = sum - n;
if (n == y) {
System.out.println("\nThe number is a perfect number.");
} else {
System.out.println("\nThe number is not a perfect number."); }
} //number equal to the sum of its divisors
static void neon() {
int x=n*n;
int r;
int sum=0;
while(x>0) {
r=x%10;
sum+=r;
x/=10; }
if(sum==n) {
System.out.println("The number is a neon number."); }
else {
System.out.println("The number is not a neon number."); }} //number whose square's digits add up to the number itself
static void factorial() {
double x=n;
int p=1;
while(x>0) {
p*=x;
x--; }
System.out.println("Factorial of this number is "+p);
}
}