Tutorial 10 Solution - Recursive Functions
Tutorial 10 Solution - Recursive Functions
Q1. [CO4] A child is taking his daily lessons on mathematics from online classes. Today he is
learning about the counting of stars. He starts counting as one star, two stars, three stars …
then 49 stars and finally 50 stars. Help the child in summing up his counting of the number of
stars using recursive function.
Sol:
#include <stdio.h>
intstars(int N)
{
if(N != 0)
{
returnN + stars(N-1);
}
else
return N;
}
int main()
{
int N = 50; ### N: No. of stars that a child counts
printf("Sum of %d Stars is %d\n", N, stars(N));
return 0;
}
Q2. [CO4] Write a program in C to print even or odd numbers in a given range using recursive
function.
Sol:
#include <stdio.h>
void EvenAndOdd(int stVal, int n);
int main()
{
int n;
printf("\n\n Recursion : Print even or odd numbers in a given range :\n");
printf("-------------------------------------------------------------\n");
Q3. [CO4] Write a program in C to check if a number is a prime number or not using recursion.
Sol:
#include<stdio.h>
int checkForPrime(int);
int i;
int main()
{
int n1,primeNo;
#include<stdio.h>
void copyString(char [], char [], int);
int main()
{
char stng1[20], stng2[20];
printf("\n\n Recursion : Copy One string to another :\n");
printf("---------------------------------------------\n");
Q5 [CO4] Write a program in C to find the largest element of an array using recursion.
Sol:
#include<stdio.h>
#define MAX 100
int MaxElem(int []);
int n;
int main()
{
int arr1[MAX],hstno,i;
printf("\n\n Recursion : Get the largest element of an array :\n");
printf("------------------------------------------------------\n");