Wednesday, September 19

Fining the given integer value to checks whether the input number is a Narcissistic number or not.

Hi All,


Explanation

First of all, what is a Narcissistic Number?
An n-digit number that is the sum of the nth powers of its digits is called an n-narcissistic number.

For example, take the number 8208

8208 = 8^4 + 2^4 + 0^4 + 8^4

Example:
 8^4 = 8*8*8*8 = 4096
 2^4 = 2*2*2*2 =   16
 0^4 = 0*0*0*0 =    0
 8^4 = 8*8*8*8 = 4096

 Total = 8208

So, this is a Narcissistic Number.


Coding:

import java.io.*;
import java.util.*;
public class MyCode {
    public static void main(String args[] ) throws Exception {

    //Write code here
        Scanner sc =new Scanner(System.in);
        int n= sc.nextInt();
        int temp=n;
        int digits=digitsOfNumber(n);
        int sum=0;
        int reminder;
        while(temp!=0)
        {
            reminder = temp%10;
            sum=sum+(int)Math.pow(reminder,digits);
            temp=temp/10;
        }
        if(sum==n)
            System.out.println("True");
        else
            System.out.println("False");
     
   }
   public static int digitsOfNumber(int n)
   {
        int temp=n;
        int digits=0;
        while(temp!=0)
        {
            digits++;
            temp=temp/10;
        }
        return digits;
    }
}


INPUT
8208

Output
True


No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...