Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,119 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,781 people online right now. Registration is fast and FREE... Join Now!




C++ Prime Factors Division Loop

 
Reply to this topicStart new topic

C++ Prime Factors Division Loop

lewicastles
13 May, 2007 - 06:22 AM
Post #1

New D.I.C Head
*

Joined: 13 May, 2007
Posts: 1


My Contributions
Hi, im new to both the C++ language and this forum so bear with any of my amatuer mistakes, i have written the below code and it appears to build ok in visual studio 2005. The actual program bears no results, could someone please help me find where the problem lies; the program is supposed to factorise a number i into its prime factors (stored in vector x) then print them.

CODE

// printstl.cpp

# include <iostream >  // Declares std :: cout , etc.
# include <vector >     // Declares std :: vector <T >.
# include "printstl.h"

using std::cout;
using std::endl;
using std::vector;


int main(void) {

// Declare a vector to hold the prime factors.
vector <int> x;

// Declare and assign variables
    i = 12;
    m = i;
    f = 2;


// LOOP: while m>1, if remainder of m/f is zero,
//       then m=m/f and add f to vector, otherwise increase f


        while(m > 1);
            {
            
            if (m%f == 0)
                {
                m = (m/f);
                x.push_back(f);
                }
            else
                {
                f++;
                }
    
            }

cout << "Elements of x: ";
vector <int>:: iterator iter;
for ( iter = x. begin (); iter != x.end (); iter ++) {
cout << (* iter ) << " ";
}
cout << endl;
}


thank you for any replys,
lewis

This post has been edited by NickDMax: 14 May, 2007 - 07:42 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month