-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program To Check Neon Number
65 lines (52 loc) · 1.74 KB
/
Program To Check Neon Number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.
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.
---------------------------------------------------------------------
// 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;
}