-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnarcissistic.java
More file actions
32 lines (27 loc) · 919 Bytes
/
narcissistic.java
File metadata and controls
32 lines (27 loc) · 919 Bytes
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
import java.util.Scanner;
//Each digit in a narcissistic number must be raised to the number of
//digits in that number and all should be sum, if the result is the
//same as the number, then it is narcissistic
public class narcissistic {
static int number;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a number ");
number = sc.nextInt();
sc.close();
boolean result = narcissist(number);
System.out.println("Is the number narcissistic? " + result);
}
public static boolean narcissist(int temp){
int sum = 0;
int original = temp;
String num = String.valueOf(original);
int length = num.length();
while (temp>0){
int digit = temp%10;
sum += Math.pow(digit, length);
temp/=10;
}
return sum == original;
}
}