supersaidso
supersaidso

Reputation: 69

For-loop works until I add printf (in C)

I want this code to be able to print a number's factors if it is not prime, and identify the number as such if it is prime.

#include <stdio.h>

main() {
int possible_prime, n, possible_divisor;

printf( "\tThis program lists all primes <= n\n\n" );
printf( "Input n: " );
scanf( "%d", &n );
printf( "\n\n\tPrimes <= %d: \n\n", n );

    for ( possible_prime = 1; possible_prime <= n; possible_prime++ ) {
          /* try to find a divisor of possible_prime */

         for ( possible_divisor = 1; possible_divisor < possible_prime; possible_divisor++ ) {
             if ( possible_prime % possible_divisor == 0 )
                printf("\n\t%d", possible_prime);
               }

          /* found a divisor so possible_prime is not prime */
          break;

    if ( possible_divisor == possible_prime )
         /* exhausted possible divisors, so possible_prime is prime */
         printf( "%d\n", possible_prime );

   }
}

It works fine without the printf beneath the if statement. When I added this, the program only prints "Prime numbers <= n " and nothing else. I don't understand why a printf would mess up the loop?

Upvotes: 1

Views: 242

Answers (2)

haccks
haccks

Reputation: 106122

Your program will neither print prime number nor factors of non-prime numbers. Here is the corrected version of your code:

#include <stdio.h>

int main() 
{
    int possible_prime, n, possible_divisor;

    printf( "\tThis program lists all primes <= n\n\n" );
    printf( "\tInput n: " );
    scanf( "%d", &n );
    printf( "\n\n\tPrimes <= %d: \n\n", n );
    printf("Prime num\tNon-prime\n");
    printf("2\n");
    for ( possible_prime = 3; possible_prime <= n; possible_prime++ ) 
    {
            /* try to find a divisor of possible_prime */

            for ( possible_divisor = 2; possible_divisor < possible_prime; possible_divisor++) 
            {
                 if ( possible_prime % possible_divisor == 0 )
                 {
                     printf("\t\t%d:", possible_prime);
                     for (i = 1; i <= possible_prime; i++)
                     {
                         if(possible_prime % i == 0)
                            printf("%d ", i);
                     }
                 printf("\n");  
                 break;
                 }
            }

      /* found a divisor so possible_prime is not prime */


          if ( possible_divisor == possible_prime )
          /* exhausted possible divisors, so possible_prime is prime */
               printf( "%d\n", possible_prime );

    }
}

Upvotes: 1

danben
danben

Reputation: 83330

Your break statement is in the wrong place. Put it inside the inner for loop; right now it is breaking out of the outer for loop after checking whether 1 is prime.

As far as why this would happen only after you added that inner print statement, I'm guessing you ended up moving some curly braces around in the process.

After fixing this you'll probably discover that your program tells you that nothing is prime. You might want to recheck the program's conditions for primality.

Upvotes: 7

Related Questions