0% found this document useful (0 votes)
6 views4 pages

C Programs for GCD, Factorial, Fibonacci

The document provides C programs to find the GCD of two integers using both iterative and recursive functions, as well as to demonstrate recursion through calculating factorials and generating the Fibonacci sequence. It includes function prototypes, main functions, and the implementation of the GCD, factorial, and Fibonacci functions. The programs prompt the user for input and display the results accordingly.

Uploaded by

5485kartheekssk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

C Programs for GCD, Factorial, Fibonacci

The document provides C programs to find the GCD of two integers using both iterative and recursive functions, as well as to demonstrate recursion through calculating factorials and generating the Fibonacci sequence. It includes function prototypes, main functions, and the implementation of the GCD, factorial, and Fibonacci functions. The programs prompt the user for input and display the results accordingly.

Uploaded by

5485kartheekssk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Week 8:

a) Write program to find GCD of two integers using non-recursive function


and recursive function
b) Write a C program to demonstrate recursion by finding the factorial and
Fibonacci sequence.

a) Write program to find GCD of two integers using non-recursive function


and recursive function

#include <stdio.h>

// Function prototype declarations

int gcd_iterative(int a, int b);

int gcd_recursive(int a, int b);

main()

int num1, num2;

printf("Enter two integers: ");

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

// Calling the iterative function

printf("GCD (Iterative) of %d and %d = %d\n", num1, num2,


gcd_iterative(num1, num2));

// Calling the recursive function

printf("GCD (Recursive) of %d and %d = %d\n", num1, num2,


gcd_recursive(num1, num2));

return 0;

}
// ---------------- Iterative GCD Function ----------------

int gcd_iterative(int a, int b)

int temp;

while (b != 0)

temp = b;

b = a % b;

a = temp;

return a;

// ---------------- Recursive GCD Function ----------------

int gcd_recursive(int a, int b)

if (b == 0)

return a;

else

return gcd_recursive(b, a % b);

}
b) Write a C program to demonstrate recursion by finding the factorial
and Fibonacci sequence.

#include <stdio.h>

// Function prototypes

int factorial(int n);

int fibonacci(int n);

main()

int num, terms;

// Factorial

printf("Enter a number to find factorial: ");

scanf("%d", &num);

printf("Factorial of %d = %d\n", num, factorial(num));

// Fibonacci

printf("\nEnter number of terms for Fibonacci sequence: ");

scanf("%d", &terms);

printf("Fibonacci Sequence: ");

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

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

return 0;

}
// ---------------- Recursive Factorial ----------------

int factorial(int n)

if (n == 0 || n == 1)

return 1;

else

return n * factorial(n - 1);

// ---------------- Recursive Fibonacci ----------------

int fibonacci(int n)

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

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

You might also like