SlideShare a Scribd company logo
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 (20)

PDF
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
PPT
array.ppt
DeveshDewangan5
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
PDF
DATA STRUCTURE USING C & C++
mustkeem khan
 
PDF
DSU C&C++ Practical File Diploma
mustkeem khan
 
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
PDF
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
DOCX
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
PDF
Data Structure using C
Bilal Mirza
 
PPT
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
PPT
array
teach4uin
 
DOC
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
PPTX
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
PDF
Write a Java application that asks for an integer and returns its fac.pdf
hadpadrrajeshh
 
DOCX
PRACTICAL COMPUTING
Ramachendran Logarajah
 
DOCX
Array Cont
Ashutosh Srivasatava
 
DOCX
Cpds lab
praveennallavelly08
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
LectureSlidData_sturcture_algorithm_v2.pptx
abhaysharma999437
 
array.ppt
DeveshDewangan5
 
Array,MULTI ARRAY, IN C
naveed jamali
 
DATA STRUCTURE USING C & C++
mustkeem khan
 
DSU C&C++ Practical File Diploma
mustkeem khan
 
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
Data Structure using C
Bilal Mirza
 
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
array
teach4uin
 
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
Write a Java application that asks for an integer and returns its fac.pdf
hadpadrrajeshh
 
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 

More from feetshoemart (20)

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

Recently uploaded (20)

PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PDF
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
digestive system for Pharm d I year HAP
rekhapositivity
 
The-Beginnings-of-Indian-Civilisation.pdf/6th class new ncert social/by k san...
Sandeep Swamy
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
community health nursing question paper 2.pdf
Prince kumar
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
Ad

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<