Neon Number - Coder's point

Saturday, March 16, 2019

Neon Number

A number is said to be a Neon Number if the sum of digits of the square of the number is equal to the numberitself. Example- 9 is a Neon Number. 9*9=81 and 8+1=9.Hence it is a Neon Number
 public class Neon  
 {  
   boolean isNeon(int n)  
   {  
     String s=Integer.toString(n*n);  
     int sum=0,i=0;  
     while(i<s.length())  
     {  
       sum+=Integer.parseInt(Character.toString(s.charAt(i)));  
       i++;  
     }  
     if(sum==n)return true;  
     return false;  
   }  
   static void main()  
   {  
     Neon neo = new Neon();  
     java.util.Scanner in = new java.util.Scanner(System.in);  
     System.out.println("Enter a no.");  
     int n=in.nextInt();  
     if(neo.isNeon(n))System.out.println("Neon no.");  
     else System.out.println("Not a Neon no.");  
   }  
 }  
OUTPUT:
Enter a no.
76
Not a Neon no.
Enter a no.
9
Neon no.

No comments:

Post a Comment