
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Prime Factor in C++ Program
Prime Factor is a prime number which is the factor of the given number.
Factor of a number are the numbers that are multiplied to get the given number.
Prime Factorisation is the process of recursively dividing the number with its prime factors to find all the prime factors of the number.
Example : N = 120 Prime factors = 2 5 3 Factorization : 2 * 2 * 2 * 3 * 5
Some points to remember about prime factors of a number
- Set of prime factors of a number is unique.
- Factorization is important in many mathematical calculations like divisibility, finding common denominators, etc.
- It’s an important concept in cryptography.
Program to find prime factors of a number
Example
#include <iostream> #include <math.h> using namespace std; void printPrimeFactors(int n) { while (n%2 == 0){ cout<<"2\t"; n = n/2; } for (int i = 3; i <= sqrt(n); i = i+2){ while (n%i == 0){ cout<<i<<"\t"; n = n/i; } } if (n > 2) cout<<n<<"\t"; } int main() { int n = 2632; cout<<"Prime factors of "<<n<<" are :\t"; printPrimeFactors(n); return 0; }
Output
Prime factors of 2632 are :2 2 2 7 47
- Related Articles
- Python Program for Find largest prime factor of a number
- C Program for Find largest prime factor of a number?
- Java Program to find largest prime factor of a number
- What is prime factor form?
- C Program for Find the largest prime factor of a number?
- Prime factor array of a Number in JavaScript
- k-th prime factor of a given number in java
- Finding the largest prime factor of a number in JavaScript
- Find largest prime factor of a number using C++.
- Explain prime factorization of numbers using factor trees with example.
- Find sum of a number and its maximum prime factor in C++
- Prime number program in Java.
- Java program to check for prime and find next Prime in Java
- Recursive program for prime number in C++
- Count all the numbers less than 10^6 whose minimum prime factor is N C++

Advertisements