C++ Exercises: Find the Happy numbers between 1 to 1000
C++ Numbers: Exercise-17 with Solution
Write a C++ program to find the Happy numbers between 1 and 1000.
Sample Solution:
C++ Code :
#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;
}
Sample Output:
Find the Happy numbers between 1 to 1000: ---------------------------------------------- The Happy numbers between 1 to 1000 are: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139 167 176 188 190 19 2 193 203 208 219 226 230 236 239 262 263 280 291 293 301 302 310 313 319 320 326 329 331 338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440 446 464 469 478 487 490 496 536 556 563 565 566 608 617 622 623 632 635 637 638 644 649 653 655 656 665 671 673 680 683 694 700 709 716 736 739 748 7 61 763 784 790 793 802 806 818 820 833 836 847 860 863 874 881 888 899 901 904 907 910 912 913 921 92 3 931 932 937 940 946 964 970 973 989 998 1000
Flowchart:



C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to check a number is a Happy or not.
Next: Write a program in C++ to check whether a number is Disarium or not.
What is the difficulty level of this exercise?
C++ Programming: Tips of the Day
Program being compiled differently in 3 major C++ compilers. Which one is right?
GCC is correct, at least according to C++11 lookup rules. 3.4.3.1 [class.qual]/2 specifies that, if the nested name specifier is the same as the class name, it refers to the constructor not the injected class name. It gives examples:
B::A ba; // object of type A A::A a; // error, A::A is not a type name struct A::A a2; // object of type A
It looks like MSVC misinterprets it as function-style cast expression creating a temporary C with y as a constructor parameter; and Clang misinterprets it as a declaration of a variable called y of type C.
Ref: https://bit.ly/3ww1Bna
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook