SlideShare a Scribd company logo
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,

More Related Content

Similar to 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 to 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
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
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
 

More from 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
 

More from 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
 

Recently uploaded

Neurulation and the formation of the neural tube
Neurulation and the formation of the neural tubeNeurulation and the formation of the neural tube
Neurulation and the formation of the neural tube
SaadHumayun7
 

Recently uploaded (20)

How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Neurulation and the formation of the neural tube
Neurulation and the formation of the neural tubeNeurulation and the formation of the neural tube
Neurulation and the formation of the neural tube
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 

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,