-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmithNumber.java
More file actions
65 lines (60 loc) · 1.55 KB
/
Copy pathSmithNumber.java
File metadata and controls
65 lines (60 loc) · 1.55 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
package basic;
import java.util.Scanner;
class SmithNumber {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("Enter a number");
int n = ob.nextInt();
if (prime(n) == 1) {
System.out.println(n + " is not a Smith_Number");
} else {
smithCheck(n);
}
}
public static int prime(int x)//Method to check Prime no.
{
if (x == 1) {
return 0;
}
int c = 0;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
c++;
}
}
if (c == 0) {
return 1;
} else {
return 0;
}
}
public static int digitSum(int x)//Method to find sum of digits
{
int sum = 0;
while (x != 0) {
int rem = x % 10;
x = x / 10;
sum += rem;
}
return sum;
}
public static void smithCheck(int n)//Method to check no. is a Smith no. or not
{
int s1 = digitSum(n);//Storing sum of entered no.
int s2 = 0;
for (int i = 2; i <= n; i++)//loop to find prime factors of no.
{
if (prime(i) == 1) {
while (n % i == 0) {
s2 += digitSum(i);
n = n / i;
}
}
}
if (s1 == s2) {
System.out.println("Entered no is a Smith no");
} else {
System.out.println("Entered no is not a Smith no");
}
}
}