BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Neon Number in Java with example

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check whether a given number is Neon number or not. We will also write a program to find all the Neon numbers in a given range.

What is a Neon Number?

A number is called Neon number if sum of digits of its square is equal to the number itself. For example 9 is a neon number because:

Original Number = 9
Square of 9 = 9 X 9 = 81
Sum of digits of its square = 8+1 = 9 = Original Number

Similarly 1 is also a neon number because:

Original number = 1
Square of 1 = 1 X 1 = 1
Sum of digits of its square = 1 = original number

Java Program to check if a number is Neon Number

The steps to check for Neon number are:

  1. Read the number input by user.
  2. Calculate the square of the input number.
  3. Calculate the sum of digits of square.
  4. Compare the sum of digits of square and number, if both the equal then display “Neon Number” else display “Not a Neon Number“.
import java.util.*;
public class JavaExample
{
  public static void main(String args[])
  {
    int sumSquareDigits=0, num;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a number: ");
    //Reading user input and storing into variable num
    num = scanner.nextInt();
    scanner.close();
    int square = num * num;
    //Finding the sum of digits of square
    while(square != 0)
    {
      //getting last digit of square
      int lastDigit = square % 10;
      //adding the last digit to the sumSquareDigits
      sumSquareDigits = sumSquareDigits + lastDigit;
     //removing last digit from square to repeat the process
      //for second last digit and so on
      square = square / 10;
    }
    //comparing the number with sum of digits of square
    if(num == sumSquareDigits)
      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.

Program to find all Neon Numbers in a Given Range

In this program, we are finding all the Neon numbers in a given range. The user enters the start and end range, we then run a loop from start range till end range and for each number between this range, we call the checkNeon() method. This method checks whether the number is Neon number or not. If the number is Neon it gets displayed else it gets skipped.

import java.util.*;
public class JavaExample
{
  static boolean checkNeon(int num)
  {
    int square = num * num;
    int sumSquareDigits = 0;
    while (square != 0)
    {
      sumSquareDigits = sumSquareDigits + square % 10;
      square = square / 10;
    }
    return sumSquareDigits == num;
  }
  public static void main(String args[])
  {
    int startRange, endRange;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the Starting Range: ");
    startRange = scanner.nextInt();
    System.out.print("Enter the Ending Range: ");
    endRange = scanner.nextInt();
    System.out.print("All Neon numbers between "+startRange+
            " and "+endRange+" are: ");
    for (int i = startRange; i <= endRange; i++)
      if (checkNeon(i))
        System.out.print(i+" ");
  }
}

Output:

Enter the Starting Range: 1
Enter the Ending Range: 500
All Neon numbers between 1 and 500 are: 1 9 

Related Java Programs

  1. Java Program to check if given number is perfect square
  2. Fascinating number in Java
  3. Automorphic number in Java
  4. Sunny number in Java
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap