C Programming Assignment 3
C Programming Assignment 3
ANSWER:
Here's how we can decompose the problem of finding the Fibonacci series of a number using a recursive
function:
4. If n is greater than 1, recursively call the function with n-1 and n-2, and add the results.
CODE:
#include <stdio.h>
if (num <= 1) {
return num;
} else {
int main()
{
int n;
scanf("%d", &n);
printf("Fibonacci Series:\n");
return 0;
In this program, the fibonacci() function is a recursive function that takes an integer num as an argument and
returns the numth Fibonacci number. The function checks if num is less than or equal to 1, and if it is, returns
num. If num is greater than 1, the function recursively calls itself with num-1 and num-2, and returns the sum
of those two values.
In the main() function, the program asks the user to enter the number of terms they want in the Fibonacci
series. It then loops through the series, calling fibonacci() with the index of the current term and printing the
result.
OUTPUT:
ANSWER:
Here's how we can decompose the problem of finding the GCD of two numbers using a recursive function:
3. If b is equal to 0, return a.
4. Otherwise, recursively call the function with b and a % b, and return the result.
CODE:
#include <stdio.h>
if (b == 0) {
return a;
} else {
int main() {
return 0;
In this program, the gcd() function is a recursive function that takes two integers a and b as arguments and
returns their greatest common divisor. The function uses Euclid's algorithm to calculate the GCD recursively. If
b is equal to 0, the function returns a. Otherwise, it recursively calls itself with b and a % b, and returns the
result.
In the main() function, the program asks the user to enter two numbers. It then calls the gcd() function with
those numbers and stores the result in the result variable. Finally, it prints the result to the console.
OUTPUT: