Narcissistic Number

 //Wap to accept a number and check it 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=1^4+6^4+3^4+4^4=1634

import java.util.*;

class narcisst

{//class narcisst opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no//153

        int r=0,c=0,s=0,k=n,a=n;//variable r to extract the digits,variable c to count the no digits,variable k and a to store the no,vvariable s to store the sum

        while(n>0)//153>0 t//15>0 t//1>0 t//0>0 false

        {

            c=c+1;//0+1=1//1+1=2//2+1=3

            n=n/10;//153/10=15//15/10=1//1/10=0

        }

        while(a>0)//153>0 t//15>0 t//1>0 t//0>0 false

        {

            r=a%10;//153%10=3//15%10=5//1%10=1

            s=s+(int)Math.pow(r,c);//0+27=27//27+125=152//152+1=153

            a=a/10;//153/10=15//15/10=1//1/10=0

            

        }

        if(s==k)//153==153 true

        System.out.println("It is a narcisst no");

        else

        System.out.println("It is not a narcisst no");

    }//main method closes

}//class closes


    

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...