forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neon_number.cpp
51 lines (48 loc) · 1.09 KB
/
neon_number.cpp
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
/*
A neon number is a number where :-
the sum of digits of square of the number is equal to the number.
The task is to check and print neon numbers in a range given by the user.
*/
#include <bits/stdc++.h>
using namespace std;
//Function that prints the sum of digits of a number
int sum_of_digits(int y)
{
int s = 0,v;
while (y != 0)
{
v=y%10;
s = s + v;
y = y / 10;
}
return s;
}
bool Neon(int n)
{
int sq = pow(n,2);
int result = sum_of_digits(sq);
//If sum of digits become equal to the number
if (result == n)
return true;
else
return false;
}
int main()
{
cout << "Enter the range:";
int a, b;
cin >> a >> b;
// Printing Neon Numbers according to the range
cout<<"Neon numbers in between "<<a<<" to "<<b<<" are : ";
for (int i = a; i <= b; i++)
if (Neon(i) == true)
cout << i << " ";
}
/*
Input: Enter the range:1 10000
Output: Neon numbers in between 1 to 10000 are : 1 9
*/
/*
Time Complexity:O(nlogn) //where n is the range of a to b.
Space Complexity:O(1)
*/