I've been working on a program and have hit a roadblock. I know conceptually what to do, but I'm having trouble implimenting it.
Ok, so what it's suppose to do is take them randomly generated numbers that have been put in arrayints and factor them. This right now for an example if the number is 100 it will print out: 2 * 50 4 * 25 5 * 20 10 * 10 1 * 100. What I want it to print out is: 2 * 2 * 5 * 5 (doesn't have to be in that order I guess) but I'm having trouble with this. It's probably something simple that I'm overlooking but if anybody could help with it that would be great. Thanks!
Conceptually, I know I need to take the number I get from the if((arrayints % j) == 0 && arrayints != j) and run it back through here. Ex. 100, you would get factor1 = 2, and factor2 = 50, so I need to somehow run factor2 back through the if statement without incrementing j. (hopefully you understand what I mean lol)

for (int i = 0; i < integerIn; i++)
{
cout << "The factors for " << arrayints[i] << " are: ";
for (int j = 2; j <= floor (sqrt((double)integerIn)); j++)
{
if ((arrayints[i] % j) == 0 && arrayints[i] != j)
{
factor1 = j;
factor2 = arrayints[i]/j;
cout << factor1 << " * " << factor2 << " ";
}
}
factor1 = 1;
factor2 = arrayints[i];
cout << factor1 << " * " << factor2 <<endl;
}

First, please learn to Format your code so we can read it easier.

You need to store each factor in an array as you generate them. Using your 100, you first generate 2. Save it in the array and you have 50 left. Run the loop again on the 50 and you'll get 2 again. Store it with the other 2. You have 25 left. Again and you'll get the 5. Store it. You loop on the 5 and discover it can't be reduced. Store it with the rest then output the array.

First,for a a number in an array call it as

int num;
//Your expression for evaluating control variable 
//certainly doesn't give all the factors,referring to
//for (int j = 2; j <= floor (sqrt((double)integerIn)); j++)
//try for integerIn=999;
//999's square root lying between 31 and 32 while one of its prime
//factors(read major) being '37'
//999 = 3*3 *3 *37
//Doesn't mean mine is the fastest(just works)
//Store the factors if its required

 for (int j=2 ; j<= num; j++)
{ //Just as WaltP suggested go on dividing unless you 
   //get a prime number
  while (num % j==0)
    {
      cout <<num;
      num/=j ;
    }
}

You want to actually find out the PRIME factors i.e. expand the number as the product of prime numbers(2,3,5,7...)
So the easiest way would be what has been told by WaltP.

Indeed waltp's way i the easiest to find prime factors of a number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.