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


Expected Output :
Input a number: 13
13 is a Happy number.

All Answers

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

# include <stdio.h>
# include <math.h>
# include <stdlib.h>
# include <stdbool.h>


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;
	printf("\n\n Check whether a number is Happy number or not: \n");
	printf(" ---------------------------------------------------\n");
	printf(" Input a number: ");
	scanf("%d",&hyno);

    if (checkHappy(hyno))
	printf("%d is a Happy number.\n",hyno);
    else
	printf("%d is not a Happy number.\n",hyno);
} 

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