Q:

Write a program in C++ to check a number is a Happy or not

0

Write a program in C++ to check a number is a Happy or not

Sample Output:

 Check whether a number is Happy number or not:                                                      
 ---------------------------------------------------                                                 
 Input a number: 23                                                                                  
23 is a Happy number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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 hyno;
 cout << "\n\n Check whether a number is Happy number or not: \n";
 cout << " ---------------------------------------------------\n";
 cout << " Input a number: ";
 cin >> hyno;

    if (checkHappy(hyno))
        cout << hyno << " is a Happy number\n";
    else
        cout << hyno << " is not a Happy number\n";
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now