-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRangeStrong.c
More file actions
88 lines (70 loc) · 1.43 KB
/
RangeStrong.c
File metadata and controls
88 lines (70 loc) · 1.43 KB
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
// this code is to find strong numbers :- a number whose all elements factorial's sum is equal to that number
// like 145 = 1! + 4! + 5! = 1 + 24 + 120
#include <stdio.h>
int fact(int x);
int strong(int a);
// function to print alternative strong numbers.
void Alter_Strong(int n);
// function to print strong number in given range.
void Print_Strong(int n);
int main()
{
// n is range upto we want num to find;
int n;
printf("Enter n : ");
scanf("%d", &n);
printf("Strong numbers : ",n);
Print_Strong(n);
printf("Alternate : ",n);
Alter_Strong(n);
return 0;
}
// to find factorial of each num;
int fact(int x)
{
if (x == 0 || x == 1)
{
return 1;
}
else
{
return x * fact(x - 1);
}
}
// to return a num like strong num;
int strong(int a)
{
int y = 0;
while (a > 0)
{
y += fact(a % 10);
a = a / 10;
}
return y;
}
// alternative strong num;
void Alter_Strong(int n){
int j = 1;
for (int i = 1; i <= n; i++)
{
if (i == strong(i))
{
if (j % 2 != 0)
{
printf("%d ", i);
}
j++;
}
}
}
// to check if num is strong num or not if yes than it will be printed on terminal;
void Print_Strong(int n){
for (int i = 1; i <= n; i++)
{
if (i == strong(i))
{
printf("%d ", i);
}
}
printf("\n");
}