ODD 2023
Tutorial Sheet –11
Software Development Fundamentals – I (15B11CI111)
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");
printf(" Input the range to print starting from 1 : ");
scanf("%d", &n);
printf("\n All even numbers from 1 to %d are : ", n);
EvenAndOdd(2, n);//call the function EvenAndOdd for even numbers
printf("\n\n All odd numbers from 1 to %d are : ", n);
EvenAndOdd(1, n);// call the function EvenAndOdd for odd numbers
printf("\n\n");
return 0;
}
void EvenAndOdd(int stVal, int n)
{
if(stVal > n)
return;
printf("%d ", stVal);
EvenAndOdd(stVal+2, n);//calling the function EvenAndOdd itself recursively
}
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;
printf("\n\n Recursion : Check a number is prime number or not :\n");
printf("--------------------------------------------------------\n");
printf(" Input any positive number : ");
scanf("%d",&n1);
i = n1/2;
primeNo = checkForPrime(n1);//call the function checkForPrime
if(primeNo==1)
printf(" The number %d is a prime number. \n\n",n1);
else
printf(" The number %d is not a prime number. \nn",n1);
return 0;
}
int checkForPrime(int n1)
{
if(i==1)
{
return 1;
}
else if(n1 %i==0)
{
return 0;
}
else
{
i = i -1;
checkForPrime(n1);//calling the function checkForPrime itself recursively
}
}
Q4 [CO4] Write a program in C to copy one string to another using recursion.
Sol:
#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");
printf(" Input the string to copy : ");
scanf("%s", stng1);
copyString(stng1, stng2, 0);
printf("\n The string successfully copied.\n\n");
printf(" The first string is : %s\n", stng1);
printf(" The copied string is : %s\n\n", stng2);
return 0;
}
void copyString(char stng1[], char stng2[], int ctr)
{
stng2[ctr] = stng1[ctr];
if (stng1[ctr] == '\0')
return;
copyString(stng1, stng2, ctr + 1);
}
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");
printf(" Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf(" Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i);
scanf("%d",&arr1[i]);
}
hstno=MaxElem(arr1);//call the function MaxElem to return the largest element
printf(" Largest element of the array is: %d\n\n",hstno);
return 0;
}
int MaxElem(int arr1[])
{
static int i=0,hstno =-9999;
if(i < n)
{
if(hstno<arr1[i])
hstno=arr1[i];
i++;
MaxElem(arr1);//calling the function MaxElem itself to compare with further element
}
return hstno;
}