63 799
Assignments Done
99,4%
Successfully Done
In August 2018

Answer to Question #7382 in C++ for Merry

Question #7382
Hi, I have no ideam why my program crashes. It is supposed to take in a number and print its decomposition into a product of prime factors.
example N = 48 then the program prints 2^4*3^1
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> factors;
int N, count=0;
cout << "Please enter an integer number greater than 0";
cin >> N;
while (N <= 0) {
cout << "Error. Number out of range. Please re-enter a valid number.";
cin >> N; }

while ((N %2) == 0) {
factors.push_back(2);
N = N / 2; }


for (int f=3; f <= N; f += 2) {
while (((N%f) == 0) && (N != f)) {
factors.push_back(f);
N = N / f;
if (N == f)
factors.push_back(N);} }

for ( int i= 0; i < factors.size(); i++){
if ((factors[i]) == (factors[i+1]))
{count++;}
else {
cout << factors[i] << "^" << count << "*";
count = 1;
}
}
cout << factors[factors.size()] << "^" << count;
return 0; }
Expert's answer

Not answered

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions