The code must be written in C. Write a program that computes the number of eleme
ID: 3810998 • Letter: T
Question
The code must be written in C.
Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2, 2, 4}). Sample execution of the program for this array is given below Enter 7 numbers 1 2 2 3 4 3 1 Enter k 2 3 elements are divisible by 2 Write and implement the following functions in your program: int divisible (int a, int b) int count (int data [], int n, int k) The function divisible returns 1 if a is divisible by b and 0 otherwise. The function count returns the number of elements divisible by k in array data. Use the function divisible in function count.Explanation / Answer
#include <stdio.h>
int main()
{ int n=7;
int data[n];
int k;
printf("Enter 7 numbers ");
int i;
for( i=0;i<7;i++)
scanf("%d",&data[i]);
printf("Enter k ");
scanf("%d",&k);
int count_div=count(data,n,k);
printf("%d elements are divisible by %d ",count_div,k);
return 0;
}
/* method to count count of number divisible by k */
int count(int data[],int n,int k){
int count_div=0;
int i;
for(i=0;i<7;i++)
{
if(divisible(data[i],k))
count_div++;
}
return count_div;
}
/* method for checking divisiblity */
int divisible(int a,int b){
if(a%b==0) /* if remainder is 0 */
return 1;
else
return 0;
}
/****OUTPUT*****
Enter 7 numbers
1 2 2 3 4 3 1
Enter k
2
3 elements are divisible by 2
*******OUTPUT*******/
/* Please do ask in case of any doubt,thanks */