This is a C Program to find gcd of given numbers using recursion.
This C program, using recursion, finds the GCD of the two numbers entered by the user.
The user enters two numbers by using a space in between them or by pressing enter after each input.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to find GCD of given Numbers using Recursion */ #include <stdio.h> int gcd(int, int); int main() { int a, b, result; printf("Enter the two numbers to find their GCD: "); scanf("%d%d", &a, &b); result = gcd(a, b); printf("The GCD of %d and %d is %d.\n", a, b, result); } int gcd(int a, int b) { while (a != b) { if (a > b) { return gcd(a - b, b); } else { return gcd(a, b - a); } } return a; }
In this C Program, we are reading the two integer numbers using ‘a’ and ‘b’ variable. The gcd() function is used to find the GCD of two entered integers using recursion.
While loop is used to check that both the ‘a’ and ‘b’ variable values are not equal. If the condition is true then execute the loop. Otherwise, if the condition is false return the value of ‘a’ variable. If else condition statement is used to check the value of ‘a’ variable is greater than the value of ‘b’ variable.
If the condition is true, then return two integer variable values. Otherwise, if the condition is false, then execute else statement and return the values of two integer variable. Print the GCD of a given number using printf statement.
$ gcc gcd_recr.c -o gcd_recr $ a.out Enter the two numbers to find their GCD: 100 70 The GCD of 100 and 70 is 10.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos