Ticker

6/recent/ticker-posts

Neon Number in JAVA

Neon Number :

A number whose sum of digits of square of the number is equal to the number is called neon number. For example, number 9 is a neon number as its square is 81 and the sum of the digits is 9. i.e. 9 is a neon number. There are only 3 neon numbers. They are 0, 1, and 9.


Example 1 : 9
      9
=>  9 * 9
=>   81
=>  8+1
=>  9 = Number = 9 , Hence 9 is a Neon number.
Example 2 : 1
      1
=  1 * 1
=   1 = 1 = Number, Hence 1 is an a Neon number.
Example 3 : 7
      7
=  7 * 7
=   49
= 4+9
=13  != 7 , Hence 7 is not a Neon number.

Algorithm to check whether a given number is a Neon Number or not:

Step 1: Accept a number.
Step 2: Calculate the square of a number and store it in a variable (say square).
Step 3: Find the sum of digits of the square of a number and store it in a variable (say sum).
Step 4: If the sum is equal to the accepted number then display "Neon number" else display "Not Neon Number".
Step 5: Stop

Sample Input and Output:

Input 1:    Enter number here --> 1
Output 1: The given number 1 is a Neon number

Input 2:    Enter number here --> 9
Output 2: The given number 9 is a Neon number

Input 3:    Enter number here --> 7
Output 3: The given number 7 is not a Neon number


Question: Write a program in java to check whether a given number is a Neon number or not.

The above Algorithm can be implemented in JAVA Programming in several ways. Some of the JAVA Program codes are given below:


Code 1: This is the general code to check for a Neon number in JAVA.



// Program to check whether a given number is a Neon number or not

import java.io.*;

public class neon {

    public static void main(String[] argsthrows IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter number here --> ");

        int number = Integer.parseInt(in.readLine());

        int square = number * number;

        int sum = 0;

        while (square > 0) {

            int lastDigit = square % 10;

            sum = sum + lastDigit;

            square = square / 10;

        }

        if (sum == number)

            System.out.println("The given number " + number + " is a Neon number");

        else

            System.out.println("The given number " + number + " is not a Neon number");

    }

}


Code 2: This code accepts a number in String data type to check whether a given number is Neon number or not.



// Program to check whether a given number is a Neon number or not

import java.io.*;

public class neon1 {

    public static void main(String[] argsthrows IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter number here --> ");

        String number = in.readLine();

        String square = Integer.toString((intMath.pow(Integer.valueOf(number), 2));

        int sum = 0;

        for (int i = 0i < square.length(); i++) {

            int currentDigit = square.charAt(i) - 48;

            sum = sum + currentDigit;

        }

        if (sum == Integer.valueOf(number))

            System.out.println("The given number " + number + " is a Neon number");

        else

            System.out.println("The given number " + number + " is not a Neon number");

    }

}



    If you have any doubts/questions related to Program,  feel free to comment below.

    Post a Comment

    0 Comments