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

PDF
You will be implementing the following functions. You may modify the.pdf
DOCX
Write C++ program that when given a value N(read from cin)- produces a.docx
PDF
Factors.javaimport java.io.; import java.util.Scanner; class .pdf
PDF
C++ TUTORIAL 2
PPTX
Assignement of c++
PDF
Please use the code below and make it operate as one program- Notating.pdf
PPTX
Example Programs of CPP programs ch 1.pptx
PDF
Write a Java application that asks for an integer and returns its fac.pdf
You will be implementing the following functions. You may modify the.pdf
Write C++ program that when given a value N(read from cin)- produces a.docx
Factors.javaimport java.io.; import java.util.Scanner; class .pdf
C++ TUTORIAL 2
Assignement of c++
Please use the code below and make it operate as one program- Notating.pdf
Example Programs of CPP programs ch 1.pptx
Write a Java application that asks for an integer and returns its fac.pdf

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

PDF
54602399 c-examples-51-to-108-programe-ee01083101
DOCX
Assignement of programming & problem solving u.s ass.(1)
PPTX
oops practical file.pptx IT IS A PRACTICAL FILE
PDF
C++ practical
PDF
10 template code program
PPTX
powerpoint presentation on square root factorising
PDF
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
PPTX
Lab-11-C-Problems.pptx
DOCX
C++ file
DOCX
C++ file
PDF
Oot practical
PPTX
Lab01.pptx
DOCX
Basic Programs of C++
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
C++ TUTORIAL 4
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
Backtracking based integer factorisation, primality testing and square root c...
PDF
PF LAB ASSIGNMENT
PDF
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
PDF
C++ TUTORIAL 3
54602399 c-examples-51-to-108-programe-ee01083101
Assignement of programming & problem solving u.s ass.(1)
oops practical file.pptx IT IS A PRACTICAL FILE
C++ practical
10 template code program
powerpoint presentation on square root factorising
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
Lab-11-C-Problems.pptx
C++ file
C++ file
Oot practical
Lab01.pptx
Basic Programs of C++
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
C++ TUTORIAL 4
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
Backtracking based integer factorisation, primality testing and square root c...
PF LAB ASSIGNMENT
Engineering Problem Solving With C++ 4th Edition Etter Solutions Manual
C++ TUTORIAL 3
Ad

More from feetshoemart (20)

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

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Lesson notes of climatology university.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Sports Quiz easy sports quiz sports quiz
Lesson notes of climatology university.
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Computing-Curriculum for Schools in Ghana
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

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<