SlideShare una empresa de Scribd logo
1 de 2
How to turn this recursive function into an iterative function:
#include <iostream>
#include <cmath>
using namespace std;
void get_divisors(int n);
int main() {
int n = 0;
cout << "Enter a number and press ENTER: ";
cin >> n;
get_divisors(n);
cout << endl;
return 0;
}
void get_divisors(int n) {
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++)
if (n % i == 0)
{
cout<<i<<",";
get_divisors(n / i);
return;
}
cout << n;
}
Solution
Above function is a recursive functive which means a function which calls it self.
function calling it self should manipulated for iterative function
iterative function
#include <iostream>//header file for input ut function
using namespace std;//it tells the compiler to link std name space
void get_divisors(int n);//function declaration
int main() {
int n = 0;
cout << "Enter a number and press ENTER: ";
cin >> n;//keyboard inputting
get_divisors(n);//calling function
cout << endl;
return 0;
}
void get_divisors(int n) {//function definition
int i;
for (i = 2; i <= n; i++){
if (n % i == 0)
{
cout<<i<<",";
}
}
return;
cout << n;
}
output
Enter a number and press ENTER: 60 2,3,4,5,6,10,12,15,20,30,60,

Más contenido relacionado

Similar a How to turn this recursive function into an iterative function- #inclu.docx

Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
seoagam1
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
Kevin III
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 

Similar a How to turn this recursive function into an iterative function- #inclu.docx (20)

UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
Function
FunctionFunction
Function
 

Más de cliftonl1

How is communication a part of best practices-Solution4 Leadership Bes.docx
How is communication a part of best practices-Solution4 Leadership Bes.docxHow is communication a part of best practices-Solution4 Leadership Bes.docx
How is communication a part of best practices-Solution4 Leadership Bes.docx
cliftonl1
 
How does the National Society of Professional Engineers Code of Ethics.docx
How does the National Society of Professional Engineers Code of Ethics.docxHow does the National Society of Professional Engineers Code of Ethics.docx
How does the National Society of Professional Engineers Code of Ethics.docx
cliftonl1
 
How do I determine the Rf Value with just the information I have provi.docx
How do I determine the Rf Value with just the information I have provi.docxHow do I determine the Rf Value with just the information I have provi.docx
How do I determine the Rf Value with just the information I have provi.docx
cliftonl1
 
How do I print a list of payroll checks for the calendar year to date.docx
How do I print a list of payroll checks for the calendar year to date.docxHow do I print a list of payroll checks for the calendar year to date.docx
How do I print a list of payroll checks for the calendar year to date.docx
cliftonl1
 
How does SO2 in the atmosphere affect global warming- In general- what.docx
How does SO2 in the atmosphere affect global warming- In general- what.docxHow does SO2 in the atmosphere affect global warming- In general- what.docx
How does SO2 in the atmosphere affect global warming- In general- what.docx
cliftonl1
 
how do the different architectures of a polymer affect their propertie.docx
how do the different architectures of a polymer affect their propertie.docxhow do the different architectures of a polymer affect their propertie.docx
how do the different architectures of a polymer affect their propertie.docx
cliftonl1
 
how would a police department exhibit different structure and procedur.docx
how would a police department exhibit different structure and procedur.docxhow would a police department exhibit different structure and procedur.docx
how would a police department exhibit different structure and procedur.docx
cliftonl1
 

Más de cliftonl1 (20)

How many dance steps would the square have-and-Construct a multiplicat.docx
How many dance steps would the square have-and-Construct a multiplicat.docxHow many dance steps would the square have-and-Construct a multiplicat.docx
How many dance steps would the square have-and-Construct a multiplicat.docx
 
How has the physician-patient relationship changed over the past centu.docx
How has the physician-patient relationship changed over the past centu.docxHow has the physician-patient relationship changed over the past centu.docx
How has the physician-patient relationship changed over the past centu.docx
 
How is treasury stock shown on the balance sheet- Treasury stock is no.docx
How is treasury stock shown on the balance sheet- Treasury stock is no.docxHow is treasury stock shown on the balance sheet- Treasury stock is no.docx
How is treasury stock shown on the balance sheet- Treasury stock is no.docx
 
How is repetition of components different from redundancy- How are the.docx
How is repetition of components different from redundancy- How are the.docxHow is repetition of components different from redundancy- How are the.docx
How is repetition of components different from redundancy- How are the.docx
 
How is communication a part of best practices-Solution4 Leadership Bes.docx
How is communication a part of best practices-Solution4 Leadership Bes.docxHow is communication a part of best practices-Solution4 Leadership Bes.docx
How is communication a part of best practices-Solution4 Leadership Bes.docx
 
How does the National Society of Professional Engineers Code of Ethics.docx
How does the National Society of Professional Engineers Code of Ethics.docxHow does the National Society of Professional Engineers Code of Ethics.docx
How does the National Society of Professional Engineers Code of Ethics.docx
 
