About:
The process in which a function calls itself is called recursion and the corresponding function is called as recursive function.
For example: In below program, gcd() is used for calculating gcd of n1 and n2 and we are calling the same function again and again until the value of n2 is equal to 0.
Program:
The process in which a function calls itself is called recursion and the corresponding function is called as recursive function.
For example: In below program, gcd() is used for calculating gcd of n1 and n2 and we are calling the same function again and again until the value of n2 is equal to 0.
Program:
#include <stdio.h>
int gcd(int n1, int n2) {
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
void main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
}
No comments:
Post a Comment