Write a c program to find out how many prime number are present in a given 20 elements of an array in c

07 Sep 2022 Balmiki Mandal 0 C Programming

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:

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.