Write a Modular C Programming Code for Printing all Prime numbers of a 1D Array?
CODE:
#include<stdio.h> void readArray(int[],int); void prime(int A[],int n); int main() { int n,occ,key; int arr[100]; printf("Enter the size of Array: "); scanf("%d",&n); printf("Enter the elements of array: \n"); readArray(arr,n); prime(arr,n); } void readArray(int arr[],int n) { int i; for(i=0;i<n;i++) { scanf("%d",&arr[i]); } } void displayArray(int arr[],int n) { int i; for(i=0;i<n;i++) { printf("%d ",arr[i]); } } void prime(int arr[],int n) { int i,j,p; printf("All prime list is:"); for (i = 0; i < n; i++) { j = 2; p = 1; while (j < arr[i]) { if (arr[i] % j == 0) { p = 0; break; } j++; } if (p == 1) { printf("%d ", arr[i]); } } return 0; }
OUTPUT
Enter the size of Array: 9 Enter the elements of array: 6 5 3 2 10 9 7 5 12 All prime list is: 5 3 2 7 5 Process returned 0 (0x0) execution time : 14.408 s Press any key to continue..
Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:
Top 100+ C Programming codes – KLE Technological University