0% found this document useful (0 votes)
71 views16 pages

Cs Lab Assignment

The document contains 10 programming assignments involving recursion in C language. The assignments include writing programs to find factorial, maximum and minimum elements in an array, sum of even/odd numbers in a range, digit count, matrix multiplication, Fibonacci series, finding the first capital letter in a string, copying a string, printing natural numbers between a range, and printing all elements of an array using recursion.

Uploaded by

20MCE1032 Viraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views16 pages

Cs Lab Assignment

The document contains 10 programming assignments involving recursion in C language. The assignments include writing programs to find factorial, maximum and minimum elements in an array, sum of even/odd numbers in a range, digit count, matrix multiplication, Fibonacci series, finding the first capital letter in a string, copying a string, printing natural numbers between a range, and printing all elements of an array using recursion.

Uploaded by

20MCE1032 Viraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

LAB ASSIGNMENT 9

ROLL NO:-20MCE1032 NAME:-VIRAJ GAONKAR


1. Write a C program to find factorial of any number using recursion.

#include<stdio.h>

long factorial(int n);

int main()

int fact,number;

printf("Enter a number:\n");

scanf("%d",&number);

fact=factorial(number);

printf("The factorial of %d is %ld.\n",number, fact);

return 0;

long factorial(int n)

if(n==0)

return 1;

else if(n>0)

return(n*factorial(n-1));

Output.

Enter a number:
8

The factorial of 8 is 40320

2. Write a C program to find maximum and minimum elements in array

usingrecursion.

#include<stdio.h>

int findmax(int [],int,int);

int findmin(int [],int,int);

int main()

int n,i,maxele,minele;

printf("Enter the size of tha array:\n");

scanf("%d",&n);

int a[n];

printf("\nEnter %d elements:\n",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

minele=findmin(a,0,n);

maxele=findmax(a,0,n);

printf("\nMaximum element of the array:%d\n",maxele);

printf("\nMinimum element of the array:%d\n",minele);

return 0;

int findmax(int arr[], int index, int n)

int max;

if(index>=n-2)

if(arr[index]>arr[index+1])
{

return arr[index];

else

return arr[index+1];

max=findmax(arr,index+1,n);

if(arr[index]>max)

return arr[index];

else

return max;

int findmin(int arr[], int index, int n)

int min;

if(index>=n-2)

if(arr[index]<arr[index+1])

return arr[index];

else

return arr[index+1];

}
}

min=findmin(arr,index+1,n);

if(arr[index]<min)

return arr[index];

else

return min;

Output.

Enter the size of tha array:

Enter 5 elements:

23

34

45

56

576

Maximum element of the array:576

Minimum element of the array:23

3. Write a C program to find sum of all even or odd numbers in given range

using recursion.

#include<stdio.h>

int even_sum(int,int);
int odd_sum(int,int);

int main()

int start, end,sum_even,sum_odd;

printf("Enter the lower limit:\n");

scanf("%d",&start);

printf("Enter the upper limit:\n");

scanf("%d",&end);

sum_even=even_sum(start,end);

printf("The sum of even numbers between %d and %d is %d\n",start,end,sum_even);

sum_odd=odd_sum(start,end);

printf("The sum of odd numbers between %d and %d is %d\n",start,end,sum_odd);

return 0;

int even_sum(int start,int end)

if(start>end)

return 0;

else if(start%2==0)

return(start+even_sum(start+2,end));

else if(start%2==1)

start=start+1;

return(start+even_sum(start+2,end));

int odd_sum(int start,int end)


{

if(start>end)

return 0;

else if(start%2==0)

start=start+1;

return(start+odd_sum(start+2,end));

else if(start%2==1)

return(start+odd_sum(start+2,end));

Output

Enter the lower limit:

Enter the upper limit:

34

The sum of even numbers between 5 and 34 is 300

The sum of odd numbers between 5 and 34 is 285

4. Write a program in C to count the digits of a given number using recursion.

#include<stdio.h>

int digitcount(int n);

int counter=0;

int main()

int n;

int count;
printf("Enter an integer:\n");

scanf("%d",&n);

count=digitcount(n);

printf("\nThe number of digits in %d is %d\n",n,count);

return 0;

}
int digitcount(int n)

if(n>0)

counter=counter+1;

return (digitcount(n/10));

}
else

return (counter);

Output.

Enter an integer:

213432

The number of digits in 213432 is 6

5. Write a program in C to multiply two matrix using recursion.

#include<stdio.h>

void multiplication(int a,int b,int A[10][10],int c,int B[10][10],int C[10][10]);

void disp(int a,int c,int C[10][10]);

int main()

int a,b,c,i,j,k;
int A[10][10],B[10][10],C[10][10]={0};

printf("Enter the number of rows and columns in matrix A:\n");

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

printf("\nEnter the number of rows and columns in matrix B:\n");

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

