In the previous article, we have discussed Java Program to Check Harshad Number
In this article we are going to understand what Neon number is and how we can check whether a number is Neon or not in Java with examples.
Java Program to Check Neon Number
Neon numbers are numbers where the sum of digits of square of the number is equal to the number itself.
Example: 9-> (9)2= 81, 8+1=9 Neon number 5 -> (5)2=25, 2+5=7 Not a Neon number 8 -> (8)2=64, 6+4=10 Not a Neon number
Where (number)2
represents the square of the number inside parenthesis.
In the above examples the number 9 square is 81 which adds upto 9. The remaining two’s square digits sum doesn’t add up to the numbers. Hence 9 is the only Neon number here.
Let’s see different ways to check neon number.
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Approach:
- We ask the user to enter a number which we store in an integer variable
num
. - Then we store the square of
num
in a variablesquare
. - We then use a while loop to iterate through all the digits in the number inside square variable and add it all.
- If the resultant sum is equal to the number, it is said to be a Neon number or else it is not.
Method-1: Java Program to Check Neon Number By Using Static Value
import java.util.Scanner; public class NeonNumber { public static void main(String args[]) { //A number is declared int num = 9; //Square holds the square of the number int square = num*num , sum = 0, remainder; //Loop to iterate and add the digits of the square number while (square > 0) { remainder = square % 10; sum = sum + remainder; square = square / 10; } if (num == sum) { System.out.println(num+" is a Neon Number"); } else { System.out.println(num+" is not a Neon Number"); } } }
Output: Enter a number : 9 9 is a Neon Number
Method-2: Java Program to Check Neon Number By Using User Input Value
import java.util.Scanner; public class NeonNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Square holds the square of the number int square = num*num , sum = 0, remainder; //Loop to iterate and add the digits of the square number while (square > 0) { remainder = square % 10; sum = sum + remainder; square = square / 10; } if (num == sum) { System.out.println(num+" is a Neon Number"); } else { System.out.println(num+" is not a Neon Number"); } } }
Output: Enter a number : 9 9 is a Neon Number
Method-3: Java Program to Check Neon Number By Using User Defined Method
import java.util.Scanner; public class NeonNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //calling the user defined method //to check Neon number or not. checkNumber(num); } //checkNumber() method tocheck Neon number public static void checkNumber(int num) { //Square holds the square of the number int square = num*num , sum = 0, remainder; //Loop to iterate and add the digits of the square number while (square > 0) { remainder = square % 10; sum = sum + remainder; square = square / 10; } if (num == sum) { System.out.println(num+" is a Neon Number"); } else { System.out.println(num+" is not a Neon Number"); } } }
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Related Java Programs: