Java program to find a number is Narcissistic Number or not.
Narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits.
1634
Total digit count is 4
Then you have apply Armstrong Number method on it.
1pow(4)+6power(4)+3pow(4)+4pow(4)
gives
1634. hence true;
Output: 1634
True
1234
False
1634
Total digit count is 4
Then you have apply Armstrong Number method on it.
1pow(4)+6power(4)+3pow(4)+4pow(4)
gives
1634. hence true;
public class PracticeDemo {
public static int getDigitCount(int number){
int divisor=0,replacer=number,count=1;
do{
divisor=replacer/10;
count++;
replacer=divisor;
}while(divisor>10);
return count;
}
public static boolean checkNarcissistic(int value){
boolean isNarcissistic=false;
int remainder,divisible=0,replacer=value,power=getDigitCount(value),suming=0;
do{
remainder=replacer%10;
divisible=replacer/10;
suming+=Math.pow(remainder, power);
replacer=divisible;
}while(divisible>10);
suming=(int)(suming+Math.pow(divisible, power));
if(suming==value)
isNarcissistic=true;
else
isNarcissistic=false;
return isNarcissistic;
}
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
getDigitCount(x);
System.out.println(checkNarcissistic(x) ? "True":"False");
sc.close();
}
}
Output: 1634
True
1234
False
Comments
Post a Comment