如果它不是素数,我希望这段代码能够打印一个数字的因子,如果它是素数,我希望它能够识别数字。
#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 );
}
}
如果没有 if 语句下方的 printf,它可以正常工作。当我添加这个时,程序只打印“素数 <= n”,没有别的。我不明白为什么 printf 会搞砸循环?