1

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?

1
  • Do not use main, use int main() instead. Commented Aug 4, 2013 at 14:18

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 );

    }
}

9 Comments

I agree my code doesn't print divisors of non-primes nor does it identify primes, but the code you gave me prints the following when I input, for example, 9: Primes <= 9: 2 3 4 5 6 7 8 9
Yes. It will now print both--prime numbers as well as factors for non-prime. See the output carefully: on left are primes and on right are factors.
Oh, gotcha. So I just have to make it print those in an organized manner. Thank you.
No wait, it's printing things that aren't factors of the given number? 4, 6 and 8 for example?
I had originally written the code to list factors of a given number, and then I just wanted to add in a bit that printed "This number is prime" if those factors were only 1 and itself. Identifying primes seems to be rather more complicated though.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.