How to check Neon number in java

Checking Neon  number




Steps to do this:-

1.take 4 integers n,r,sum=0,sq (where n is for the user input number . sq will hold the square of the number. sum will do the addition )

2. now do the square of the number by (n*n) and store it in sq

3.now start the while loop and condition is (sq>0) and check this
r=sq%10; // r stores the remainder (whenever we do module with a number it will return the last value of of this number ex:- if we do this
123%10 at first it will give us the 3 )

sum=sum+r; //in here sum=0 then 0=0+r this will happen and the each digit sum will be stored in the sum variable 

note* we have to initialize sum by sum=0 .if we don't it will store the garbage value and our code will calculate wrong answer

sq=sq/10;// whenever we divide a number by 10 it will elemenate the last number. so in here sq is now 1st two numbers 
(ex:-123/10 and sq=sq/10 then sq=12)

now close the while loop

4.now check if(sum==n) then print "this is neon number" else
print "this is not a neon number "

5.save and run

The Source Code

import java.util.Scanner;
class neon
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int n,r,sum=0;//n is for user input sum=0 for calculation of square
        int sq;
        System.out.println("enter the number");
        n=s.nextInt();
        sq=n*n;
        while(sq>0)
        {
            r=sq%10;
            sum=sum+r;
            sq=sq/10;
        }
        if(sum==n)
        {
            System.out.println("this is a neon number !");
        }
        else
        {
            System.out.println("this is not aneon number !");
        }
          
            }
}


OUTPUT





Watch My Video 



  

Comments

Popular Posts