-
Notifications
You must be signed in to change notification settings - Fork 0
/
week 6 in c
82 lines (60 loc) · 1.65 KB
/
week 6 in 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
<1> Functions in C
Task
Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
Note
There is not built in max function in C. Code that will be reused is often put in a separate function, e.g. int max(x, y) that returns the greater of the two values.
Input Format
Input will contain four integers - , one on each line.
Output Format
Print the greatest of the four integers.
Note: I/O will be automatically handled.
Sample Input
3
4
6
5
Sample Output
6
#include <stdio.h>
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
<2> Calculate the Nth term
Task
There is a series, , where the next term is the sum of pervious three terms. Given the first three terms of the series, , , and respectively, you have to output the nth term of the series using recursion.
Recursive method for calculating nth term is given below.
Input Format
The first line contains a single integer, .
The next line contains 3 space-separated integers, , , and .
Output Format
Print the nth term of the series, .
Sample Input 0
5
1 2 3
Sample Output 0
11
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int find_nth_term(int n, int a, int b, int c) {
if(n==1)
return a;
else if(n==2)
return b;
else if(n==3)
return c;
else
return find_nth_term(n-1,a,b,c)+find_nth_term(n-2,a,b,c)+find_nth_term(n-3,a,b,c);
}
int main() {
int n, a, b, c;
scanf("%d %d %d %d", &n, &a, &b, &c);
int ans = find_nth_term(n, a, b, c);
printf("%d", ans);
return 0;
}