C++ Program to Check Neon Numbers in a Given Range

11/04/2024 0 By indiafreenotes

A Neon number is an intriguing concept in the realm of mathematics and computer science. It is a number where the sum of the digits of its square equals the number itself. For instance, 9 is a neon number because 9^2 = 81, and the sum of the digits of 81 (8 + 1) equals 9.

Understanding Neon Numbers

To identify neon numbers, one must perform the following steps for each number in the given range:

  • Square the Number:

First, compute the square of the number.

  • Sum the Digits:

Next, calculate the sum of the digits of the squared number.

  • Compare with Original:

Finally, check if the sum of the digits equals the original number. If so, the number is a neon number.

This process involves fundamental operations like multiplication and digit extraction, the latter typically achieved through division and modulus operations.

Algorithm for Finding Neon Numbers in a Range

Before implementing the program, let’s outline the algorithm to find and display all neon numbers within a specified range:

  1. Input Range: Obtain the lower and upper bounds of the range from the user.
  2. Iterate Through the Range: For each number in the range, perform the following steps:
    • Square the number.
    • Calculate the sum of the digits of the square.
    • Check if the sum equals the original number.
    • If so, display the number as a neon number.
  3. Repeat until all numbers in the range have been checked.

Implementing the Program

Now, let’s translate the above algorithm into a C++ program:

#include <iostream>

using namespace std;

// Function to calculate the sum of digits of a number

int sumOfDigits(int n) {

    int sum = 0;

    while (n > 0) {

        sum += n % 10; // Add the rightmost digit to sum

        n /= 10;       // Remove the rightmost digit from n

    }

    return sum;

}

// Function to check if a number is a Neon Number

bool isNeon(int num) {

    int square = num * num; // Calculate square of num

    int sum = sumOfDigits(square); // Sum the digits of the square

    return sum == num; // Return true if sum of digits equals num

}

int main() {

    int lower, upper;

    // Prompt user for the range

    cout << “Enter the lower and upper bounds of the range: “;

    cin >> lower >> upper;

    cout << “Neon numbers between ” << lower << ” and ” << upper << ” are:” << endl;

    // Iterate through the range and check for Neon Numbers

    for (int i = lower; i <= upper; ++i) {

        if (isNeon(i)) {

            cout << i << ” “;

        }

    }

    return 0;

}

 

Program Explanation

  1. Sum of Digits Function (sumOfDigits):

This helper function takes an integer n and returns the sum of its digits. It does so by repeatedly extracting the rightmost digit (using n % 10), adding it to the sum, and then removing this digit from n (using n /= 10).

  1. Neon Number Checker Function (isNeon):

The isNeon function checks whether a given number is a neon number. It squares the number, uses sumOfDigits to calculate the sum of the digits of the square, and then checks if this sum equals the original number.

  1. Main Function:

After obtaining the range from the user, the program iterates through each number in this range, using the isNeon function to determine if it is a neon number. If a number is found to be neon, it is printed to the console.