forked from hermanwongkm/CS1010
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HappyNumber.c
56 lines (41 loc) · 1.52 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
#include <stdio.h>
#include <math.h>
int computeHappyNumbers(int,int);
int computeFinalForm(int);
int main(void){
int lower1, upper1, lower2, upper2, number1, number2;
printf("Enter first range: ");
scanf("%d %d",&lower1,&upper1);
printf("Enter second range: ");
scanf("%d %d",&lower2, &upper2);
number1 = computeHappyNumbers(lower1,upper1);
number2 = computeHappyNumbers(lower2,upper2);
printf("The numbers of happy numbers in the two ranges are: %d %d\n", number1, number2);
if(number1 > number2)
printf("There are more happy numbers in the first range.\n");
else if(number1 < number2)
printf("There are more happy numbers in the second range.\n");
else
printf("The numbers of happy numbers in both ranges are the same.\n");
return 0;
}
int computeHappyNumbers(int lower, int upper){
int i,num, counter = 0,squaredSum;
for(i = lower; i <= upper; i++){
squaredSum = i;
while(squaredSum != 0 && squaredSum != 1 && squaredSum != 4 && squaredSum != 16 && squaredSum != 20 && squaredSum != 37 && squaredSum != 42 && squaredSum != 58 && squaredSum != 89 && squaredSum != 145 ){
squaredSum = computeFinalForm(squaredSum);
}
if(squaredSum == 1)
counter++;
}
return counter;
}
int computeFinalForm(int num){
int squaredSum = 0;
while (num >0){
squaredSum = squaredSum + pow(num%10,2);
num = num/10;
}
return squaredSum;
}