Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program That uses a stack to print the prime factors of a positive i

ID: 3661488 • Letter: W

Question

Write a C++ program
That uses a stack to print the prime factors of a positive integer in descending order.


I need original code and not something copied exactly off the Internet

Not a copy of the code from any of these sites or elsewhere online.

http://www.dreamincode.net/forums/topic/320584-use-a-stack-to-print-the-prime-factors-of-a-positive-integer-in-descen/
http://www.cplusplus.com/forum/beginner/129984/
https://www.gidforums.com/t-9497.html
http://cboard.cprogramming.com/cplusplus-programming/96202-code-finding-all-prime-factors-number-using-stacks.html

Just looking for unique code that can't be found online at a bunch of sites already

Thanks much
Jason

Explanation / Answer

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>

void main()
{int n;
cout<<"ënter the number ";
cin>>n;
primefactors(n);

void primeFactors(int number)

{

               stack<int> primenumbersStack;

               assert(number >= 0);

               for(int p = 2; p <= number; p++) /*find factors >1 */

               {

                               if(number % p == 0) /* It is a factor > 1 */

                               {

                                              bool prime = true;

                                              for(int q = 2; q < p; q++) /* to check if factor is prime */

                                              {

                                                             if(p % q == 0) /* factor is not prime */

                                                                            prime = false;

                                              }

                                              if(prime)

                                                             primenumbersStack.push(i); /*if it is prime push it */

                               }

               }

               while(!primenumbersStack.empty())

               {

                               cout << primenumbersStack.top() << endl;

                               primenumbersStack.pop(); /* pop the stack to get the prime factors */

               }

}

now we can sort the stack seperately:

using the following functions:

/* Recursive function to insert an item x in sorted way */

void sortedInsert(struct stack **primenumbersstack, int x)

{

    // Base case: Either stack is empty or newly inserted

    // item is greater than top (more than all existing)

    if (isEmpty(*primenumbersstack) || x > top(*s))

    {

        push(primenumberstack, x);

        return;

    }

    // If top is greater, remove the top item and recur

    int temp = pop(primenumberstack);

    sortedInsert(primenumberstack, x);

    // Put back the top item removed earlier

    push(primenumberstack, temp);

}

// Function to sort stack

void sortStack(struct stack **primenumberstack)

{

    // If stack is not empty

    if (!isEmpty(*primenumberstack))

    {

        // Remove the top item

        int x = pop(primenumberstack);

        // Sort remaining stack

        sortStack(primenumberstack);


OUTPUT:

enter number:35

prime factors: 7,5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote