Recurs Ions
Recurs Ions
Recurs Ions
21/11/24
AIM:
To solve the problems given using C programing language topic recursions.
Question 1
Jake is exploring triangular numbers and wants to determine if a given number is a triangular
number.
A triangular number Tn is a figurative number that can be represented in the form of an equilateral
triangular grid of elements such that every subsequent row contains an element more than the
previous one.
Help him with a program that takes n as input and implements a recursive function to check whether
the number is a triangle number or not.
Source code:
#include<stdio.h>
int triangular_numbers(int n,int sum,int i)
{ if(sum==n)
{return 1;}
if (sum>n)
{return 0;}
return triangular_numbers(n,sum+i,i+1);}
int main()
{ int n;
scanf("%d",&n);
if (triangular_numbers(n,0,1))
{printf("%d is a triangular number",n);}
24I310
else
{printf("%d is not a triangular number",n);}
return 0; }
Question 2
A student needs a program to find the n th term of an arithmetic sequence. Develop a
program that takes the first term, common difference, and the value of n as input and
prints the n th term of the arithmetic sequence.
Example
Input: 10 5 15
Output: 80
Explanation:
In this sequence, the first term a is 10, and the common difference d is 5.
The formula to find the n th term of the sequence: a n = a1 + (n-1) * d
Substituting the values from the input, we get:
a15 = 10 + (15-1) * 5
a15 = 10 + 14 * 5
a15 = 10 + 70
a15 = 80
Input format :
The input consists of three space-separated integers, a representing the first term, d representing
the common difference and the value of n in the arithmetic sequence, respectively.
Output format :
The output prints an integer representing the nth term of the arithmetic sequence.
Source code:
#include<stdio.h>
int nthTerm(int a,int d,int n)
{ if(n==1)
{return a;}
return d+nthTerm(a,d,n-1);}
24I310
int main()
{int a,d,n,result;
scanf("%d %d %d",&a,&d,&n);
result=nthTerm(a,d,n);
printf("%d",result);
return 0;}
Question 3
Heather wants to find the maximum divisor of a given positive integer N other than the number itself.
Develop a program for Heather that takes N as input and implements a recursive function to find the
maximum divisor of N excluding N itself.
Source code:
#include<stdio.h>
int maxDivisor(int n,int divisor)
{ if(divisor==1)
{ return 1;}
else if(n%divisor==0)
{ return divisor;}
else
{return maxDivisor(n,divisor-1);}}
int main()
{ int n ;
scanf("%d",&n);
int max_divisor=maxDivisor(n,n/2);
printf("%d",max_divisor);
24I310
return 0;}
Question 4
In a quaint village, there is an ancient tree that stands tall with a series of wooden platforms leading
up to its crown. Local children love to play around the tree, and a particularly adventurous child
named Leo has decided to reach the highest platform, which is the Nth level.
The catch is that Leo can only climb from one platform to another by either stepping up 1 level or
jumping up 2 levels at a time. Every time Leo makes a jump or step, he wonders how many distinct
ways he can reach the Nth platform starting from the ground level (0th level).
Write a program that takes an integer N as input (the total number of levels) and determines the total
number of distinct paths Leo can take to reach the Nth platform.
Input format :
The input consists of a single integer N, representing the number of levels.
Output format :
The output prints a single integer representing the total number of distinct paths Leo can take to
reach the Nth platform.
Source code:
#include<stdio.h>
int countWays(int n)
{ if (n<=1)
{return 1;}
else if(n==2)
{ return 2;}
else {
return countWays(n-1)+countWays(n-2);}}
int main()
{int n;
scanf("%d",&n);
int ways=countWays(n);
printf("%d",ways);
return 0;}
24I310
Question 5
Reena is studying number theory and is fascinated by the concept of the Least Common Multiple
(LCM) of three numbers. She wants to write a program to find the LCM of three numbers using
recursion.
Source code:
#include<stdio.h>
int gcd(int a,int b)
{ if (b==0)
{return a;}
return gcd(b,a%b);}
int find_lcm(int a,int b,int c)
{ if (a==0||b==0||c==0)
{return 0;}
int lcm_ab=(a*b)/gcd(a,b);
return (lcm_ab*c)/gcd(lcm_ab,c);}
int main()
{ int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int result=find_lcm(a,b,c);
printf("%d", result);
return 0;
}
24I310
Question 6
Anu wants to develop a program to check whether a number can be expressed as the
sum of two prime numbers using recursion.
Given a positive integer N, write a program for her to check if the number N can be
represented as the sum of two prime numbers.
Source code:
#include<stdio.h>
int isprime(int ns)
{if (ns<=1)
{return 0;}
for (int i=2;i*i<=ns;i++)
{if (ns%i==0){
return 0;}}
return 1;}
void sum_of_two_primes(int n)
{for (int i=2;i<=n/2;i++)
{if(isprime(i)&&isprime(n-i))
{printf("%d = %d + %d\n",n,i,n-i);}}}
int main()
{int n;
scanf("%d",&n);
if(n%2!=0)
24I310
{printf("%d cannot be expressed as the sum of two prime numbers",n);}
else
{sum_of_two_primes(n);}
return 0;}
Question 7
James is interested in finding the factors of a given number using a recursive approach. He wants to
check an array of integers to see which of them are factors of a specified key number. The program
should print each integer in the array along with a message indicating whether it is a factor of the
key. Finally, it should print all the factors found.
Input format :
The first line of input consists of an integer N, representing the number of elements in the array.
The second line of input consists of N space-separated integers, representing the elements of the
array.
The third line consists of the value of M, for which the factors have to be found.
Output format :
For each element in the array, print "Checking index X: Y" where X is the current index and Y is
the element at that index.
If the element is a factor of the key, print "Y is a factor of M".
If the element is not a factor, print "Y is not a factor of M".
Source code:
#include<stdio.h>
void recur(int arr[],int size,int num,int index,int result[],int *resultIndex)
{ if (index==size)
return;
printf("Checking index %d: %d\n",index,arr[index]);
if(num % arr[index]==0)
{ printf("%d is a factor of %d\n",arr[index], num);
result[*resultIndex]=arr[index];
(*resultIndex)++;
}
else
{ printf("%d is not a factor of %d\n",arr[index],num);
}
24I310
recur(arr,size,num,index+1,result,resultIndex);}
int main()
{int size,num;
scanf("%d",&size);
int arr[size];
int result[size];
int resultIndex=0;
for(int i=0;i<size;i++)
{scanf("%d",&arr[i]);}
recur(arr,size,num,0,result,&resultIndex);
printf("All factors of %d are: ",num);
for(int i=0;i<resultIndex;i++)
{printf("%d ",result[i]);}
return 0;}
Question 8
Arun is working on a program to find the Golomb sequence. The Golomb sequence is a non-
decreasing integer sequence where the n th term is equal to the number of times n appears in the
sequence.
Given a positive integer n. The task is to find the first n terms of the Golomb sequence.
Source code:
#include<stdio.h>
24I310
int recur(int n)
{ if (n==1) return 1;
return 1+recur(n-recur(recur(n-1)));
}
void printrecur(int n)
{for (int i=1;i<=n;i++)
{printf("%d ",recur(i));}
printf("\n");}
int main()
{int n;
scanf("%d",&n);
printrecur(n);
return 0;}
Question 9
Viaan is a student who wants to reverse the input string for his assignment. He is asked
to solve it using a recursive function but is unable to crack the logic.
Help him write a program that uses a recursive function to reverse the words.
Source code:
#include<stdio.h>
#include<string.h>
void reverse(char* str,int start,int end)
{ if (start>=end)
return;
24I310
char temp=str[start];
str[start]=str[end];
str[end]=temp;
reverse(str,start+1,end-1);}
int main()
{char str[100];
scanf("%s",&str);
int n=strlen(str);
reverse(str,0,n-1);
printf("%s",str);
return 0;}
Question 10
Julie is working on a program to print even or odd numbers in a given range: a to b. The program
should use a recursive function named print to print numbers from a to b based on the given type
('e' - even / 'o' - odd).
Source code:
#include<stdio.h>
void printE(int m,int n)
{ if (m>n){return ;}
if(m%2==0){
printf("%d ",m);}
24I310
printE(m+1,n);}
void printO(int m,int n)
{if(m>n) return;
if(m%2!=0)
printf("%d ",m);
printO(m+1,n);}
int main()
{int m,n;
char OE;
scanf("%d ",&m);
scanf("%d",&n);
scanf("%c",&OE);
if(OE=='e')
{printE(m,n);}
else{printO(m,n);}
return 0;}
Question 11
Nancy, an aspiring programmer, has a series of alphabets from A-Z arranged circularly.
Write a program that takes an integer input representing the position in the series and
prints the corresponding character.
Example:
1. If Nancy inputs 1, it corresponds to 'A'.
2. If Nancy inputs 28, it corresponds to 'B' because the alphabet series is circular, and
position 28 loops back to the second letter in the series.
Help Nancy by crafting a program that meets her challenge using a recursive function
called generateTerm().
Input format :
The input consists of an integer n, representing the position for which the character needs to be
retrieved.
Output format :
The output prints the character present at the given position.
24I310
Source code:
#include<stdio.h>
int generateTerm(int n)
{ if (n<=27)
{return n;}
generateTerm(n%26);}
int main()
{int n;
scanf("%d",&n);
int c=generateTerm(n);
printf("%c",c+64);
return 0;}
RESULT:
Hence all the problems are solved using the recursion technique of C
programming.
24I310