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?
main
, useint main()
instead.