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.

import Foundation import Glibc print("Please enter the number-") var inputNumber = Int(readLine()!)! var squareNumber = inputNumber * inputNumber var sum = 0 var reminder = 0 while(squareNumber > 0){ reminder = squareNumber % 10 sum = sum + reminder squareNumber = squareNumber / 10 } if (sum == inputNumber){ print("Entered number \(inputNumber) is neon number") } else { print("Entered number \(inputNumber) is not neon number") }

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
reminder = 0
sum = 0
while(81 > 0){
   reminder = 81 % 10 = 1
   sum = 0 + 1 = 1
   squareNumber = 81 / 10 = 8.1 = 8
}
while(8 > 0){
   reminder = 8 % 10 = 8
   sum = 1 + 8 = 9
   squareNumber = 8 / 10 = 0.8 = 0
}

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.

import Foundation import Glibc func checkingNeonNumber(inputNumber : Int) -> Bool{ var squareNumber = inputNumber * inputNumber var sum = 0 var reminder = 0 while(squareNumber > 0){ reminder = squareNumber % 10 sum = sum + reminder squareNumber = squareNumber / 10 } return (sum == inputNumber) } print("Please enter lower limit") var lowLimit = Int(readLine()!)! print("Please enter upper limit") var upperLimit = Int(readLine()!)! print("Neon numbers are:") for j in lowLimit...upperLimit{ if (checkingNeonNumber(inputNumber:j)){ print(j) } }

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: 01-Aug-2022

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements