C++: Find Prime Factors of a Number.

Given a number, we have to find it's prime factors.

Prime Factorization is finding which prime numbers multiply together to make the original number.[mathsisfun]

You can find many such projects from here.

Here is the C++ implementation to find prime factors.

#include <iostream>
#include <vector>

std::vector<int> find_prime_factors(int n)
    std::vector<int> result;
    for (int i = 2; i <= n; i++)
        while (n % i == 0)
            n = n/i;
    return result;

int main()
    int n;
    std::cout << "Enter number\n";
    std::cin >> n;
    std::vector<int> prime_factors;
    prime_factors = find_prime_factors(n);
    std::cout << "Prime Factors of " << n << ":\n";
    for (int i = 0; i < prime_factors.size(); i++)
        std::cout << prime_factors[i] << " ";
    std::cout << "\n";
You can find this code on GitHub.



You may also like 
C++: Tic Tac Toe 
C++: Simple Pendulum Animation on Ubuntu Machine
C++: Add binary numbers
Check if the String is Palindrome (C++ & Java Program)
Top 10 Web Sites Every Programmer Should Visit Daily