- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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.

## Program to illustrate the working of our solution,

## Example

#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[0])); 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

- Related Questions & Answers
- Check if a large number is divisible by 20 in C++
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Check if a number is divisible by all prime divisors of another number in C++
- Write a C# program to check if a number is divisible by 2
- Check if a large number is divisible by 11 or not in java
- Check if a large number is divisible by 3 or not in java
- Check if a large number is divisible by 13 or not in C++
- Check if a large number is divisible by 11 or not in C++
- Check if a large number is divisible by 25 or not in C++
- Check if a large number is divisible by 3 or not in C++
- Check if a large number is divisible by 5 or not in C++
- Check if a large number is divisible by 75 or not in C++
- Check if a large number is divisible by 8 or not in C++
- Check if a large number is divisible by 9 or not in C++

Advertisements