finding prime factors of a given number c++ without functions -
i need find prime factors of number user inputs
example:
enter number : 1430. prime factors of 1430 2,5,11,13
i prefer not using function haven't covered yet
here code
#include <iostream> #include <iomanip> #include <cmath> using namespace std ; int main () { int count, , i2 ; int userinput, prime ; bool flag = false ; cout << "enterr: " ; cin >> userinput ; cout << "the prime factors are: " ; (i = 2; < userinput ; i++) { if (userinput % == 0) // loop check counter (i) multiple of userinput { prime = i; // next loop check multiple prime number for( i2 = 2; i2< ceil(sqrt(prime)) ; i2++) { if (prime % i2 == 0) flag = true ; } } if (flag == false ) cout << << ", " ; flag = false ; } cout<< endl ; return 0 ; }
my output ignores second loop , outputs integers less userinput
i able create piece of code checks if number prime number here :
#include <iostream> #include <iomanip> #include <cmath> using namespace std ; int main () { int userinput, prime ; int ; bool flag = false ; cin >> userinput ; for( = 2; < static_cast<int>(sqrt(userinput) + 1) ; i++) { if (userinput % == 0) flag = true; } if (flag == false ) cout << "number prime" << endl ; else cout << "number not prime " << endl ; return 0 ; }
sorry if there errors. i'm still beginner @ c++
you're there - need move block
if (flag == false ) cout << << ", " ; flag = false ;
into
if (userinput % == 0)
block (you want print numbers divisors of input number), , should fine.
Comments
Post a Comment