## 10591 - Happy Number

Moderator: Board moderators

shafin_ohin
New poster
Posts: 2
Joined: Tue Jan 06, 2015 1:54 pm

### Re: 10591 - Happy Number

Why RE??

Code: Select all

``````#include<iostream>
#include<cstdio>

using namespace std;

long long int sds(long long int a)
{
long long int sum=0;
while(a!=0)
{
sum+=(a%10)*(a%10);
a/=10;
}
return(sum);
}

int main()
{
long long int x,c,temp,n=0,uh=0,k=1,ara[100000],i=0;
scanf("%lld",&c);
long long int tmp=c;
for(int h=0;h<tmp;h++)
{
cin>>x;
temp=x;
long long int tm=temp;
while(sds(x)!=1)
{
x=sds(x);
ara[i++]=x;
n+=1;
for(int j=0;j<i-1;j++)
if(ara[j]==x)
{
uh=1;
break;
}
if(temp==x)
{
uh=1;
break;
}

}
if(uh!=1)
printf("Case #%lld: %lld is a Happy number\n",k++,tm);
else
printf("Case #%lld: %lld is an Unhappy number\n",k++,tm);
n=0;
uh=0;
i=0;
}
return(0);
}``````
Last edited by brianfry713 on Fri Jan 09, 2015 12:11 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 10591 - Happy Number

When I use gdb and g++ -O2 on your code, for input:

Code: Select all

``````2
1
2``````
Your code throws a seg fault on line 30: ara[i++]=x;
When I print i it returns <value optimized out>
Check input and AC output for thousands of problems on uDebug!