printf("\nEnter the elements of matrix A:\n");/*horrow vercol*/

for(i=0;i<a;i++)

for(j=0;j<b;j++)

scanf("%d",&A[i][j]);

printf("\nEnter the elements of matrix B:\n");

for(j=0;j<b;j++)

for(k=0;k<c;k++)

scanf("%d",&B[j][k]);

multiplication(a,b,A,c,B,C);

printf("\nThe product of matrix A and B is:\n");

disp(a,c,C);

return 0;
}

void multiplication(int a,int b,int A[10][10],int c,int B[10][10],int C[10][10])

static int i=0,j=0,k=0;/*order of a is ij, b is jk, and c is ik*/


if(i<a)

if(j<b)

if(k<c)

C[i][k]=C[i][k]+A[i][j]*B[j][k];

k++;

multiplication(a,b,A,c,B,C);

k=0;

j++;

multiplication(a,b,A,c,B,C);
}

j=0;

i++;

multiplication(a,b,A,c,B,C);

void disp(int a,int c,int C[10][10])

int i,k;

for(i=0;i<a;i++)

for(k=0;k<c;k++)

printf("%d ",C[i][k]);

printf("\n");

}
Output.

Enter the number of rows and columns in matrix A:

33

Enter the number of rows and columns in matrix B:

33

Enter the elements of matrix A:

111

111

111

Enter the elements of matrix B:

111

111

111

The product of matrix A and B is:

333

333

333

6. Write a C program to find Fibonacci series of given number using recursion.

#include<stdio.h>

int fibonacci(int n);

int main()

{
int i,n,m;

printf("Enter total numbers of the series required:\n");

scanf("%d",&n);

printf("\nThe numbers in the series are:\n");


printf("0 ");

for(i=0;i<n-1;i++)

printf("%d ",fibonacci(m));

m++;

return 0;

int fibonacci(int n)

if(n==0 || n==1)

return n;

else

return fibonacci(n-1)+fibonacci(n-2);

Output.

Enter total numbers of the series required:

40

The numbers in the series are:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465
14930352 24157817 39088169 63245986

7. Write a Program to find the First Capital Letter in a String using recursion.
#include<stdio.h>

#include<string.h>

char all_caps(char *);

int main()

{
char s[20],alpha;

printf("Enter the string:\n");

scanf("%s",&s);

alpha=all_caps(s);

if(alpha==0)

printf("No capital letters are present.");

else if(alpha>=0)

printf("The first capital letter in the string %s is %c",s,alpha);

return 0;

char all_caps(char *s)

int ele;

static int i;

if(i<strlen(s))

for(ele=0;ele<strlen(s);ele++)

if(s[ele]>='A' && s[ele]<='Z')

return s[ele];
}

Output.

Enter the string:

232Balaan

The first capital letter in the string 232Ballan is B

8. Write a Program to copy one string to another string using recursion.

#include<stdio.h>

void copystr(char [],char [],int);

int main()

char s1[1000],s2[1000];

printf("Enter the string to be copied:\n");

scanf("%s",&s1);

printf("\nAfter copying the string.\n");

copystr(s1,s2,0);

printf("\nThe original string is:%s \n",s1);

printf("\nThe copied string is:%s \n",s2);

return 0;

}
void copystr(char s1[],char s2[],int i)

s2[i]=s1[i];

if(s1[i]=='\0')

return ;

copystr(s1,s2,i+1);
}

Output.

Enter the string to be copied:

423*eawW

After copying the string.

The original string is:423*eawW

The copied string is:423*eawW

9. Write a C program to print all natural numbers between 1 to n using recursion.

#include<stdio.h>

void printnumbers( int lower, int upper);

int main()

int lower,upper;

printf("Enter the lower limit:\n");

scanf("%d",&lower);

printf("\nEnter the upper limit:\n");

scanf("%d",&upper);

printf("\nAll the natural numbers from %d to %d are:\n",lower,upper);

printnumbers(lower,upper);

return 0;

void printnumbers( int lower, int upper)

if(lower>upper)

return;
}

printf("%d ",lower);

printnumbers( lower+1, upper);

Output.

Enter the lower limit:

22

Enter the upper limit:

32

All the natural numbers from 22 to 32 are:

22 23 24 25 26 27 28 29 30 31 32

10. Write a C program to display all array elements using recursion.

#include<stdio.h>

void print_array(int a[],int n,int i);

int main()

int n,i;

printf("Enter the size of the array:\n");

scanf("%d",&n);

int a[n];
printf("\nEnter %d elements:\n",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("\nThe elements of the array are:\n");

print_array(a,n,0);

return 0;
}

void print_array(int a[],int n,int i)

if(i>=n)

return;

printf("%d ",a[i]);

print_array( a,n,i+1);

Output.

Enter the size of the array:

Enter 5 elements:

29

32

45

54

68

The elements of the array are:

29 32 45 54 68

You might also like