C 程序:寻找以 1 结尾的素数
#include <stdio.h>
// Function to check if a number is prime int is_prime(int n) { if (n <= 1) { // If number is less than or equal to 1, it is not prime return 0; } for (int i = 2; i * i <= n; i++) { // Check for factors from 2 to square root of n if (n % i == 0) { // If a factor is found, number is not prime return 0; } } return 1; // Number is prime if no factors are found }
int main() { int n; scanf("%d", &n); // Read input from user
int count = 0; // Counter to keep track of prime numbers ending with 1
for (int i = 1; i <= n; i++) {
if (i % 10 == 1 && is_prime(i)) { // If number ends with 1 and is prime
count++; // Increment count
printf("%d ", i); // Print the prime number
}
}
if (count == 0) { // If no prime numbers ending with 1 are found
printf("-1"); // Print -1
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pxBm 著作权归作者所有。请勿转载和采集!