C program that check whether given number is Happy or not - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Wednesday, November 27, 2019

C program that check whether given number is Happy or not - My CS Tutorial

C program to check that given number is Happy  or not.

In this tutorial we will learn how to write a program that check given number is Happy or not.






EXPLANATION:-


A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1.

If it is not happy number it will end at '4'. 

For Ex:- To check whether 19 is happy or not 

19=1^2+9^2=82 then we take 82 
82=8^2+2^2=68 then again we take 65
68=6^2+8^2=100
100=1^2+0^2+0^2=1
So it is Happy Number

If the number is not happy then it will end at 4. 



C program code


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int i,j,num,temp,sum=0;
 printf("\n Enter a number:\n ");
 scanf("%d",&num);
  while(sum!=1 && sum!=4)
  {
   sum=0;
   while(num>0)
  {
    j=num%10;
    sum+=(j*j);
    num=num/10;
  }
  num=sum;
  }

 if(sum==1)
 printf("\n Given number is a Happy Number.\n");
 else
 printf("\n Given number is an UnHappy Number.\n");
 getch();
}



OUTPUT 1:






OUTPUT 2:







In first output,the value of number is 763

The given number is a Happy number.


In second output,the value of number is 200

The given number is an Unhappy number.



Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.

Created by-- HARSH CHAUHAN


No comments:

Post a Comment