-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprog1.c
More file actions
52 lines (45 loc) · 925 Bytes
/
prog1.c
File metadata and controls
52 lines (45 loc) · 925 Bytes
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
#include<stdio.h>
int search(int arr[], unsigned int arr_len, int x){
for (unsigned int i = 0; i < arr_len; i++)
if (arr[(unsigned int)i] == x)
return (int)i;
return -1;
}
int factorial(int num);
int verifyFactorial(int num){
int fact = 1;
if(num < 0){
fact = -1;
}
else if(num > 1){
do{
fact = fact * num;
num = num - 1;
}while(num>1);
}
else {
fact = 1;
}
return fact;
}
void main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
int num = 6;
int result = search(arr, 6, num);
printf("%d\n", result);
result = factorial(num);
printf("%d\n", result);
}
//This should be linked to its function declaration
int factorial(int num){
int copyNum = num;
int fact = 1;
if(num < 0)
fact = -1;
else{
while(num > 1){
fact = fact * num--;
}
}
return fact==verifyFactorial(copyNum)? fact:-2;
}