Swift Program to Check whether the input number is a Neon Number

This tutorial will discuss how to write a swift program to check whether the input number is a Neon Number.

In a number system, a number whose sum of digits of square of the number is equal to the number is known as neon number.

Below is an example of the neon numbers ?

Let?s see if number 9 is a neon number or not? -

The square of 9 is 81 and the sum of 81 is 9

So 9 is a neon number

Let?s take another example - 15

The entered number is not a neon number

Here, 15 is a not neon number because the square of 15 is 225 and the sum of 225 is 9(2 + 2 + 5 = 9) which is not equal to 15

Algorithm

  • Step 1 ? Enter the number from the user in integer value.

  • Step 2 ? Read the integer number and assign it to the variable named inputNumber.

  • Step 3 ? Calculate the square of the number and assign it to a variable named squareNumber.

  • Step 4 ? Calculate the sum of the digits by slicing and assigning to the squareNumber using while loop.

  • Step 5 ? Check if the sum match or not with the entered number using if statement. If the sum match, then the entered number is neon number. If not, then the entered number is not a neon number.

  • Step 6 ? Display the final result on the output screen

Example 1

The following program shows how to check whether the input number is a Neon Number.

<div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the number-"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> inputNumber <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token keyword">var</span> squareNumber <span class="token operator">=</span> inputNumber <span class="token operator">*</span> inputNumber
<span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">var</span> reminder <span class="token operator">=</span> <span class="token number">0</span>

<span class="token keyword">while</span><span class="token punctuation">(</span>squareNumber <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	reminder <span class="token operator">=</span> squareNumber <span class="token operator">%</span> <span class="token number">10</span>
	sum <span class="token operator">=</span> sum <span class="token operator">+</span> reminder
	squareNumber <span class="token operator">=</span> squareNumber <span class="token operator">/</span> <span class="token number">10</span>
<span class="token punctuation">}</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>sum <span class="token operator">==</span> inputNumber<span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered number \(inputNumber) is 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 function">print</span><span class="token punctuation">(</span><span class="token string">"Entered number \(inputNumber) is not neon number"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>

STDIN Input

Please enter the number-
9

Output

Entered number 9 is neon number

In the above code, first we take input from the user using readLine() function that is 9. After that we find the square of the given number that is 81. Now calculate the sum of the digits of the square that is 8 + 1 = 9 using the following code ?

// Check if squareNumber is positive
while(squareNumber > 0){

   // Find the remainder of the squareNumber
   // Using mod 10
   reminder = squareNumber % 10

   // Add the remainder to the sum
   sum = sum + reminder

   // Now leave the last digit of the division
   // and store the value
    squareNumber = squareNumber / 10
}

The working of the above code is ?

squareNumber = 81<br>reminder = 0<br>sum = 0<br>while(81 > 0){<br>   reminder = 81 % 10 = 1<br>   sum = 0 + 1 = 1<br>   squareNumber = 81 / 10 = 8.1 = 8<br>}<br>while(8 > 0){<br>   reminder = 8 % 10 = 8<br>   sum = 1 + 8 = 9<br>   squareNumber = 8 / 10 = 0.8 = 0<br>}

Now using the if statement, we check the sum we get is equal to the input value. If the sum is equal to the input value, then print the entered number is a neon number. If the sum is not equal to the input value, then print the entered number is not a neon number.

Here in our code, the sum is equal to the input value (that is 9 == 9), so the output is the Entered number 9 is the neon number.

Example 2

The following program shows how to find the neon number in the specified user input range.

<div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

func <span class="token function">checkingNeonNumber</span><span class="token punctuation">(</span>inputNumber <span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Bool<span class="token punctuation">{</span>

	<span class="token keyword">var</span> squareNumber <span class="token operator">=</span> inputNumber <span class="token operator">*</span> inputNumber
	<span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span>
	<span class="token keyword">var</span> reminder <span class="token operator">=</span> <span class="token number">0</span>

	<span class="token keyword">while</span><span class="token punctuation">(</span>squareNumber <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		reminder <span class="token operator">=</span> squareNumber <span class="token operator">%</span> <span class="token number">10</span>
		sum <span class="token operator">=</span> sum <span class="token operator">+</span> reminder
		squareNumber <span class="token operator">=</span> squareNumber <span class="token operator">/</span> <span class="token number">10</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token punctuation">(</span>sum <span class="token operator">==</span> inputNumber<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter lower limit"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> lowLimit <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter upper limit"</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> upperLimit <span class="token operator">=</span> <span class="token function">Int</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Neon numbers are:"</span><span class="token punctuation">)</span>

<span class="token keyword">for</span> j <span class="token keyword">in</span> lowLimit<span class="token operator">...</span>upperLimit<span class="token punctuation">{</span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">checkingNeonNumber</span><span class="token punctuation">(</span>inputNumber<span class="token operator">:</span>j<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token function">print</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div>

STDIN Input

Please enter lower limit
1
Please enter upper limit
100

Output

Neon numbers are:
1
9

In the above code, first, we create a function named as checking NeonNumber(). This function returns neon numbers. The process of finding a neon number is the same as we discussed in the above algorithm. After that, we take the lower and upper limit from the user using the readLine() function. Here, the lower limit and upper limit represent the starting and end point of the range in between which we will find all the neon numbers. Now using for loop we iterate the loop from the lower limit to upper limit and check each number for a neon number using the checkingNeonNumber() as shown in the below code:

for j in lowLimit...upperLimit{
   if (checkingNeonNumber(inputNumber:j)){
      print(j)
   }
}

And display the list of neon numbers present in between the given range which is 1 and 9.

Updated on: 2022-08-01T13:16:46+05:30

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements