Given a number (num) we need to check whether it is a Neon Number ( i.e. a number where the sum of digits of the square of the number is equal to the number ) and return “true” or “false” accordingly.
Example:
Input: num = 9 Output: true
Explanation: square of 9 is 9 * 9 = 81 , sum of digit of square is 8 + 1 = 9 (i.e equal to given number).
Input: num = 10 Output: false
Explanation: Square of 10 is 10 * 10 = 100 , sum of digit of square is 1 + 0 + 0 = 1 (i.e. not equal to given number).
Approach
The basic idea is to first calculate the square of the number and then by calculating the sum of digits of the square we can check whether the given number is Neon Number or not.
Algorithm
- At first, we need to find the square of the given number.
- Then we need to extract the digits of the square of the number using a loop.
- Then we need to calculate the sum of digits.
- At last, we need to check the sum with the given number, and the Outcome will be:
- ” true ” , if the sum is equal to the given number, or
- ” false ” , if the sum is not equal to the given number.
Example:
// C program to demonstrate whether // a number is Neon number or not #include <stdio.h> int Check_Neon_Number( int num) {
// Calculating the square of the number
int square = num * num;
// Copying the square in a variable
// to extract the digit
int n = square;
// Declaring a variable to store the digits
int digit;
// Initializing a variable to
// calculate the sum of digits
int sum = 0;
// To calculate the sum of digits
while (n != 0) {
// Extracting the digit
digit = n % 10;
sum = sum + digit;
n = n / 10;
}
// Checking the condition of a Neon Number
if (sum == num)
return 1; // If condition is true.
else
return 0; // If condition is false.
} // Driver Code int main()
{ int num = 9;
// Calling the function
int ans = Check_Neon_Number(num);
if (ans == 1)
// The number is Neon
printf ( "true" );
else
// The number is not Neon
printf ( "false" );
return 0;
} |
true
Time Complexity: O(log n)
Auxiliary Space: O(1) as no extra space is required, so space is constant