Program to Find Prime Factors of a Number in C++ [How to] <div dir="ltr" style="text-align: left;" trbidi="on"><h2 style="text-align: center;"><span style="color: #3d85c6; font-family: "trebuchet ms" , sans-serif;"><span style="background-color: white;">How to Find Prime Factors [C++]</span></span></h2><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><span style="color: #93c47d;">Goal:- </span>Program for Finding Prime factors of a given number.</span></span></h3><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><span style="color: #93c47d;">Method:- </span></span></span><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;">There are many methods which you can implement but as this post is only for beginners so I will try to keep it simple.</span><span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif; font-weight: normal;"> </span></h3><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><span style="color: #93c47d;">Basic Idea:- </span> If you are a beginner to coding or wants to become good coder the first step to work on your mindset.The way we approach the problem represent how good coder we are. So before you code anything try to understand the problem by dividing it into as many parts as you can. So let's start with the Prime Factors. </span></span></h3><div><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;">Before We start writing a program which find prime factors of a number we must learn what is the prime factor or How we calculate them in math.</span></span></h3></div><div><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><b><span style="color: #6aa84f;">Prime Numbers- </span>Numbers which are only divisible by 1 or itself. Like 5,2,3</b></span></span></h3><h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><b><span style="color: #6aa84f;">Prime Factors- </span>Breaking down a Number into combination of Prime Numbers.</b></span></span></h3></div><div><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;"><b><br /> </b></span></span></div><div><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;"></span></span><br /> <h3 style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;"><span style="color: #93c47d;">Example:- </span>Here we will break down 24 into its Prime Factors-</span></span></h3><div><h4 style="text-align: left;"></h4><h3 style="text-align: left;"></h3><h3 style="text-align: left;"></h3><h4 style="text-align: left;"><ul style="text-align: left;"><li>24 -- 2 X <span style="background-color: yellow; font-family: "trebuchet ms" , sans-serif; font-weight: normal;">12 </span></li> </ul><ul style="text-align: left;"><li>24 -- 2 X <span style="background-color: yellow; font-family: "trebuchet ms" , sans-serif; font-weight: normal;">2 X 6</span><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;"> (2 is a prime number but 12 isn't )</span></li> </ul><ul style="text-align: left;"><li>24 -- 2 X 2 X <span style="background-color: yellow; font-family: "trebuchet ms" , sans-serif; font-weight: normal;">2 X 3</span><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;"> (Similar to above 6 wasn't a prime number)</span></li> </ul><ul style="text-align: left;"><li>24 -- 2 X 2 X 2 X 3 (All numbers are the prime number.)</li> </ul></h4><ol style="text-align: left;"></ol><ol style="text-align: left;"></ol><ol style="text-align: left;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-family: "trebuchet ms" , sans-serif; font-weight: normal;"> </span></span></ol></div></div><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br /> <!-- between_post2 --><br /> <ins class="adsbygoogle" data-ad-client="ca-pub-1000631018247743" data-ad-slot="8769746517" style="display: inline-block; height: 90px; width: 728px;"></ins><br /> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><br /> <br /> <h3 style="text-align: left;"><span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif; font-weight: normal;">Program:-</span></h3><span style="font-size: large;"><script class="brush: java" type="syntaxhighlighter"> int main() { cout << "Enter a number: "; int num; cin >> num; cout<<"The prime factors are: "; for (int i=2; i <= num; i++) { while(num % i == 0) { num = num/i; // "num" divided by "i" is now "num" cout<<i<<" "; } } } </script></span> <h3 style="text-align: left;"> <span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;">Output:-</span></span></h3><div style="text-align: left;"> <div style="text-align: left;"> <span style="font-family: "trebuchet ms" , sans-serif; font-size: large;"><span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span style="font-weight: normal;">Enter a number:24</span></span></span> <span style="font-family: "trebuchet ms" , sans-serif; font-size: large;"><span style="font-weight: normal;">The prime factors are: 2 2 2 3</span></span></div><div style="text-align: left;"> <span style="font-size: large;"><span style="font-family: "arial" , "helvetica" , sans-serif; font-weight: normal;"> </span></span></div><div style="text-align: left;"> <span style="font-size: large;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><span style="color: red;">This program is requested by our subscriber. If you also want to request any project or program.Please send us a request via </span><a href="http://codieehome.blogspot.in/p/contact-us_15.html" target="_blank"><span style="color: lime;">Contact us</span></a><span style="color: red;"> or our </span><a href="https://www.facebook.com/codieehome" target="_blank"><span style="color: lime;">Facebook</span></a><span style="color: red;"> page</span>.</span></span></div></div><div style="text-align: left;"> <span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: large; font-weight: normal;"> </span></span></div><div class="separator" style="clear: both; text-align: center;"> </div><div class="separator" style="clear: both; text-align: center;"> </div><div style="text-align: left;"> <span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: large; font-weight: normal;"> </span></span></div><div style="text-align: left;"> <span style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span style="font-size: large; font-weight: normal;"></span></span></div><h3 style="text-align: left;"> <span class="Apple-style-span" style="color: #93c47d; font-family: "trebuchet ms" , sans-serif;"><span class="Apple-style-span" style="font-size: large; font-weight: normal;"></span></span></h3><div style="text-align: left;"> <h3 style="background-color: white; color: #333333; font-family: bitter, serif, sans-serif; font-weight: normal; margin: 0px 0px 10px;"> <span style="font-size: large;"><span style="color: red; line-height: 19.1875px;">If you want to share more information about topic discussed above or find anything incorrect Please do comments.</span> </span></h3></div><span style="font-family: "bitter" , serif , sans-serif; font-size: large;"><b style="color: #333333;">For more Programs Stay Connected</b><span style="color: #333333;">..</span><a href="http://www.facebook.com/codieehome" target="_blank"><span style="color: red;">Like</span></a><span style="color: #333333;"> us on Facebook.</span></span></div> Program to Find Prime Factors of a Number in C++ [How to] How to Find Prime Factors [C++] Goal:- Program for Finding Prime factors of a given number. Method:- There are many methods which you can... + Read more »