SlideShare a Scribd company logo
1 of 7
Q) C program to findfactorial of number usingrecursionas well as iteration
π‘“π‘Žπ‘π‘‘π‘œπ‘Ÿπ‘–π‘Žπ‘™(𝑛) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 ; 𝑖𝑓 𝑛 == 0 π‘œπ‘Ÿ 𝑛 == 1
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛 βˆ— π‘“π‘Žπ‘π‘‘π‘œπ‘Ÿπ‘–π‘Žπ‘™( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1
Using loops:
factorial=1;
while(n>1)
{
factorial=factorial*n;
n=n-1;
}
Q) Calculate power of a number program in c usingRecursion and Iteration
π‘π‘œπ‘€π‘’π‘Ÿ(𝑏, 𝑒) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑒 == 0
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑏 βˆ— π‘π‘œπ‘€π‘’π‘Ÿ( 𝑏, 𝑒 βˆ’ 1); 𝑖𝑓 𝑒 > 0
Explanationwithexample:
53 = 5 βˆ— 52 power(5,3) = 5*power(5,2)
power(5,3) = 5 * power(5,3-1) = 5 * power(5,2) = 5*25 = 125
power(5,2) = 5 * power(5,2-1) = 5 * power(5,1) = 5*5 =25
power(5,1) = 5 * power(5,1-1) = 5 * power(5,0) = 5*1 = 5
power(5,0) = 1
Using loops:
power=1;
while(e>0)
{
power=power*b;
e=e-1;
}
Q) Write a C program to count digitsof a number usingRecursion and Iteration.
π‘π‘œπ‘’π‘›π‘‘(𝑛) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 0
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 + π‘π‘œπ‘’π‘›π‘‘( 𝑛/10); 𝑖𝑓 𝑛 > 0
Explanationwithexample:
count(872) = 1+count(872/10) = 1+count(87) = 1+2=3
count(87) = 1+count(87/10) = 1+count(8) =1+1 = 2
count(8) = 1+count(8/10) = 1+count(0) =1+0 =1
count(0) = 0
Using loops:
count = 0;
while(n>0)
{
count = count+1;
n=n/10;
}
Q) Write a C program to find sum of firstn natural numbers using Recursion.
π‘ π‘’π‘š(𝑛) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑛 == 1
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛 + π‘ π‘’π‘š( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1
Explanationwithexample:
sumof first5 natural numbers=
1+2+3+4+5
5+4+3+2+1
5+sum of first4 natural numbers
sumof first4 natural numbers=
4+sum of first3 natural numbers
sumof first3 natural numbers=
3+sum of first2 natural numbers
sumof first2 natural numbers =
2+sum of first1 natural numbers
sumof first1 natural numbers= 1
sum(5) = 5+sum(5-1) = 5+ sum(4) = 10+5 = 15
sum(4) = 4+sum(4-1) = 4+ sum(3) = 6+4 = 10
sum(3) = 3+sum(3-1) = 3+ sum(2) = 3+3 = 6
sum(2) = 2+sum(2-1) = 2+ sum(1) = 2+1 = 3
sum(1) = 1
Using loops:
sum=0
while(n>0)
{
sum=sum+n;
n=n-1;
}
Q. C program to print sum of digitsof a given numberusing recursion
π‘ π‘’π‘š(𝑛) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 0
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛%10 + π‘ π‘’π‘š( 𝑛/10); 𝑖𝑓 𝑛 > 0
Explanationwithexample:
sum(872) = 872%10 + sum(872/10) = 2+sum(87) = 2+15= 17
sum(87) = 87%10 + sum (87/10) = 7+sum(8) = 7+8 = 15
sum(8) = 8%10 + sum (8/10) = 8+sum(0) = 8+0 = 8
sum(0) = 0
Q. Write a C program to findnth term in Fibonacci Seriesusing Recursion.
𝑓𝑖𝑏(𝑛) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 1
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑛 == 2
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑓𝑖𝑏( 𝑛 βˆ’ 1) + 𝑓𝑖𝑏( 𝑛 βˆ’ 2); 𝑖𝑓 𝑛 > 2
Q. C program to find out the GCD (GreatestCommonDivisor )ofthe two numbers usingrecursion
𝑔𝑐𝑑 (π‘Ž, 𝑏) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘Ž; 𝑖𝑓 𝑏%π‘Ž == 0
𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› gcd( 𝑏%π‘Ž, π‘Ž) ; 𝑖𝑓 𝑏%π‘Ž ! = 0
Explanationwithexample:
Sample Input:8 12
Sample Output:4
8 ) 12 ( 1
8
---------
4 ) 8 ( 2
8
-------
0
--------
gcd(8,12) =gcd(12%8 , 8) = gcd( 4, 8) = 4
gcd(4,8) = 4
Full Program:
#include <stdio.h>
intprint(intn)
{
if(n<= 2)
printf("%d",n%2);
else
{
print(n/2);
printf("%d",n%2);
}
}
intmain()
{
int n;
printf("enteranumber");
scanf("%d",&n);
print(n);
}
Q. Write a C program to findthe first uppercase letterin the givenstring usingrecursion.Sample
Input : kl University
Sample Output: U
π‘ π‘’π‘Žπ‘Ÿπ‘β„Ž(π‘ π‘‘π‘Ÿ, 𝑖) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› βˆ’ 1; 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] == β€²0β€²
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑖; 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] == β€²π‘ˆβ€²
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘ π‘’π‘Žπ‘Ÿπ‘β„Ž( π‘ π‘‘π‘Ÿ, 𝑖 + 1); 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] ! = β€²π‘ˆβ€²
Q) write C program to calculate lengthof the string usingRecursion
𝑙𝑒𝑛(π‘ π‘‘π‘Ÿ) = {
0 𝑖𝑓 π‘ π‘‘π‘Ÿ[0] == β€²0β€²
𝑛
1 + len(string left after removing first character) 𝑖𝑓 π‘ π‘‘π‘Ÿ[0] ! = β€²0β€²
Explanationwithexample:
lengthof stringβ€œabc” = 1+ lengthof stringβ€œbc”
lengthof stringβ€œbc” = 1+ lengthof stringβ€œc”
lengthof stringβ€œc” = 1+ lengthof stringβ€œ0”
lengthof stringβ€œ0” = 0
Q) Write a program in C to count numberof divisorsof a givennumberusing recursion
π‘π‘œπ‘’π‘›π‘‘(𝑛, 𝑖) = {
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑖 == 𝑛
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 + π‘π‘œπ‘’π‘›π‘‘( 𝑛, 𝑖 + 1) ; 𝑖𝑓 (𝑛%𝑖 == 0)
π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘π‘œπ‘’π‘›π‘‘( 𝑛, 𝑖 + 1); 𝑖𝑓(𝑛%𝑖 ! = 1)
Using loops:
for(i=1;i<=n;i++)
{
If(n%i ==0)
{
count=count+1;
}
}
Recursive programtocheck whetheragivennumberisprime orcomposite.
#include <stdio.h>
intcount(intn,inti)
{
if(i == n)
return1;
else if(n%i==0)
return1+count(n,i+1); //1+count of divisorsfromi+1 to n if i isdivisor
else if(n%i!=0)
returncount(n,i+1); // count of divisorswontchange if i isnot divisor
}
intmain()
{
int n;
printf("enteranumber");
scanf("%d",&n);
int c=count(n,1);//count of divisorsfrom1 to n
if(c== 2)
printf("prime");
else
printf("composite");
}
Q. C program to displays integers100 through 1 usingRecursion and Iteration
π‘‘π‘–π‘ π‘π‘™π‘Žπ‘¦(𝑛) = {
π‘π‘Ÿπ‘–π‘›π‘‘π‘“(%d, 𝑛); 𝑖𝑓 𝑛 == 1
𝑛
π‘π‘Ÿπ‘–π‘›π‘‘π‘“(%d, 𝑛); π‘‘π‘–π‘ π‘π‘™π‘Žπ‘¦( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1
Q. Write a program in C to converta decimal number to binary usingrecursion.
Sample Input : 66
Sample Output: 1000010
π‘‘π‘’π‘π‘‘π‘œπ‘π‘–π‘›(𝑛) = {
π‘π‘Ÿπ‘–π‘›π‘‘π‘“("%d", 𝑛%2); 𝑖𝑓 𝑛 ≀ 2
𝑛
π‘‘π‘’π‘π‘‘π‘œπ‘π‘–π‘›( 𝑛/2); π‘π‘Ÿπ‘–π‘›π‘‘π‘“("%d", 𝑛%2); 𝑖𝑓 𝑛 > 2
Full program
#include <stdio.h>
intdectobin(intn)
{
if(n<= 2)
printf("%d",n%2);
else
{
dectobin(n/2);
printf("%d",n%2);
}
}
intmain()
{
int n;
printf("enteranumber");
scanf("%d",&n);
dectobin(n);
}
RecursionStackof factorial of 3
intfactorial(1)
{
if(1== 0 || 1==1)
return1;
else if(n>1)
returnn*factorial(n-1);
}
intfactorial(2)
{
if(2== 0 || 2 ==1)
return1;
else if(2>1)
return2*factorial(2-1);
}
intfactorial(3)
{
if(3== 0 || 3==1)
return1;
else if(3>1)
return3*factorial(3-1);
}
intmain()
{
int n,fact;
printf("enteranumber");
scanf("%d",&n);
fact=factorial(n);//factorial(3)
printf("factorialof %dis%d",n,fact);
}
Recursionstackof 4 th term of Fibonacci
intfib(2)
{
if( 2 == 1)
return0;
else if(2== 2)
return1;
else
returnfib(n -1)+fib(n-2);
}
intfib(1)
{
if( n == 1)
return0;
else if(n==2)
return1;
else
returnfib(n -1)+fib(n-2);}
intfib(2)
{
if( 2 == 1)
return0;
else if(2== 2)
return1;
else
returnfib(n -1)+fib(n-2);
}
intfib(3)
{
if( 3 == 1)
return0;
else if(3== 2)
return1;
else
returnfib(3-1)+fib(3-2);
}
intfib(4)
{
if( 4 == 1)
return0;
else if(4== 2)
return1;
else
returnfib(4-1)+fib(4-2);
}
intmain()
{
int n,term;
printf("entern value");
scanf("%d",&n);
term=fib(n); //fib(4)
printf("%dthtermis%d",n,term);
}
C program to findfactorial of number usingrecursion as well as iteration ,
Calculate power of a number program in c usingRecursion and Iteration,Write a C program to
count digitsof a numberusing Recursionand Iteration, Write a C program to find sum of firstn
natural numbers usingRecursion, C program to printsum of digits ofa givennumber using
recursion,Write a C program to findnth term in Fibonacci SeriesusingRecursion, C program to
findout the GCD (GreatestCommonDivisor )ofthe two numbersusing recursion,
Write a C program to find the first upper case letterin the givenstring using recursion, write C
program to calculate lengthof the string using Recursion,
Write a program in C to count number of divisorsof a givennumber usingrecursion, Recursive
program to checkwhetheragivennumberisprime orcomposite,
C program to displaysintegers100 through 1 using Recursionand Iteration,Write a program in C
to convert a decimal numberto binary usingrecursion,
RecursionStackof factorial of 3 Recursionstackof 4th term of Fibonacci

More Related Content

What's hot

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
Β 
SaraPIC
SaraPICSaraPIC
SaraPIC
Sara Sahu
Β 

What's hot (20)

LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
Β 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Β 
C tech questions
C tech questionsC tech questions
C tech questions
Β 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
Β 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
Β 
Vcs9
Vcs9Vcs9
Vcs9
Β 
Simple C programs
Simple C programsSimple C programs
Simple C programs
Β 
Bayesian workflow with PyMC3 and ArviZ
Bayesian workflow with PyMC3 and ArviZBayesian workflow with PyMC3 and ArviZ
Bayesian workflow with PyMC3 and ArviZ
Β 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
Β 
Programming for Mechanical Engineers in EES
Programming for Mechanical Engineers in EESProgramming for Mechanical Engineers in EES
Programming for Mechanical Engineers in EES
Β 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
Β 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Β 
Core programming in c
Core programming in cCore programming in c
Core programming in c
Β 
Cpl
CplCpl
Cpl
Β 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.com
Β 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
Β 
Kalman filter
Kalman filterKalman filter
Kalman filter
Β 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
Β 
C faq pdf
C faq pdfC faq pdf
C faq pdf
Β 
SaraPIC
SaraPICSaraPIC
SaraPIC
Β 

Similar to Recursion in C

Similar to Recursion in C (20)

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
Β 
Progr2
Progr2Progr2
Progr2
Β 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
Β 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Β 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Β 
C lab excellent
C lab excellentC lab excellent
C lab excellent
Β 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
Β 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
Β 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
Β 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Β 
week-3x
week-3xweek-3x
week-3x
Β 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Β 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Β 
Progr3
Progr3Progr3
Progr3
Β 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Β 
Informe laboratorio nΒ°1
Informe laboratorio nΒ°1Informe laboratorio nΒ°1
Informe laboratorio nΒ°1
Β 
2D array
2D array2D array
2D array
Β 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
Β 
Struct examples
Struct examplesStruct examples
Struct examples
Β 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
Β 

More from Lakshmi Sarvani Videla

More from Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
Β 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
Β 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
Β 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
Β 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
Β 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
Β 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
Β 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Β 
Functions
FunctionsFunctions
Functions
Β 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Β 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
Β 
Graphs
GraphsGraphs
Graphs
Β 
B trees
B treesB trees
B trees
Β 
Functions in python3
Functions in python3Functions in python3
Functions in python3
Β 
Dictionary
DictionaryDictionary
Dictionary
Β 
Sets
SetsSets
Sets
Β 
Lists
ListsLists
Lists
Β 
C programs
C programsC programs
C programs
Β 
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
Β 
Dbms
DbmsDbms
Dbms
Β 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
Β 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
Β 

Recently uploaded (20)

Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Β 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Β 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Β 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Β 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Β 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Β 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
Β 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Β 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Β 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Β 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
Β 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Β 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
Β 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
Β 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Β 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
Β 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
Β 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Β 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Β 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Β 

Recursion in C

  • 1. Q) C program to findfactorial of number usingrecursionas well as iteration π‘“π‘Žπ‘π‘‘π‘œπ‘Ÿπ‘–π‘Žπ‘™(𝑛) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 ; 𝑖𝑓 𝑛 == 0 π‘œπ‘Ÿ 𝑛 == 1 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛 βˆ— π‘“π‘Žπ‘π‘‘π‘œπ‘Ÿπ‘–π‘Žπ‘™( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1 Using loops: factorial=1; while(n>1) { factorial=factorial*n; n=n-1; } Q) Calculate power of a number program in c usingRecursion and Iteration π‘π‘œπ‘€π‘’π‘Ÿ(𝑏, 𝑒) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑒 == 0 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑏 βˆ— π‘π‘œπ‘€π‘’π‘Ÿ( 𝑏, 𝑒 βˆ’ 1); 𝑖𝑓 𝑒 > 0 Explanationwithexample: 53 = 5 βˆ— 52 power(5,3) = 5*power(5,2) power(5,3) = 5 * power(5,3-1) = 5 * power(5,2) = 5*25 = 125 power(5,2) = 5 * power(5,2-1) = 5 * power(5,1) = 5*5 =25 power(5,1) = 5 * power(5,1-1) = 5 * power(5,0) = 5*1 = 5 power(5,0) = 1 Using loops: power=1; while(e>0) { power=power*b; e=e-1; } Q) Write a C program to count digitsof a number usingRecursion and Iteration. π‘π‘œπ‘’π‘›π‘‘(𝑛) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 0 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 + π‘π‘œπ‘’π‘›π‘‘( 𝑛/10); 𝑖𝑓 𝑛 > 0 Explanationwithexample: count(872) = 1+count(872/10) = 1+count(87) = 1+2=3 count(87) = 1+count(87/10) = 1+count(8) =1+1 = 2 count(8) = 1+count(8/10) = 1+count(0) =1+0 =1 count(0) = 0 Using loops: count = 0; while(n>0) { count = count+1; n=n/10; } Q) Write a C program to find sum of firstn natural numbers using Recursion. π‘ π‘’π‘š(𝑛) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑛 == 1 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛 + π‘ π‘’π‘š( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1 Explanationwithexample: sumof first5 natural numbers= 1+2+3+4+5
  • 2. 5+4+3+2+1 5+sum of first4 natural numbers sumof first4 natural numbers= 4+sum of first3 natural numbers sumof first3 natural numbers= 3+sum of first2 natural numbers sumof first2 natural numbers = 2+sum of first1 natural numbers sumof first1 natural numbers= 1 sum(5) = 5+sum(5-1) = 5+ sum(4) = 10+5 = 15 sum(4) = 4+sum(4-1) = 4+ sum(3) = 6+4 = 10 sum(3) = 3+sum(3-1) = 3+ sum(2) = 3+3 = 6 sum(2) = 2+sum(2-1) = 2+ sum(1) = 2+1 = 3 sum(1) = 1 Using loops: sum=0 while(n>0) { sum=sum+n; n=n-1; } Q. C program to print sum of digitsof a given numberusing recursion π‘ π‘’π‘š(𝑛) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 0 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑛%10 + π‘ π‘’π‘š( 𝑛/10); 𝑖𝑓 𝑛 > 0 Explanationwithexample: sum(872) = 872%10 + sum(872/10) = 2+sum(87) = 2+15= 17 sum(87) = 87%10 + sum (87/10) = 7+sum(8) = 7+8 = 15 sum(8) = 8%10 + sum (8/10) = 8+sum(0) = 8+0 = 8 sum(0) = 0 Q. Write a C program to findnth term in Fibonacci Seriesusing Recursion. 𝑓𝑖𝑏(𝑛) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 0; 𝑖𝑓 𝑛 == 1 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑛 == 2 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑓𝑖𝑏( 𝑛 βˆ’ 1) + 𝑓𝑖𝑏( 𝑛 βˆ’ 2); 𝑖𝑓 𝑛 > 2 Q. C program to find out the GCD (GreatestCommonDivisor )ofthe two numbers usingrecursion 𝑔𝑐𝑑 (π‘Ž, 𝑏) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘Ž; 𝑖𝑓 𝑏%π‘Ž == 0 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› gcd( 𝑏%π‘Ž, π‘Ž) ; 𝑖𝑓 𝑏%π‘Ž ! = 0 Explanationwithexample: Sample Input:8 12 Sample Output:4 8 ) 12 ( 1 8 --------- 4 ) 8 ( 2 8 ------- 0
  • 3. -------- gcd(8,12) =gcd(12%8 , 8) = gcd( 4, 8) = 4 gcd(4,8) = 4 Full Program: #include <stdio.h> intprint(intn) { if(n<= 2) printf("%d",n%2); else { print(n/2); printf("%d",n%2); } } intmain() { int n; printf("enteranumber"); scanf("%d",&n); print(n); } Q. Write a C program to findthe first uppercase letterin the givenstring usingrecursion.Sample Input : kl University Sample Output: U π‘ π‘’π‘Žπ‘Ÿπ‘β„Ž(π‘ π‘‘π‘Ÿ, 𝑖) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› βˆ’ 1; 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] == β€²0β€² π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 𝑖; 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] == β€²π‘ˆβ€² π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘ π‘’π‘Žπ‘Ÿπ‘β„Ž( π‘ π‘‘π‘Ÿ, 𝑖 + 1); 𝑖𝑓 π‘ π‘‘π‘Ÿ[𝑖] ! = β€²π‘ˆβ€² Q) write C program to calculate lengthof the string usingRecursion 𝑙𝑒𝑛(π‘ π‘‘π‘Ÿ) = { 0 𝑖𝑓 π‘ π‘‘π‘Ÿ[0] == β€²0β€² 𝑛 1 + len(string left after removing first character) 𝑖𝑓 π‘ π‘‘π‘Ÿ[0] ! = β€²0β€² Explanationwithexample: lengthof stringβ€œabc” = 1+ lengthof stringβ€œbc” lengthof stringβ€œbc” = 1+ lengthof stringβ€œc” lengthof stringβ€œc” = 1+ lengthof stringβ€œ0” lengthof stringβ€œ0” = 0 Q) Write a program in C to count numberof divisorsof a givennumberusing recursion π‘π‘œπ‘’π‘›π‘‘(𝑛, 𝑖) = { π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1; 𝑖𝑓 𝑖 == 𝑛 π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› 1 + π‘π‘œπ‘’π‘›π‘‘( 𝑛, 𝑖 + 1) ; 𝑖𝑓 (𝑛%𝑖 == 0) π‘Ÿπ‘’π‘‘π‘’π‘Ÿπ‘› π‘π‘œπ‘’π‘›π‘‘( 𝑛, 𝑖 + 1); 𝑖𝑓(𝑛%𝑖 ! = 1) Using loops: for(i=1;i<=n;i++) { If(n%i ==0) { count=count+1; } }
  • 4. Recursive programtocheck whetheragivennumberisprime orcomposite. #include <stdio.h> intcount(intn,inti) { if(i == n) return1; else if(n%i==0) return1+count(n,i+1); //1+count of divisorsfromi+1 to n if i isdivisor else if(n%i!=0) returncount(n,i+1); // count of divisorswontchange if i isnot divisor } intmain() { int n; printf("enteranumber"); scanf("%d",&n); int c=count(n,1);//count of divisorsfrom1 to n if(c== 2) printf("prime"); else printf("composite"); } Q. C program to displays integers100 through 1 usingRecursion and Iteration π‘‘π‘–π‘ π‘π‘™π‘Žπ‘¦(𝑛) = { π‘π‘Ÿπ‘–π‘›π‘‘π‘“(%d, 𝑛); 𝑖𝑓 𝑛 == 1 𝑛 π‘π‘Ÿπ‘–π‘›π‘‘π‘“(%d, 𝑛); π‘‘π‘–π‘ π‘π‘™π‘Žπ‘¦( 𝑛 βˆ’ 1); 𝑖𝑓 𝑛 > 1 Q. Write a program in C to converta decimal number to binary usingrecursion. Sample Input : 66 Sample Output: 1000010 π‘‘π‘’π‘π‘‘π‘œπ‘π‘–π‘›(𝑛) = { π‘π‘Ÿπ‘–π‘›π‘‘π‘“("%d", 𝑛%2); 𝑖𝑓 𝑛 ≀ 2 𝑛 π‘‘π‘’π‘π‘‘π‘œπ‘π‘–π‘›( 𝑛/2); π‘π‘Ÿπ‘–π‘›π‘‘π‘“("%d", 𝑛%2); 𝑖𝑓 𝑛 > 2 Full program #include <stdio.h> intdectobin(intn) { if(n<= 2) printf("%d",n%2); else { dectobin(n/2); printf("%d",n%2); } } intmain() { int n; printf("enteranumber"); scanf("%d",&n); dectobin(n); }
  • 5. RecursionStackof factorial of 3 intfactorial(1) { if(1== 0 || 1==1) return1; else if(n>1) returnn*factorial(n-1); } intfactorial(2) { if(2== 0 || 2 ==1) return1; else if(2>1) return2*factorial(2-1); } intfactorial(3) { if(3== 0 || 3==1) return1; else if(3>1) return3*factorial(3-1); } intmain() { int n,fact; printf("enteranumber"); scanf("%d",&n); fact=factorial(n);//factorial(3) printf("factorialof %dis%d",n,fact); }
  • 6. Recursionstackof 4 th term of Fibonacci intfib(2) { if( 2 == 1) return0; else if(2== 2) return1; else returnfib(n -1)+fib(n-2); } intfib(1) { if( n == 1) return0; else if(n==2) return1; else returnfib(n -1)+fib(n-2);} intfib(2) { if( 2 == 1) return0; else if(2== 2) return1; else returnfib(n -1)+fib(n-2); } intfib(3) { if( 3 == 1) return0; else if(3== 2) return1; else returnfib(3-1)+fib(3-2); } intfib(4) { if( 4 == 1) return0; else if(4== 2) return1; else returnfib(4-1)+fib(4-2); } intmain() { int n,term; printf("entern value"); scanf("%d",&n); term=fib(n); //fib(4) printf("%dthtermis%d",n,term); }
  • 7. C program to findfactorial of number usingrecursion as well as iteration , Calculate power of a number program in c usingRecursion and Iteration,Write a C program to count digitsof a numberusing Recursionand Iteration, Write a C program to find sum of firstn natural numbers usingRecursion, C program to printsum of digits ofa givennumber using recursion,Write a C program to findnth term in Fibonacci SeriesusingRecursion, C program to findout the GCD (GreatestCommonDivisor )ofthe two numbersusing recursion, Write a C program to find the first upper case letterin the givenstring using recursion, write C program to calculate lengthof the string using Recursion, Write a program in C to count number of divisorsof a givennumber usingrecursion, Recursive program to checkwhetheragivennumberisprime orcomposite, C program to displaysintegers100 through 1 using Recursionand Iteration,Write a program in C to convert a decimal numberto binary usingrecursion, RecursionStackof factorial of 3 Recursionstackof 4th term of Fibonacci