SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere รคhnliche Inhalte

Was ist angesagt?

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 ANSWERSKavyaSharma65
ย 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
ย 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
ย 
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 ANSWERSKavyaSharma65
ย 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
ย 
Simple C programs
Simple C programsSimple C programs
Simple C programsab11cs001
ย 
Bayesian workflow with PyMC3 and ArviZ
Bayesian workflow with PyMC3 and ArviZBayesian workflow with PyMC3 and ArviZ
Bayesian workflow with PyMC3 and ArviZCorrie Bartelheimer
ย 
Programming for Mechanical Engineers in EES
Programming for Mechanical Engineers in EESProgramming for Mechanical Engineers in EES
Programming for Mechanical Engineers in EESNaveed Rehman
ย 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
ย 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.comAkanchha Agrawal
ย 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functionsAwinash Goswami
ย 
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 solutionHazrat Bilal
ย 
C faq pdf
C faq pdfC faq pdf
C faq pdfDebiPanda
ย 
SaraPIC
SaraPICSaraPIC
SaraPICSara Sahu
ย 

Was ist angesagt? (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
ย 

ร„hnlich wie Recursion in C

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.2020vrgokila
ย 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
ย 
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 ...DR B.Surendiran .
ย 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab SolutionsSrinivas Reddy Amedapu
ย 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
ย 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
ย 
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 HyderabadSrinivas Reddy Amedapu
ย 
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 HyderabadSrinivas Reddy Amedapu
ย 
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 structuresLakshmi Sarvani Videla
ย 
Informe laboratorio nยฐ1
Informe laboratorio nยฐ1Informe laboratorio nยฐ1
Informe laboratorio nยฐ1luisescobedo38
ย 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
ย 
Struct examples
Struct examplesStruct examples
Struct examplesmondalakash2012
ย 
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 RAMALINGAMSaraswathiRamalingam
ย 

ร„hnlich wie 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
ย 

Mehr von 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
ย 

Kรผrzlich hochgeladen

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
ย 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
ย 
๐Ÿฌ The future of MySQL is Postgres ๐Ÿ˜
๐Ÿฌ  The future of MySQL is Postgres   ๐Ÿ˜๐Ÿฌ  The future of MySQL is Postgres   ๐Ÿ˜
๐Ÿฌ The future of MySQL is Postgres ๐Ÿ˜RTylerCroy
ย 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
ย 
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 educationjfdjdjcjdnsjd
ย 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
ย 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
ย 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
ย 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
ย 
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 FresherRemote DBA Services
ย 
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 DiscoveryTrustArc
ย 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
ย 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
ย 
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...Martijn de Jong
ย 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
ย 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
ย 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
ย 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
ย 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
ย 
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...DianaGray10
ย 

Kรผrzlich hochgeladen (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
ย 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
ย 
๐Ÿฌ The future of MySQL is Postgres ๐Ÿ˜
๐Ÿฌ  The future of MySQL is Postgres   ๐Ÿ˜๐Ÿฌ  The future of MySQL is Postgres   ๐Ÿ˜
๐Ÿฌ The future of MySQL is Postgres ๐Ÿ˜
ย 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.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
ย 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
ย 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
ย 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
ย 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
ย 
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
ย 
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
ย 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
ย 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ย 
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...
ย 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
ย 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
ย 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
ย 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
ย 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
ย 
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...
ย 

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