Happy Number

Guys, I'm back with a new program and it is also in C language. 
So get ready to find your lucky number now..😃


Happy Number..
          Doesn't it seem interesting.. 😉
          Yeah off course it is..!

           What is a happy number. If the repeated sum of digits of  any number is '1', then it is considered to be a happy.. And if it is not then it is sad number. 
         Also when the sum of  digits of a number becomes 4..we cannot get '1' as a result because it requires so many iterations and we remain messed up into an infinite loop.., so if sum of digits is 4 then it is also a sad number.
           Let's take an example.Suppose we have a number 19. 

19 -> 1 and 9 
1^2 + 9^2 = 82
again,
82 -> 8 and 2
8^2 + 2^2 = 68
again,
68-> 6 and 8
6^2 + 8 ^2 = 100
again,
100 -> 1 0 and 0
1^2 + 0^2 + 0 ^2 = 1


Yeeh..we got 1. Thus the number 19 is a happy number.😄

So which one is the sad number.. Let's take an example of it. Examples are always better.
           Suppose we have a number 11.

11 means 1 and 1
1^2 + 1^2 = 2
2^2 = 4 ... 

Ohh..here we get the number 4. Our number '11' is a sad number.😢

So this is the concept of happy number and the sad one.

          Now let's see the program for calculating whether the number you have entered is happy or sad. I've also shown the square operation to help you know the number of iterations required.

#include<stdio.h>
#include<conio.h>
int main()
{
int sum=0,temp=0,i=1;
long num=0;
clrscr();
printf("Enter the number to check whether it is happy or sad!: ");
scanf("%d",&num);
do  // loop for taking sum again as input
{
while(num>=1) //loop for separating and summing up the digits
{
temp = num % 10;
sum =sum+ (temp*temp);
printf("Sum of square operation no %d performed is %d.\n",i,sum); //to show iteration
i++;
num = num /10;
}
if(sum==4) //sad condition;exit
break;

num = sum;
sum=0;
}
while(num>1);
if(num==1)
printf("Your number is happy!");
else
printf("Your number is sad..!");
getch();
return 0;
}

Here's the output of above program:=>
happy number
Output of the above program
           So guys.. I keep updating my blog regularly with new and interesting programs . Follow for getting a notification . Feel free to ask any queries in the comment box below.

Comments

Popular posts from this blog

POLYGON CLIPPING PROGRAM IN C | SUTHERLAND HODGEMAN ALGORITHM FOR POLYGON CLIPPING

Traffic Signal Simulator in C graphics

Floyd's triangle and reverse Floyd's triangle in java