Write a program in C++ to find the Happy numbers between 1 to 1000
- برمجة سي بلس بلس
- برمجة
- 2021-05-14
- MarwaMohammed
الأجوبة
#include <bits/stdc++.h>
using namespace std;
int SumOfSquNum(int givno)
{
int SumOfSqr = 0;
while (givno)
{
SumOfSqr += (givno % 10) * (givno % 10);
givno /= 10;
}
return SumOfSqr;
}
bool checkHappy(int chkhn)
{
int slno, fstno;
slno = fstno = chkhn;
do
{
slno = SumOfSquNum(slno);
fstno = SumOfSquNum(SumOfSquNum(fstno));
}
while (slno != fstno);
return (slno == 1);
}
int main()
{
int j,ctr;
cout << "\n\n Find the Happy numbers between 1 to 1000: \n";
cout << " ----------------------------------------------\n";
cout << " The Happy numbers between 1 to 1000 are: "<<endl;
for (j=1;j<=1000;j++)
{
if (checkHappy(j))
cout<<j<<" ";
}
cout <<endl;
}أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال