Divisible 3 or Divisible by 5 ? 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

  Divisible 3 or Divisible by 5 ?

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 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 a function that checks if a number is divisible by 3:

int is_divisible_by_3(int n) { return n % 3 == 0; }

This function returns 1 if n is divisible by 3, and 0 otherwise.

Here is a function that checks if a number is divisible by 5:

int is_divisible_by_5(int n) { return n % 5 == 0; }

This function returns 1 if n is divisible by 5, and 0 otherwise.

To count the number of elements in an array that are divisible by 3 and/or 5, you can use the following function:

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 function starts a loop from 0 to n, and for each element in the array, it calls the is_divisible_by_3 and is_divisible_by_5 functions to check if the element is divisible by 3 and/or 5. If the element is divisible by either 3 or 5, it increments the count.

Finally, the function returns -1 if the count is 0, and the count otherwise.

For example, if you call count_divisible([10, 1, 13, 3, 9], 5), the function will return 3, as there are three elements in the array that are divisible by 3 or 5.

If you call count_divisible([13, 9, 9, 25, 6, 17, 30], 7), the function will return 5, as there are five elements in the array that are divisible by 3 or 5.

If you call count_divisible([2, 11], 2), the function will return -1, as there are no elements in the array that are divisible by 3 or 5.


Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();