SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a program that prompts the user to enter a positive integer and displays all its smallest
factors in decreasing order. For example, if the integer is 120, the smallest factors are displayed
as 5, 3, 2, 2, 2. Use the StackOflntegers class to store the factors (e.g., 2, 2, 2, 3, 5) and retrieve
and display them in reverse order.
Solution
#include
#include
using namespace std;
class StackOfIntegers {
int s[100],top;
public:
StackOfIntegers() {
top=-1;
}
void put(int n) {
top++;
s[top]=n;
}
int get() {
if(top==-1) {
return -1;
}
return s[top--];
}
}stack;
void primeFactors(int n) {
while (n%2 == 0) {
stack.put(2);
n = n/2;
}
for (int i=3; i<=sqrt(n); i=i+2) {
while (n%i==0) {
stack.put(i);
n = n/i;
}
}
if (n > 2)
stack.put(n);
}
int main() {
int n;
cout<<"Enter a positive integer : ";
cin>>n;
primeFactors(n);
int f;
f=stack.get();
while(f>0) {
cout<

More Related Content

Similar to Write a program that prompts the user to enter a positive integer and.pdf

DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 

Similar to Write a program that prompts the user to enter a positive integer and.pdf (20)

array.ppt
array.pptarray.ppt
array.ppt
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
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
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
array
arrayarray
array
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Write a Java application that asks for an integer and returns its fac.pdf
Write a Java application that asks for an integer and returns its fac.pdfWrite a Java application that asks for an integer and returns its fac.pdf
Write a Java application that asks for an integer and returns its fac.pdf
 
Vcs16
Vcs16Vcs16
Vcs16
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Array Cont
Array ContArray Cont
Array Cont
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 

More from feetshoemart

Convert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdfConvert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdf
feetshoemart
 
Computer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdfComputer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdf
feetshoemart
 
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdfDiscussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
feetshoemart
 
wk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdfwk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdf
feetshoemart
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
feetshoemart
 
The process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdfThe process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdf
feetshoemart
 
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdfTerrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
feetshoemart
 
What do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdfWhat do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdf
feetshoemart
 
Using following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdfUsing following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdf
feetshoemart
 
11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf
feetshoemart
 

More from feetshoemart (20)

Convert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdfConvert the following expressions from infix to Reverse Polish Notati.pdf
Convert the following expressions from infix to Reverse Polish Notati.pdf
 
Andy and Joan are married and under 65 years of age. They have two c.pdf
Andy and Joan are married and under 65 years of age. They have two c.pdfAndy and Joan are married and under 65 years of age. They have two c.pdf
Andy and Joan are married and under 65 years of age. They have two c.pdf
 
Compare the methods by which the Parsees of India benefitted from th.pdf
Compare the methods by which the Parsees of India benefitted from th.pdfCompare the methods by which the Parsees of India benefitted from th.pdf
Compare the methods by which the Parsees of India benefitted from th.pdf
 
Computer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdfComputer Forensics MethodologiesList them and explain each one.P.pdf
Computer Forensics MethodologiesList them and explain each one.P.pdf
 
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdfDocument2- Word (Product Activation Failed) Mailings Review View Tell.pdf
Document2- Word (Product Activation Failed) Mailings Review View Tell.pdf
 
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdfDiscussion 1 Choose one of the Opinion Poll questions (click here f.pdf
Discussion 1 Choose one of the Opinion Poll questions (click here f.pdf
 
calculate the hydrogen ion concentration in mol for the following s.pdf
calculate the hydrogen ion concentration in mol for the following s.pdfcalculate the hydrogen ion concentration in mol for the following s.pdf
calculate the hydrogen ion concentration in mol for the following s.pdf
 
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdfAt a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
At a certain temperature, 0.5011 mol of N2 and 1.781 mol of H2 are pl.pdf
 
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdfConsider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
Consider mappinp, phi Z rightarrow R, defined as phi(x) = 2x, where .pdf
 
wk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdfwk3 reply to prof.I need help with this question below a 200 words.pdf
wk3 reply to prof.I need help with this question below a 200 words.pdf
 
Write a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdfWrite a function called countElements that counts the number of times.pdf
Write a function called countElements that counts the number of times.pdf
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
 
There is no video, All you have to do is show how you reference the .pdf
There is no video, All you have to do is show how you reference the .pdfThere is no video, All you have to do is show how you reference the .pdf
There is no video, All you have to do is show how you reference the .pdf
 
The process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdfThe process of Imperialism and Colonialism played a role in the esta.pdf
The process of Imperialism and Colonialism played a role in the esta.pdf
 
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdfTerrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
Terrain AnalysisBackgroundAircraft frequently rely on terrain el.pdf
 
What do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdfWhat do viral genomes look like compared to those of living organism.pdf
What do viral genomes look like compared to those of living organism.pdf
 
Verify the identity Verify the identity by transforming the left-han.pdf
Verify the identity Verify the identity by transforming the left-han.pdfVerify the identity Verify the identity by transforming the left-han.pdf
Verify the identity Verify the identity by transforming the left-han.pdf
 
Using following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdfUsing following main file and solve the taskInst.pdf
Using following main file and solve the taskInst.pdf
 
Suppose there are 14 children trying to form two teams with seven ch.pdf
Suppose there are 14 children trying to form two teams with seven ch.pdfSuppose there are 14 children trying to form two teams with seven ch.pdf
Suppose there are 14 children trying to form two teams with seven ch.pdf
 
11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf11. Define a simple deformable model to detect a half-circular shape.pdf
11. Define a simple deformable model to detect a half-circular shape.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
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
 
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
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
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
 
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.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Write a program that prompts the user to enter a positive integer and.pdf

  • 1. Write a program that prompts the user to enter a positive integer and displays all its smallest factors in decreasing order. For example, if the integer is 120, the smallest factors are displayed as 5, 3, 2, 2, 2. Use the StackOflntegers class to store the factors (e.g., 2, 2, 2, 3, 5) and retrieve and display them in reverse order. Solution #include #include using namespace std; class StackOfIntegers { int s[100],top; public: StackOfIntegers() { top=-1; } void put(int n) { top++; s[top]=n; } int get() { if(top==-1) { return -1; } return s[top--]; } }stack; void primeFactors(int n) { while (n%2 == 0) { stack.put(2); n = n/2; } for (int i=3; i<=sqrt(n); i=i+2) { while (n%i==0) { stack.put(i);
  • 2. n = n/i; } } if (n > 2) stack.put(n); } int main() { int n; cout<<"Enter a positive integer : "; cin>>n; primeFactors(n); int f; f=stack.get(); while(f>0) { cout<