Code in C not C++ I use Dev-C++ if that helps Write a program that computes the
ID: 3695084 • Letter: C
Question
Code in C not C++
I use Dev-C++ if that helps
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 For this assignment, YOU MUST: Store the User input into an array of size-7 Create functions for int divisible (int a, int b) - The function divisible() returns 1 if a is divisible by b and 0 otherwise. int count (int data[],int n, int k) - The function count() returns the number of elements divisible by k in array data. Be sure to add comments to your code and a header with your name, id, and assignment number.Explanation / Answer
#include<stdio.h>
// function prototype
int divisible(int a, int b);
int count(int data[], int n, int k);
int main(){
// declaring an array of size 7
int data[7], i, k;
// asking for user input
printf("Enter 7 numbers ");
for(i=0; i<7; i++)
scanf("%d", &data[i]);
printf("Enter k ");
scanf("%d", &k);
printf("%d elements aree divisible by %d ", count(data, 7, k), k);
return 0;
}
// function defination
int divisible(int a, int b){
if(a%b == 0)
return 1;
else
return 0;
}
int count(int data[], int n, int k){
int cnt = 0, i;
for(i=0; i<n; i++){
if(divisible(data[i], k) == 1)
cnt = cnt + 1;
}
return cnt;
}
/*
Sample run:
Enter 7 numbers
1 2 2 3 4 3 1
Enter k
2
3 elements aree divisible by 2
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.