0% found this document useful (0 votes)
11 views

C Programming Assignment 3

The document contains code for two C programs that use recursion. The first program defines a recursive fibonacci function to calculate the nth Fibonacci number. It is called in a loop to print the Fibonacci series. The second program defines a recursive gcd function that implements Euclid's algorithm to find the greatest common divisor of two numbers. Both functions break down the problems into base cases and recursive calls.

Uploaded by

sambit.222103101
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Programming Assignment 3

The document contains code for two C programs that use recursion. The first program defines a recursive fibonacci function to calculate the nth Fibonacci number. It is called in a loop to print the Fibonacci series. The second program defines a recursive gcd function that implements Euclid's algorithm to find the greatest common divisor of two numbers. Both functions break down the problems into base cases and recursive calls.

Uploaded by

sambit.222103101
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C PROGRAMMING ASSIGNMENT #3

Q1) IT IS REQUIRED TO FIND THE FIBONACCI SERIES OF A NUMBER. DECOMPOSE THE


PROBLEM TO OBTAIN THE SAME USING A RECURSIVE FUNCTION. WRITE A C
PROGRAM TO IMPLEMENT THE LOGIC.

ANSWER:

Here's how we can decompose the problem of finding the Fibonacci series of a number using a recursive
function:

1. Define a recursive function to calculate the nth Fibonacci number.

2. The function should take an integer n as input.

3. If n is less than or equal to 1, return n.

4. If n is greater than 1, recursively call the function with n-1 and n-2, and add the results.

5. Return the sum of the two recursive calls.

CODE:
#include <stdio.h>

int fibonacci(int num)

if (num <= 1) {

return num;

} else {

return fibonacci(num - 1) + fibonacci(num - 2);

int main()

{
int n;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series:\n");

for (int i = 0; i < n; i++) {

printf("%d ", fibonacci(i));

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:

Q2) WRITE A PROGRAM IN C TO FIND THE GCD OF TWO NUMBERS. SYNTHESIZE IT


IN THE MAIN PROGRAM TO FIND GCD OF 2 NUMBERS ENTERED BY THE USER.

ANSWER:

Here's how we can decompose the problem of finding the GCD of two numbers using a recursive function:

1. Define a recursive function to calculate the GCD of two numbers.

2. The function should take two integers a and b as input.

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>

int gcd(int a, int b) {

if (b == 0) {

return a;

} else {

return gcd(b, a % b);

int main() {

int num1, num2, result;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

result = gcd(num1, num2);

printf("GCD of %d and %d is %d\n", num1, num2, result);

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:

You might also like