Problem Statement
You are given an array of size n . And the next line will contain n positive integers. Now you need to tell how many numbers are divisible by 3 and divisible by 5. For this you need to write two functions , First one is for finding whether a number is divisible by 3 or not and the second one is for finding whether a number is divisible by 5 or not. See the sample input/output and explanation for more clarification.
Print the total count of how many numbers are divisible by 3 and divisible by 5 if there are no such numbers then return -1 from the functions,and that time print -1 only once(see the sample input and output)
Note - if a number is divisible by both 3 and 5,then consider it only once.
Constraints-
1<=n<=100
Values of array will be given between 1-1000
Sample Input 1- Sample Output 1-
5 3
10 1 13 3 9 Sample Output 2-
Sample Input 2- 5
7
13 9 9 25 6 17 30
Sample Input 3- Sample Output 3-
2
2 11 -1
Explanation -
In sample input 1 -
10 is divisible by 5
3 and 9 is divisible by 3
Total count = 3
In sample input 2 -
9 is divisible by 3 , there are two 9 exist
6 is divisible by 3
25 is divisible by 5
30 is divisible by both 3 and 5 , so you must consider it only once.
Total count = 5
answer:
Here is the complete program in C that reads an array of integers and counts how many elements are divisible by 3 and/or 5:
#include <stdio.h>
// Function prototypes
int is_divisible_by_3(int n);
int is_divisible_by_5(int n);
int count_divisible(int *arr, int n);
int main(void) {
// Read the size of the array
int n;
scanf("%d", &n);
// Read the elements of the array
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Count the number of elements divisible by 3 and/or 5
int count = count_divisible(arr, n);
printf("%d\n", count);
return 0;
}
int is_divisible_by_3(int n) {
return n % 3 == 0;
}
int is_divisible_by_5(int n) {
return n % 5 == 0;
}
int count_divisible(int *arr, int n) {
int count = 0;
for (int i = 0; i < n; i++) {
if (is_divisible_by_3(arr[i]) || is_divisible_by_5(arr[i])) {
count++;
}
}
return count == 0 ? -1 : count;
}
This program reads the size of the array and then reads the elements of the array. It then calls the count_divisible
function to count the number of elements that are divisible by 3 and/or 5, and prints the result.
The is_divisible_by_3
and is_divisible_by_5
functions check if a number is divisible by 3 and 5, respectively, and return 1 if the number is divisible and 0 otherwise.
The count_divisible
function starts a loop from 0 to n
, and for each element in the array, it calls the is_divisible_by_3
and `is_
0 Comments