How to check whether the input number is a Neon Number in Golang?


In this tutorial, we are going to write and explain the code to check whether the given number is a neon number or not. The Neon number is a number that is equal to the sum of all the digits of its square.

For example, 9 is a neon number as shown below

The square of 9 is: 9 * 9 = 81 The sum of each digit of the square i.e 81 is 9 and the number is also 9 so 9 is a Neon number.

Algorithm

  • STEP 1 − First we are declaring the numbers between which we have to find the Neon Numbers.

  • STEP 2 − Now, we are taking input from the user that we have to check whether it is a Neon number or not.

  • STEP 3 − Calling the isNeonNumber(num int32) function and check that a current number is a Neon number or not and if yes then printing the number.

Example

In this example, we are going to find the Neon numbers between two integers which are taken as input from the user.

Time Complexity

O(1) - The time complexity is constant because no matter what is the input the program will take sane time.

Space Complexity

O(1) - The variables are static in the program so the space complexity is also constant.

package main // fmt package provides the function to print anything import "fmt" func isNeonNumber(num int32) bool { // declaring the sum variable which will store // the sum of each digit in the square of the number var sum int32 = 0 // declaring and initializing the tempNum variable with the square of num // on which we will perform some arithmetic operations ahead var tempNum int32 = num * num // running a for loop till the tempNum become zero for tempNum != 0 { // picking each digit by doing mode on the current number currDigit := tempNum % 10 // adding the current digit into the sum variable sum = sum + currDigit // eliminating the last digit from the end tempNum = tempNum / 10 } // if the sum is equal to the number then returning true if sum == num { return true } return false } func main() { // declaring the integer number using the var keyword between which we // have to find the Neon numbers var number int32 fmt.Println("Program to check whether the given number is a Neon number or not.") fmt.Println("Enter the number to check whether it is the Neon number or not.") // Taking the input from the user fmt.Scanln(&number) if isNeonNumber(number) { fmt.Println(number, "is a Neon number.") } else { fmt.Println(number, "is not a Neon number.") } }

Output1

Program to check whether the given number is a Neon number or not.
Enter the number to check whether it is the Neon number or not.
9
9 is a Neon number.

Output2

Program to check whether the given number is a Neon number or not.
Enter the number to check whether it is the Neon number or not.
202
202 is not a Neon number.

Description of code

  • var number int32 - This line of code is declaring int32 variables. We will check whether this number is a Neon number or not.

  • fmt.Scanln(&number)- Here we are taking the input from the user.

  • if isNeonNumber(number) {} - In this if condition the isNeonNumber() function is called and passes the parameter num which is the current index of the for loop. The if condition is checking the value return by isNeonNumber() function. If the value is true then we are printing the number.

  • func isNeonNumber(num int32) bool {} - This is the isNeonNumber() function. It consists of a num parameter with data type int32 and has a return type bool.

    • var sum int32 = 0 - Here we are declaring the sum variable of int32 type which will store the sum of digits of the square of the number.

    • var tempNum int32 = num - Declaring the tempNum variable of int32 type which is getting initialized by the square of num value. We will do arithmetic operations on tempNum that is why we are not directly performing these arithmetic operations on num variables which will get compared with sum later.

    • for tempNum != 0 {} - This for loop is running till the tempNum become zero.

    • currDigit := tempNum % 10 - We fetch the last digit of the current number by applying the % with 10 and storing that in currDigit.

    • sum = sum + currDigit - Adding the currDigit to the sum variable.

    • tempNum = tempNum / 10 - Dividing the tempNum by 10 so that the last digit gets removed from the value.

    • if sum == num {} - In the end, we are comparing the sum with the number and returning true or else false.

Logic Explanation

The Number is Neon

Suppose we have a number 371 and we have to check if that number is Armstrong or not.

  • Taking the last number by doing % - 81 % 10 = 1 Sum = 0 + 1 -> sum = 1

    Num -> 81 /10 = 8

  • Taking the last number by doing % - 8 % 10 = 8 Sum = 1 + 8 -> sum = 9

    Num -> 8 /10 = 0

As you can see the sum is equal to the initial number that is why 9 is a Neon number

The Number is not Neon

Suppose we have a number 12 and we have to check if that number is Neon or not.

The Square of 12 is 144 now we will find the sum of each digit of 144..

  • Taking the last number by doing % -144 % 10 = 4 Sum = 0 + 4 -> sum = 4

    Num -> 144 /10 = 14

  • Taking the last number by doing % - 14 % 10 = 4 Sum = 4 + 4 -> sum = 8

    Num -> 14 /10 = 1

  • Taking the last number by doing % - 1 % 10 = 1 Sum = 8 + 1 -> sum = 9

    Num -> 1 /10 = 0

Conclusion

As you can see the sum is not equal to the initial number that is why 12 is not a Neon number. This is all about the Golang code to check whether the given number is a Neon number or not. To learn more about go you can explore these tutorials.

Updated on: 29-Aug-2022

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements