为什么我的打印质数的代码提前终止?
Why does my code to print prime numbers terminate early?
我试着用 C 编写程序来打印从 2 到给定数的所有素数。
#include <stdio.h>
#include <math.h>
int main() {
int up, t = 1, i, j;
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
scanf("%d", &up);
for(i == 2; i <= up; i++) {
for(j == 1; j <= sqrt(i); j++) {
if(i % j != 0) {
t = 0;
}
if(t == 1) {
printf("%d", i);
}
}
}
return(0);
}
Returns 结果如下:
$ gcc prime.c -lm
$ ./a.out
This program will show you a list of all prime numbers from 2 to the number you enter below:
10
我的意思是,我输入10后,程序就终止了。
想法?
谢谢。
这是您程序的一个可能的固定版本。
请注意,您应该从 2 而不是 1 开始除法。
#include <stdio.h>
#include <math.h>
int main() {
int up, i, j;
int prime;
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
scanf("%d", &up);
for(i = 2; i <= up; i++) {
prime = 1;
for(j = 2; j <= sqrt(i); j++) {
if(i % j == 0) {
prime = 0;
}
}
if (prime == 1)
printf("%d ", i);
}
printf("\n");
return(0);
}
示例:
$ ./prime
This program will show you a list of all prime numbers from 2 to the number you enter below:
10
2 3 5 7
使用此代码:-
#include <stdio.h>
int isprime(int num){
int loop; /* loop variable */
for(loop=2;loop<num;++loop){
if(num % loop==0){
return 0;
}
}
return 1;
}
int main(){
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
int up;
scanf("%d", &up);
if (isprime(up)){
printf("It is a prime number\n");
} else{
printf("It is not a prime number\n");
}
return(0);
}
看起来你是一名学生。您正在使用 == 这是一个比较运算符。您必须在循环中使用 = 。了解更多。
我试着用 C 编写程序来打印从 2 到给定数的所有素数。
#include <stdio.h>
#include <math.h>
int main() {
int up, t = 1, i, j;
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
scanf("%d", &up);
for(i == 2; i <= up; i++) {
for(j == 1; j <= sqrt(i); j++) {
if(i % j != 0) {
t = 0;
}
if(t == 1) {
printf("%d", i);
}
}
}
return(0);
}
Returns 结果如下:
$ gcc prime.c -lm
$ ./a.out
This program will show you a list of all prime numbers from 2 to the number you enter below:
10
我的意思是,我输入10后,程序就终止了。 想法? 谢谢。
这是您程序的一个可能的固定版本。
请注意,您应该从 2 而不是 1 开始除法。
#include <stdio.h>
#include <math.h>
int main() {
int up, i, j;
int prime;
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
scanf("%d", &up);
for(i = 2; i <= up; i++) {
prime = 1;
for(j = 2; j <= sqrt(i); j++) {
if(i % j == 0) {
prime = 0;
}
}
if (prime == 1)
printf("%d ", i);
}
printf("\n");
return(0);
}
示例:
$ ./prime
This program will show you a list of all prime numbers from 2 to the number you enter below:
10
2 3 5 7
使用此代码:-
#include <stdio.h>
int isprime(int num){
int loop; /* loop variable */
for(loop=2;loop<num;++loop){
if(num % loop==0){
return 0;
}
}
return 1;
}
int main(){
puts("This program will show you a list of all prime numbers from 2 to the number you enter below: ");
int up;
scanf("%d", &up);
if (isprime(up)){
printf("It is a prime number\n");
} else{
printf("It is not a prime number\n");
}
return(0);
}
看起来你是一名学生。您正在使用 == 这是一个比较运算符。您必须在循环中使用 = 。了解更多。