forked from seeditsolution/cprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happyNumber.c
93 lines (85 loc) · 2.07 KB
/
happyNumber.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <conio.h>
int sqsum (int n)
{
int r,s=0;
while(n!=0)
{
r=n%10;
s+=r*r;
n/=10;
}
return s;
}
int psqsum(int n)
{
int r,s=0;
while(n!=0)
{
r=n%10;
s+=r*r;
n/=10;
printf("%d+",r*r);
}
printf("=%d\n",s);
return s;
}
int main()
{
int num,sum,ch,n1,n2,i;
printf("Select the method : 1.Easy 2.Medium 3.Hard\n");
scanf("%d",&ch);
switch(ch)
{
case 1 :
printf("Enter the number : ");
scanf("%d",&num);
sum = num ;
while(sum>1)
{
sum = sqsum(sum);
if (sum == 4)
break;
}
if (sum==1)
printf("%d is Happy number",num);
if (sum==4)
printf("%d is Unhappy number",num );
break;
case 2 :
printf("Enter the range : \n");
scanf("%d%d",&n1,&n2);
printf("Happy numbers in the given range are : ");
for(i=n1;i<=n2;i++)
{
sum = i ;
while(sum>1)
{
sum = sqsum(sum);
if (sum == 4)
break;
}
if (sum==1)
printf("%d ",i);
}
break;
case 3 :
printf("Enter the number : ");
scanf("%d",&num);
sum = num ;
while(sum>1)
{
sum = psqsum(sum);
if (sum == 4)
break;
}
if (sum==1)
printf("\n%d is Happy number",num);
if (sum==4)
printf("\n%d is Unhappy number",num );
default :
printf("Wrong choice!!!..... Please try again");
break;
}
return 0;
}