• C

# SEMANTIC ERROR with while loop in C program

I'm using a while loop to calculate prime factors. I'm getting the prime factors, but I'm getting TWO sets of each one. So, for 100 (primes should be just 2 and 5, I'm getting 2 2 5 5. Could someone please take a look at my code (especially my while loop) and suggest where my error might be? Any other general suggestions on how to improve what I've written would also be appreciated.

------------------------------------MY CODE-------------------------------------------------------

// prime factorizarion.

#include <stdio.h>

// isPrime function
// return 1 if number prime
int isPrime(int n)
{
int i = 2;
while ( i <= n/2 )
{      if ( (n % i) == 0 )
//this is not a prime
return 0;
i++;
}
// this is a prime
return 1;
}

// prime factor a number
int main()
{
int prime; // prime divider
int num; // number to find primes factors of

// get number
printf("Enter a number greater than 1: ");
scanf("%d",&num);

while(num > 1)
{
prime = 2; // start with lowest prime

// To prime factor a number, begin dividing by the smallest possible prime
// and continue until the quotient is a prime number.
while(!isPrime(num))
{
// test if num is prime
if((num % prime)==0)
{
printf("%d ",prime);  // print out factor
num = num / prime;
}

// find next prime
else
{
prime++;

while(!isPrime(prime))
{
prime++;
}
}

}

// print out last factor
printf("%d\n",num);

// get next  number
printf("Enter a number greater than 1: ");
scanf("%d",&num);
}

printf("\n");

return 1;
}
Asked:
###### Who is Participating?

Improve company productivity with a Business Account.Sign Up

x

Commented:
Hi computerese,

Your code, when presented with 100 will go through the following steps:

Is 2 a prime factor of 100? Yes! Print '2' and set number to 50.
Is 2 a prime factor of 50? Yes! Print '2' and set number to 25.
Is 2 a prime factor of 25? No!
Is 3 a prime factor of 25? No!
Is 5 a prime factor of 25? Yes! Print '5' and set number to 5.
Is 2 a prime factor of 5? No!
Is 3 a prime factor of 5? No!
Is 5 a prime factor of 5? Yes! Print '5' and set number to 1.
Finished.

So the correct prime fators of 100 ARE 2,2,5,5. What did you expect?

You may like to change this code:

if((num % prime)==0)
{
printf("%d ",prime);  // print out factor
while ( (num % prime) == 0 ) num = num / prime;
}

Paul
0

Commented:
Hi there,

Turn this:
// test if num is prime
if((num % prime)==0)
{
printf("%d ",prime);  // print out factor
num = num / prime;
}

Into this  (use a while loop to divide be the prime as OFTEN as possible)

// test if num is prime
while((num % prime)==0)
{
printf("%d ",prime);  // print out factor
num = num / prime;
}
0

Commented:
Sorry, this still printed out the prime several times ;-)

if((num % prime)==0)
{
printf("%d ",prime);  // print out factor only once
while((num % prime)==0)
num = num / prime;     // but divide as often as possible
}

0

Commented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.