Java Program to Check If a Number is Neon Number or Not Last Updated : 16 Mar, 2023 Comments Improve Suggest changes 13 Likes Like Report A neon number is a number where the sum of digits of the square of the number is equal to the number. The task is to check and print neon numbers in a range. Illustration: Case 1: Input : 9 Output : Given number 9 is Neon number Explanation : square of 9=9*9=81; sum of digit of square : 8+1=9(which is equal to given number) Case 2: Input : 8 Output : Given number is not a Neon number Explanation : square of 8=8*8=64 sum of digit of square : 6+4=10(which is not equal to given number) Algorithm : First, find the square of the given number.Find the sum of the digit of the square by using a loop. The condition checksum is equal to the given numberReturn trueElse return false.Pseudo code : Square =n*n; while(square>0) { int r=square%10; sum+=r; square=square/10; } Example: Java // Java Program to Check If a Number is Neon number or not // Importing java input/output library import java.io.*; class GFG { // Method to check whether number is neon or not // Boolean type public static boolean checkNeon(int n) { // squaring the number to be checked int square = n * n; // Initializing current sum to 0 int sum = 0; // If product is positive while (square > 0) { // Step 1: Find remainder int r = square % 10; // Add remainder to the current sum sum += r; // Drop last digit of the product // and store the number square = square / 10; } // Condition check // Sum of digits of number obtained is // equal to original number if (sum == n) // number is neon return true; else // number is not neon return false; } // Main driver method public static void main(String[] args) { // Custom input int n = 9; // Calling above function to check custom number or // if user entered number via Scanner class if (checkNeon(n)) // Print number considered is neon System.out.println("Given number " + n + " is Neon number"); else // Print number considered is not neon System.out.println("Given number " + n + " is not a Neon number"); } } OutputGiven number 9 is Neon number Time Complexity: O(l) where l is the number of the digit in the square of the given number Recursive Approach: Explanation: In this approach, we use a recursive function isNeonNumber to check if the input number is a neon number.The function takes two arguments: the square of the input number and the input number itself.At each recursive call, we extract the last digit of the square number and subtract it from the input number.We then call the function recursively with the remaining digits of the square number and the updated input number.If the square number has no more digits left (i.e., square == 0), then we check if the input number is zero (i.e., number == 0).If the input number is zero, then the original input number is a neon number; otherwise, it is not. Java import java.util.Scanner; public class NeonNumber { public static void main(String[] args) { int number=9; int square = number * number; if (isNeonNumber(square, number)) { System.out.println(number + " is a neon number"); } else { System.out.println(number + " is not a neon number"); } } private static boolean isNeonNumber(int square, int number) { if (square == 0) { return number == 0; } else { int digit = square % 10; return isNeonNumber(square / 10, number - digit); } } } Output9 is a neon number Time Complexity: O(logn) Auxiliary Space: O(logn) Comment S sambhavshrivastava20 Follow 13 Improve S sambhavshrivastava20 Follow 13 Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like