Given an array A of N integers and two integers X and Y, find the number of integers in the array that are both less than or equal to X and divisible by Y.
#include <stdio.h>
int count(int* A, int N, int X, int Y) {
int num, i;
num = 0;
for (i=0; i<N; i++) {
if (A[i] <= X && A[i] % Y == 0) {
num++;
}
}
return num;
}
int main() {
int A[] = { 3, 5, 6, 7, 10, 20, 15, 21, 48, 6};
int n = sizeof(A) / sizeof(A[0]);
int x = 20, y = 3;
int num;
num = count(A, n, x, y);
printf("There are %d numbers less or equal %d and divided by %d in the array.\n",
num, x, y);
return 0;
}
Comments
Leave a comment