Functions Program
Functions Program
2.To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
int recgcd(int x, int y);
int nonrecgcd(int x, int y);
void main()
{
int a, b, c, d;
printf("Enter two numbers a and b");
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);
}
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);
}
#include <stdio.h>
int rpower(int , int);
void power(int,int)
int main() {
int base, p, result;
printf("Enter base number:\n ");
scanf("%d", &base);
printf("Enter power number(positive integer):\n ");
scanf("%d", &p);
result = power(base, p);
printf("%d^%d = %d\n", base, a, result);
power(base,p);
return 0;
}
void power(int base,int p)
{
int i,re=1;
for(i=1;i<=p;i++)
re=re*base;
printf("%d^%d = %d\n", base, a, re);
}
int power(int base, int p) {
if (p != 0)
return (base * power(base, p - 1));
else
return 1;
}