# Find if a number is divisible by every number in a list in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a list of n numbers and a number. Our task is to find if a number is divisible by every number in a list.

We need to check if the given number divides all elements of the list or not.

## Let’s take an example to understand the problem,

Input: list[] = [4, 10 ,6, 5, 9] num = 5

Output: No

Explanation:

Elements 4, 6, 9 are not divisible by 5.

Solution Approach:

To solve the problem, we need to check if any element of the list is divisible by num. If every number of lists is divisible by num, return true else return false.

Algorithm:

Step 1: Loop for i -> 0 to n, n is the length of the list.

Step 1.1: if list[i] % num != 0, return -1.
Step 1.2: else, list[i] % num == 0, continue.

Step 2: return 1.

## Example

Live Demo

#include <iostream>
using namespace std;

bool isListDivNum(int list[], int num, int size)
{
for (int i = 0; i < size; i++) {
if (list[i] % num != 0)
return false;
}
return true;
}

int main() {

int list[] = {762, 9, 123, 99};
int num = 3;
int size = (sizeof(list) / sizeof(list));
if (isListDivNum(list, num , size))
cout<<"All elements of the list are divisible by number";
else
cout<<"All elements of the list are not divisible by number";

return 0;
}

## Output −

All elements of the list are divisible by number