Thread: code for finding all prime factors of a number using stacks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    code for finding all prime factors of a number using stacks

    This assignment is working with stacks.
    The problem posed:
    Write a program that uses a stack to print the prime factors of a positive integer in decending order.
    My algorithm is:
    1. ask for a number
    2. push 1 into the stack
    3. from divisor = 2 through divisor = number
    if number % divisor == 0 && divisor is prime
    push divisor into stack
    4. if the top item in the stack is 1
    print number and stack
    otherwise print stack until stack is empty
    4. ask for another number or quit

    The code I have so far works except for 3. finding the 'prime' factors. But, I am getting some numbers that aren't prime with my for loop condition and can't figure out a way to get all cases. Is there something very simple that I am missing here? For example 50 gives me 25 as a prime.
    Code:
    #include <iostream>
    #include <stack>
    using namespace std;
    
    void displayBanner();
    void displayQuestion();
    
    int main()
    {
    	int number,
    		halfNumber;
    	char userResponse;
    	stack<int> smallPrimeFac;
    
    	displayBanner();
    	displayQuestion();
    	cin >> userResponse;
    	
    	while (userResponse == 'Y' || userResponse == 'y')
    	{	
    		cout << "Enter the number: ";
    		cin >> number;
    		cout << endl;
    		cout << "\nThe prime factors of " << number << " are: ";
    		smallPrimeFac.push(1);
    		if((number % 2) == 0)
    			smallPrimeFac.push(2);
    		for (int i = 3; i < number - 1; i++)
    		{
    			if((number % i) == 0 &&  i % 2 != 0 )
    				smallPrimeFac.push(i);
    		}//end for
    		if (smallPrimeFac.size() == 1)
    				cout << number << " ";
    		while (!smallPrimeFac.empty())
    		{
    			cout << smallPrimeFac.top() << " ";
    			smallPrimeFac.pop();
    		}//end print stack while
    		cout << endl;
    		displayQuestion();
    		cin >> userResponse;
    	}//end while
    	return 0;
    }//end main
    
    void displayBanner()
    {
    	cout << "\nThis program requests you to input a non-negative number."
    		 << "\nIt then calculates all the prime factors of the number,"
    		 << "\nand prints the factors out in decending order" << endl;
    }
    
    void displayQuestion()
    {
    	cout << "\nWould you like to enter a number to find it's prime factors?"
    		 << "\n\   'Y' to continue, anything else to quit:  ";
    }
    As always, your help is most appreciated.
    Last edited by clegs; 11-24-2007 at 11:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More fun with the chinese prime number algorigthm
    By kryonik in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 04:41 PM
  2. Prime Number stops after 29, but why?
    By Daveo in forum C Programming
    Replies: 22
    Last Post: 09-17-2004, 10:55 AM
  3. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM