Count total number of elements divisible by a specific number in an array
C program to count total number of elements divisible by a specific number in an array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void main()
{
int arr[MAX];
int i, n, num, count;
count=0;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
printf("Enter the number by which divisibility is to be checked\t:");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i]%num==0)
count++;
}
printf("\nThere are total of %d numbers which are divisible by %d",count,num);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :25
Element 3 :56
Element 4 :45
Element 5 :32
Element 6 :58
Element 7 :47
Element 8 :89
Enter the number by which divisibility is to be checked :2
There are total of 4 numbers which are divisible by 2