Write C programs that use both recursive and non-recursive functions

i.

To find the factorial of a given integer.

Program:

#include <stdio.h>

#include <conio.h>

void main()

{

int n, a, b;

clrscr();

printf("Enter any number\n");

scanf("%d", &n);

a = recfactorial(n);

printf("The factorial of a given number using recursion is %d \n", a);

b = nonrecfactorial(n);

printf("The factorial of a given number using nonrecursion is %d ", b);

getch();

}

int recfactorial(int x)

{

int f;

if(x == 0)

{

return(1);

}

else

{

f = x * recfactorial(x - 1);

return(f);

}

}

int nonrecfactorial(int x)

{

int i, f = 1;

for(i = 1;i <= x; i++)

{

f = f * i;

}

return(f);

}

Output:


ii.

To find the GCD (greatest common divisor) of two given integers.

Program:

#include <stdio.h>

#include <conio.h>

void main()

{

int a, b, c, d;

clrscr();

printf("Enter two numbers a, b\n");

scanf("%d%d", &a, &b);

c = recgcd(a, b);

printf("The gcd of two numbers using recursion is %d\n", c);

d = nonrecgcd(a, b);

printf("The gcd of two numbers using nonrecursion is %d", d);

getch();

}

int recgcd(int x, int y)

{

if(y == 0)

{

return(x);

}

else

{

return(recgcd(y, x % y)

}

}

int nonrecgcd(int x, int y)

{

int z;

while(x % y != 0)

{

z = x % y;

x = y;

y = z;

}

return(y);

}


output: