(Q). A happy number is defined as:
Take a positive number and replaced number by
the sum of square of it's digit.Repeat the process
untill the number equals 1.If the number ends
With 1 then it is called happy number.Write a
Program to input a number and check whether
It a "Happy number" or not. The program display a message accordingly.
Sample input=31
Solution=31=3^2+1^2=10=1^2+0=0.
Sample output ="Happy number".
Solution:
#include<stdio.h>
#include<string.h>
void main()
{
int n,r,sum=0,s=0; //s=no. Of digit
printf("enter a number");
scanf("%d",&n);
while(1)
{
while(n!=0)
{
r=n%10;
n=n/10;
sum=sum+r*r;
s=s+1;
}
if(s==1)
break;
else
s=0;
n=sum;
sum=0;
}
if(s==1)
printf("happy number");
else
printf("not a happy number");
getch();
}
Take a positive number and replaced number by
the sum of square of it's digit.Repeat the process
untill the number equals 1.If the number ends
With 1 then it is called happy number.Write a
Program to input a number and check whether
It a "Happy number" or not. The program display a message accordingly.
Sample input=31
Solution=31=3^2+1^2=10=1^2+0=0.
Sample output ="Happy number".
Solution:
#include<stdio.h>
#include<string.h>
void main()
{
int n,r,sum=0,s=0; //s=no. Of digit
printf("enter a number");
scanf("%d",&n);
while(1)
{
while(n!=0)
{
r=n%10;
n=n/10;
sum=sum+r*r;
s=s+1;
}
if(s==1)
break;
else
s=0;
n=sum;
sum=0;
}
if(s==1)
printf("happy number");
else
printf("not a happy number");
getch();
}
3 Comments
Good one
ReplyDelete👌👌👌👌
ReplyDeleteNice program
ReplyDelete