Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token class-name">The</span> square of <span class="token number">9</span> is<span class="token operator">:</span> <span class="token number">9</span> <span class="token operator">*</span> <span class="token number">9</span> <span class="token operator">=</span> <span class="token number">81</span> <span class="token class-name">The</span> sum of each digit of the square i<span class="token punctuation">.</span>e <span class="token number">81</span> is <span class="token number">9</span> and the number is also <span class="token number">9</span> so <span class="token number">9</span> is a <span class="token class-name">Neon</span> number<span class="token punctuation">.</span> </div>
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.
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">isNeonNumber</span><span class="token punctuation">(</span>num int32<span class="token punctuation">)</span> bool <span class="token punctuation">{</span>
<span class="token comment">// declaring the sum variable which will store</span>
<span class="token comment">// the sum of each digit in the square of the number</span>
<span class="token keyword">var</span> sum int32 <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// declaring and initializing the tempNum variable with the square of num</span>
<span class="token comment">// on which we will perform some arithmetic operations ahead</span>
<span class="token keyword">var</span> tempNum int32 <span class="token operator">=</span> num <span class="token operator">*</span> num
<span class="token comment">// running a for loop till the tempNum become zero</span>
<span class="token keyword">for</span> tempNum <span class="token operator">!=</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token comment">// picking each digit by doing mode on the current number</span>
currDigit <span class="token operator">:</span><span class="token operator">=</span> tempNum <span class="token operator">%</span> <span class="token number">10</span>
<span class="token comment">// adding the current digit into the sum variable</span>
sum <span class="token operator">=</span> sum <span class="token operator">+</span> currDigit
<span class="token comment">// eliminating the last digit from the end</span>
tempNum <span class="token operator">=</span> tempNum <span class="token operator">/</span> <span class="token number">10</span>
<span class="token punctuation">}</span>
<span class="token comment">// if the sum is equal to the number then returning true</span>
<span class="token keyword">if</span> sum <span class="token operator">==</span> num <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// declaring the integer number using the var keyword between which we</span>
<span class="token comment">// have to find the Neon numbers</span>
<span class="token keyword">var</span> number int32
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to check whether the given number is a Neon number or not."</span><span class="token punctuation">)</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Enter the number to check whether it is the Neon number or not."</span><span class="token punctuation">)</span>
<span class="token comment">// Taking the input from the user</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>number<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token function">isNeonNumber</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> <span class="token string">"is a Neon number."</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> <span class="token string">"is not a Neon number."</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div>
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.
