Narcissistic Number Java Program 20:07 Add Comment Edit Program for Narcissistic Number in Java : import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.io.*; public class Demo { public static void main(String args[]) throws Exception { // Write code here Scanner in = new Scanner(System.in); int n = in.nextInt(); int c = 0, a, temp = n, d = 0; while (temp != 0) { d++; temp = temp / 10; } temp = n; while (temp != 0) { a = temp % 10; c = (int) (c + power(a, d)); temp = temp / 10; } if (n == c) { System.out.println("True"); } else System.out.println("False"); } static int power(int a, int b) { int c, p = 1; for (c = 1; c <= b; c++) { p = p * a; } return p; } } Share this:
0 comments:
Post a Comment