forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Neon number added (HarshCasper#3050)
* Neon number added * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp * Update neon_number.cpp Co-authored-by: ankan1811 <[email protected]>
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
*/ |