You are on page 1of 11

Muhammad Souban Javaid FA20-BEE-2C-146

Introduction to Computer Programming CSC141


Spring 2021
Lab 9: Recursive functions and pointers

Lab Tasks:
1. Write a function power (a, b), to calculate the value of a raised to b, using:
(a) non-recursive method
(b) recursion
Solution:
#include<stdio.h>

int power(int a,int b);


int main()
{
int a,b,value;
printf("Insert the value of a and b: ");
scanf("%d%d",&a,&b);
value=power(a,b);
printf("The value of a = %d raised to b = %d is %d",a,b,value);
return 0;
}

int power(int a,int b)


{
int i,value;
value=a;
for(i=1;i<b;i++)
{
value=value*a;
}
return value;
}
Execution:

#include<stdio.h>
void calculate_power(int,int);

void main()
{
int a,b;
printf("Insert the value of a\n");
scanf("%d",&a);
printf("Insert the value of b\n");
scanf("%d",&b);
calculate_power(a,b);
}

void calculate_power(int a,int b)


{
int power=1;
while(b>0)
{
power=power*a;
b--;
}
printf("The value of a raised to b = %d",power);
}

Execution:

By recursive method:
#include <stdio.h>

int power(int a, int b);

int main() {

int a, b, result;

printf("Insert number a: ");


scanf("%d", &a);

printf("Insert number b : ");

scanf("%d", &b);

result = power(a, b);

printf("%d^%d = %d", a,b, result);

return 0;

int power(int a, int b)


{
if (b!= 0)
return (a * power(a, b - 1));
else
return 1;
}
Execution:
2. Write a program to take the values of two integers and use pointers to add 10 to the value of
each integer.
Solution:

Execution:

3. Write a recursive function to obtain the running sum of first 25 natural numbers.
Solution:
#include<stdio.h>
int sum(int num);
int recr_sum(int num);
void main()
{
int z;

printf("Insert Range: ");


scanf("%d", &z);
printf("\nNon-Recursive: Sum of first %d numbers is: %d",z, sum(z));
printf("\nRecursive: Sum of first %d numbers is: %d",z, recr_sum(z));
}
// This function is for non-recursion
int sum(int num)
{
int res=0;
while(num)
{
res = res + num;
num = num-1;
}
return res;
}

int recr_sum(int num)


{
while(num)
{
return (num+sum(num-1));
}
}

Execution:
Home Task:
1. The greatest common divisor of integers x and y is the largest integer that evenly divides both
x and y. Write a recursive function GCD that returns the greatest common divisor of x and y.
Solution:
#include <stdio.h>
int hcf(int x, int y);
int main() {
int x, y;
printf("Insert two positive integers: ");
scanf("%d %d", &x, &y);
printf("G.C.D of %d and %d is %d.", x, y, hcf(x, y));
return 0;
}
int hcf(int x, int y) {
if (y != 0)
return hcf(y, x % y);
else
return x;
}

Execution:
2. Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence.
Solution:
#include<stdio.h>
int fibon(int num);
void main()
{
int num,z=0,j;

printf("Insert number: ");


scanf("%d", &num);

printf("Fibonacci Series:\n");

for(j=1;j<=num;j++)
{
printf("%d\n", fibon(z));
z++;
}
}
int fibon(int num)
{
if(num==0)
{
return 0;
}

else if(num==1)
{
return 1;
}

//fibonacci = 1 1 2 3 5 8
// where n = (n-1) + (n-2)
else
{
return (fibon(num-1)+fibon(num-2));
}
}

Execution:
3. Write a function countEven(int*, int) which receives an integer array and its size and returns
the number of even numbers in the array.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int array1[] = {1, 7, 8, 10, 13, 21, 23};
int arr_size = sizeof(array1)/sizeof(array1[0]);
printf("%d",test(array1, arr_size));
}
int test(int nums[], int arr_size)

{
int evens = 0;
for (int i = 0; i < arr_size; i++)
{
if (nums[i] % 2 == 0) evens++;
}
return evens;

}
Execution:

-----------------------------------------------------------------------------------------------------

You might also like