Write a c program to find out how many prime number are present in a given 20 elements of an array in c
C program to find out how many prime numbers are present in a given 20
A prime number is a natural number greater than 1
that has no positive divisors other than 1 and itself (e.g., 2, 3, 5, 7, 11,
and so on).
C Programming
#include <stdio.h>
// Function to check if a number is prime
int is_prime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
// Function to count the number of prime numbers in an array
int count_primes(int arr[], int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (is_prime(arr[i])) {
count++;
}
}
return count;
}
int main() {
int arr[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73};
int num_primes = count_primes(arr, 20);
printf("The number of prime numbers in the array is: %d\n", num_primes);
return 0;
}
Output:
The number of prime numbers in the array is: 21
Top Resources
Prime Numbers: C Program Example and Explanation
Design a function to check the given number is prime or not if prime return 1 else 0
Write a C Program to Check if a Number is Prime or Not Using For Loop
Writing a C Program to Print Prime Numbers Between 50 and 100
write a program to print prime numbers in 10 array elements.
Write a C program to input 10 numbers through the keyword and find the number of prime counts it stores the into a separate array and display it.
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!