problem

This is a discussion on problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to find out how to create a program which will print the prime factors of a ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    95

    problem

    Hi, I am trying to find out how to create a program which will print the prime factors of a number inputed. my code is
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {   int x;
        cin>>x;
        int i=1;
        while(i<x)
        {
            if (x%i==0)
            {
                cout<<i<<"\n";
                x=x/i;
                 
    
    
            }
            else 
            {i++;
                      }
    
    cout<<x;
    
    
        }
    
    
    
    
    }
    is it possible to make a code that would add all this factors??
    Last edited by Tamim Ad Dari; 01-12-2013 at 07:08 AM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,835
    For this to work properly i needs to be a prime. You should start with a decent list of primes, if you don't want to implement a prime number test.

    Also a good compile does not mean that your program is free of logic errors. Compiling doesn't detect those.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 06:49 AM
  2. Replies: 1
    Last Post: 12-07-2012, 09:00 AM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21