-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNext Happy Number.cpp
More file actions
48 lines (47 loc) · 711 Bytes
/
Next Happy Number.cpp
File metadata and controls
48 lines (47 loc) · 711 Bytes
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
#include<iostream>
using namespace std;
bool ishappyNo(int n)
{
if(n==1)
return true;
else if(n>9)
{
int sum=0;
while(n!=0)
{
int x=n%10;
sum+=(x*x);
n=n/10;
}
if(ishappyNo(sum))
return true;
else
return false;
}
return false;
}
void nextsmallest(int n)
{
int i=n+1;
while(true)
{
if(ishappyNo(i)==true)
{
cout<<i;
break;
}
i++;
}
}
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
nextsmallest(n);
cout<<endl;
}
return 0;
}