0% found this document useful (0 votes)
20 views17 pages

Assignment9 2105595

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)
20 views17 pages

Assignment9 2105595

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/ 17

Name :- Yash Priyam

Roll No. – 2105595

Q1: Write a program in C to print first 50 natural numbers using recursion

PROGRAM:
#include<stdio.h>
int x=1;
void PRINT(){
if (x<=50){
printf("%d\t",x);
x++;
PRINT(x);
}
else {
}
}
int main(){
PRINT();
return 0;
}

OUTPUT:
Q2: Write a program in C to calculate the sum of numbers from 1 to n using
recursion.

PROGRAM:
#include <stdio.h>

int Sum (int n){

if(n>0){

return(n+Sum(n-1));

else{

return 0;

}
}

int main()

{ int n,sum;

printf("Enter a number: ");

scanf("%d",&n);

sum = Sum(n);

printf("The sum of numbers upto %d is %d",n,sum);

return 0;

OUTPUT:

Q3: Write a program in C to print the array elements using recursion.

PROGRAM:
#include <stdio.h>

int count=0;
void Print(int arr[],int n){

if(count<n){

printf("%d\t",arr[count]);

count++;

Print(arr,n);

else{

int main()

{ int n;

printf("Enter number of elements in array: ");

scanf("%d",&n);

int arr[n];

printf("Enter value of array\n");

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

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

printf("The given array is: ");

Print(arr,n);

return 0;

}
OUTPUT:

Q4: Write a program in C to get the largest element of an array using recursion.
PROGRAM:
#include <stdio.h>

int count=1,m;

int max(int arr[],int n){

if(count<n){

if(m<arr[count]){

m= arr[count];

count++;

max(arr,n);

else{

return m;
}

int main()

{ int n;

printf("Enter number of elements in array: ");

scanf("%d",&n);

int arr[n];

printf("Enter value of array\n");

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

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

m=arr[0];

int large=max(arr,n);

printf("Largest element of the array is %d",large);

return 0;

OUTPUT:
Q5: Write a program in C to check a number is a prime number or not using
recursion.
PROGRAM:
#include <stdio.h>

int count=2;

void prime(int n){

if(n==1){

printf("Not prime");

return ;

if(count<=n/2){

if(n%count==0){

printf("Not prime");

return ;

else{

count++;

prime(n);
}

else{

printf("Prime");

return ;

int main()

{ int n,p;

printf("Enter a number: ");

scanf("%d",&n);

prime(n);

return 0;

OUTPUT:
Q6: Write a program in C to print even or odd numbers in given range using
recursion.
PROGRAM:
#include <stdio.h>

int count ;

void even(int start,int end){

if(count<=end){

if(count%2==0){

printf("%d\t",count);

count++;

even(start,end);

else{

return ;

void odd(int start,int end){

if(count<=end){

if(count%2!=0){

printf("%d\t",count);

count++;

odd(start,end);

}
else{

return ;

int main()

{ int start,end,display;

printf("Enter range of numbers in the format x-y : ");

scanf("%d-%d",&start,&end);

count = start;

printf("Enter 0 for even numbers in the given range or 1 for odd numbers in the given range: ");

scanf("%d",&display);

if(display==0){

printf("Even numbers in the given range are: ");

even(start,end);

else if(display==1){

printf("Odd numbers in the given range are: ");

odd(start,end);

else{

printf("Enter a valid input");

return 0;

OUTPUT:
Q7: Write a program in C to find the LCM of two numbers using recursion.

PROGRAM:
#include <stdio.h>

int count ;

int lcm(int n1,int n2){

if(count%n1==0&&count%n2==0){

return count;

else{

count++;

lcm(n1,n2);

}
int main()

{ int n1,n2,l;

printf("Enter two numbers : ");

scanf("%d %d",&n1,&n2);

count =n1;

l = lcm(n1,n2);

printf("LCM of %d and %d is %d",n1,n2,l);

return 0;

OUTPUT:

Q8: WAP to find out the sum of n elements of an integer array a[] by using
recursion.
PROGRAM:
#include<stdio.h>

int count=0,m=0;
int sum(int arr[],int n){

if(count<n){

m=m+arr[count];

count++;

sum(arr,n);

else{

return m;

int main()

{ int n;

printf("Enter number of elements in array: ");

scanf("%d",&n);

int arr[n];

printf("Enter value of array\n");

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

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

int S= sum(arr,n);

printf("Sum of all elements of array is %d",S);

return 0;

OUTPUT:
Q9: WAP by designing a recursive function to calculate the sum of all even digits
of any given integer.
PROGRAM:
#include <stdio.h>

int count=0,r,a=0;

int sum(int n){

if(n>0){

r=n%10;

if(r%2==0){

a = a+r;

n=n/10;

sum(n);

else{
return a;

int main()

{ int n;

printf("Enter a number: ");

scanf("%d",&n);

int s=sum(n);

printf("The sum of even digits of %d is %d",n,s);

return 0;

OUTPUT:

Q10: WAP to test whether a number n is palindrome number or not using


recursion.
PROGRAM:
#include <stdio.h>
int count=0,r,a=0;

int rev(int n){

if(n>0){

r=n%10;

a = a*10+r;

n=n/10;

rev(n);

else{

return a;

int main()

{ int n;

printf("Enter a number: ");

scanf("%d",&n);

int s=rev(n);

if(s==n){

printf("The given number is a palindrome");

else{

printf("The given number is not a palindrome");

return 0;

}
OUTPUT:

You might also like