how does cinchona catalyst workSolutionCatalyst working- 1) Catalyst i.docx
how does cinchona catalyst workSolutionCatalyst working- 1) Catalyst i.docxhow does cinchona catalyst workSolutionCatalyst working- 1) Catalyst i.docx
how does cinchona catalyst workSolutionCatalyst working- 1) Catalyst i.docx
 
How does the evolution of attack tools affect existing systems- What s.docx
How does the evolution of attack tools affect existing systems- What s.docxHow does the evolution of attack tools affect existing systems- What s.docx
How does the evolution of attack tools affect existing systems- What s.docx
 
How do you think the DATA Act will impact governmental entities and st.docx
How do you think the DATA Act will impact governmental entities and st.docxHow do you think the DATA Act will impact governmental entities and st.docx
How do you think the DATA Act will impact governmental entities and st.docx
 
How do I determine the Rf Value with just the information I have provi.docx
How do I determine the Rf Value with just the information I have provi.docxHow do I determine the Rf Value with just the information I have provi.docx
How do I determine the Rf Value with just the information I have provi.docx
 
How do I print a list of payroll checks for the calendar year to date.docx
How do I print a list of payroll checks for the calendar year to date.docxHow do I print a list of payroll checks for the calendar year to date.docx
How do I print a list of payroll checks for the calendar year to date.docx
 
How does SO2 in the atmosphere affect global warming- In general- what.docx
How does SO2 in the atmosphere affect global warming- In general- what.docxHow does SO2 in the atmosphere affect global warming- In general- what.docx
How does SO2 in the atmosphere affect global warming- In general- what.docx
 
How does an awareness of the historical link between the United States.docx
How does an awareness of the historical link between the United States.docxHow does an awareness of the historical link between the United States.docx
How does an awareness of the historical link between the United States.docx
 
How do you utilize PM software to manage team activities-SolutionProje.docx
How do you utilize PM software to manage team activities-SolutionProje.docxHow do you utilize PM software to manage team activities-SolutionProje.docx
How do you utilize PM software to manage team activities-SolutionProje.docx
 
how do the different architectures of a polymer affect their propertie.docx
how do the different architectures of a polymer affect their propertie.docxhow do the different architectures of a polymer affect their propertie.docx
how do the different architectures of a polymer affect their propertie.docx
 
How do phospholipid bilayers maintain specific concentrations of solut.docx
How do phospholipid bilayers maintain specific concentrations of solut.docxHow do phospholipid bilayers maintain specific concentrations of solut.docx
How do phospholipid bilayers maintain specific concentrations of solut.docx
 
how would a police department exhibit different structure and procedur.docx
how would a police department exhibit different structure and procedur.docxhow would a police department exhibit different structure and procedur.docx
how would a police department exhibit different structure and procedur.docx
 
How will you close your knowledge gap- Is your mind geared to re-think.docx
How will you close your knowledge gap- Is your mind geared to re-think.docxHow will you close your knowledge gap- Is your mind geared to re-think.docx
How will you close your knowledge gap- Is your mind geared to re-think.docx
 
How to do web application testing on a website checking it-'s (Functio.docx
How to do web application testing on a website checking it-'s (Functio.docxHow to do web application testing on a website checking it-'s (Functio.docx
How to do web application testing on a website checking it-'s (Functio.docx
 
How to make a Mic 1 L2 instruction set that takes in user input throug.docx
How to make a Mic 1 L2 instruction set that takes in user input throug.docxHow to make a Mic 1 L2 instruction set that takes in user input throug.docx
How to make a Mic 1 L2 instruction set that takes in user input throug.docx
 

Último

Último (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

How to turn this recursive function into an iterative function- #inclu.docx

  • 1. How to turn this recursive function into an iterative function: #include <iostream> #include <cmath> using namespace std; void get_divisors(int n); int main() { int n = 0; cout << "Enter a number and press ENTER: "; cin >> n; get_divisors(n); cout << endl; return 0; } void get_divisors(int n) { int i; double sqrt_of_n = sqrt(n); for (i = 2; i <= sqrt_of_n; i++) if (n % i == 0) { cout<<i<<","; get_divisors(n / i); return; } cout << n; } Solution Above function is a recursive functive which means a function which calls it self.
  • 2. function calling it self should manipulated for iterative function iterative function #include <iostream>//header file for input ut function using namespace std;//it tells the compiler to link std name space void get_divisors(int n);//function declaration int main() { int n = 0; cout << "Enter a number and press ENTER: "; cin >> n;//keyboard inputting get_divisors(n);//calling function cout << endl; return 0; } void get_divisors(int n) {//function definition int i; for (i = 2; i <= n; i++){ if (n % i == 0) { cout<<i<<","; } } return; cout << n; } output Enter a number and press ENTER: 60 2,3,4,5,6,10,12,15,20,30,60,