使用递归从用户处打印给定范围内两个最大素数的程序.
在这段代码中,我试图得到2个最大的素数并打印出来.你可以看到num1和num2是未使用的,它们应该是给定范围内两个最大素数的变量.
#include <stdio.h>
void usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);
int main() {
int x, y, i, j, num1, num2;
usersInput(&x, &y);
if (x > y) {
swapping(&x, &y);
}
printf("The value of x is %d, and the value of y is %d.\n", x, y);
printf("\nThe two largest prime numbers from %d to %d are : ", x, y);
for (i = x; i <= y; i++)
if (primeCheck(i, y) == 0)
printf("%d ", i);
return 0;
}
void usersInput(int *x, int *y) {
printf("Enter the value of x: ");
scanf("%d", x);
printf("Enter the value of y: ");
scanf("%d", y);
}
void swapping(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int primeCheck(int i, int j) {
if (j == i) {
return 0;
} else if (j % i == 0) {
return 1;
} else {
return primeCheck(i + 1, j);
}
}
正如你所看到的,我还没有任何函数可以得到两个最大的素数.我真的不知道怎么做,所以请帮帮我
以下是实际输出:
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 6 7 8 9 10
以下是预期输出
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7