Neon Number In Java Trick 2024 | topperbhai.com

 Neon Number Program In Java

Neon Number In Java 2022

  

What Is 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 number itself. 

For  example:- If the input number is 9, it's square is 9*9=81 and the sum of the digits is also 9 (= 8+1).

 Neon  number  between  1 and 1000 are 1 and 9

 
Neon Number In Java


In this tutorial, first we will learn how to write the Neon Number in Java Algorithm, after that we will write the code of  Neon Number in Java program to check whether the input number is neon or not.



 Neon Number in Java Algorithm:-

Step 1:  Initialize a number (n).

Step 2:  Find the square of the number (sq).

Step 3:    Extract the last digit of the square (extract).

Step 4:   Then add the digit of the square (sumofdig).

Step 5:  Repeat steps 3 and 5 until the input number becomes 0.

Step 6:  If the sum of digits (sumofdig) are equal to the input number (n), then the number is a  Neon number otherwise not.


For  example:- 

=  9

 = 9 * 9 = 81

 = 8 +1 = 9


The number 9 is  a neon number because  the sum of all digits  of the square of the number 9 represent the same result i.e. 9. 

 

If you are  not able to understand the above sentence,  then look at the example given above. 


In this post, I have have  written a short and easy to understand java program below which checks whether the number input by the user is a neon number or not.


Basically, the logic I have implemented in this program is pretty easy to understand.


First, I will ask the user to enter a number then, find the square of that number after that I will extract the digits (starting from the last digit) of the  square.


Repeat this process till the first digit then, add all digits of the square. Finally, I will check if the sum of all  digits are equal to number or not. 


 

  Neon Number In Java using While Loop

 import java.util.*;  
 class Neon_Number  
 {  
   public static void main()  
   {  
     Scanner sc=new Scanner(System.in);  
     int n,sq,sumofdig=0,extract;  
     System.out.println("Enter any Number");  
     n=sc.nextInt(); //Example: n=9  
     sq=n*n;//Square of Number (Ex. sq=9*9;)  
     while(sq!=0)  
     {  
       extract=sq%10;  //Extract digits of square (ex.81%10=1)  
       sumofdig=sumofdig+extract;  //Add digits(e.g. sumofdig=0+1;)  
       sq=sq/10;  //Reduces Number(ex.81/10=8)  
     }  
     if(sumofdig == n) //Checks sumofdig of sq is equal to n  
     {  
       System.out.println("It's a Neon Number");  
     }  
     else  
     {  
       System.out.println("Not a Neon Number");  
     }  
   }  
 }  
 -------------  
 OUTPUT:-
 -------------  
 Enter any Number  
 9  
 It's a Neon Number 

Post a Comment

0 Comments