Find if a number is divisible by every number in a list - GeeksforGeeks

27 Jan, 2022
Given a list and a number task is to find the number which is divided by every element of the list. Examples :  
Input : List   = [1, 2, 3, 4, 5]
        Number = 3
Output : No

Input : List   = [4, 8, 12, 16, 20]
        Number = 4
Output : Yes
Algorithm:  
1. Run a loop till length of the list
2. Divide every element with the given number
3. If number is not divided by any element, 
   return 0.
4. Return 1.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program which check is a number// divided with every element in list or not#include <bits/stdc++.h>using namespace std; // Function which check is a number// divided with every element in list or notbool findNoIsDivisibleOrNot(int a[], int n, int l){    for (int i = 0; i < l; i++) {        if (a[i] % n != 0)            return false;    }    return true;} // driver programint main(){    int a[] = {14, 12, 4, 18};    int n = 2;    int l = (sizeof(a) / sizeof(a[0]));    if (findNoIsDivisibleOrNot(a, n, l))        cout << "Yes";    else        cout << "No";     return 0;} // This code is contributed by Sam007
// Java program which check is a number// divided with every element in list// or not class GFG {     // Function which check is a number    // divided with every element in list or not    static boolean findNoIsDivisibleOrNot(int a[], int n)    {        for (int i = 0; i < a.length; i++) {            if (a[i] % n != 0)                return false;        }        return true;    }     // driver program    public static void main(String[] args)    {        int a[] = {14, 12, 4, 18};        int n = 2;        if (findNoIsDivisibleOrNot(a, n))            System.out.println("Yes");        else            System.out.println("No");    }} // This code is contributed by Pramod Kumar
# Python program which check is a number# divided with every element in list# or notdef findNoIsDivisibleOrNot(n, l =[]):     # Checking if a number is divided    # by every element or not    for i in range(0, len(l)):        if l[i]% n != 0:            return 0    return 1 # Driver codel = [14, 12, 4, 18]n = 2if findNoIsDivisibleOrNot(n, l) == 1:    print ("Yes")else:    print ("No")
// C# program which check is a number// divided with every element in list or nousing System; class GFG {     // Function which check is a number    // divided with every element in list or not    static bool findNoIsDivisibleOrNot(int[] a, int n)    {        for (int i = 0; i < a.Length; i++) {            if (a[i] % n != 0)                return false;        }        return true;    }     // driver program    public static void Main()    {        int[] a = {14, 12, 4, 18};        int n = 2;        if (findNoIsDivisibleOrNot(a, n))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by Sam007
<?php// PHP program which// check is a number// divided with every// element in list or not // Function which check// is a number divided// with every element// in list or notfunction findNoIsDivisibleOrNot($a, $n, $l){    for ($i = 0; $i < $l; $i++)    {        if ($a[$i] % $n != 0)            return false;    }    return true;}     // Driver Code    $a = array(14, 12, 4, 18);    $n = 2;    $l = sizeof($a);    if (findNoIsDivisibleOrNot($a, $n, $l))        echo "Yes";    else        echo "No"; // This code is contributed by ajit?>
<script> // JavaScript program which check is a number// divided with every element in list or not // Function which check is a number// divided with every element in list or notfunction findNoIsDivisibleOrNot(a, n, l){    for (let i = 0; i < l; i++) {        if (a[i] % n != 0)            return false;    }    return true;} // driver program    let a = [14, 12, 4, 18];    let n = 2;    let l = a.length;    if (findNoIsDivisibleOrNot(a, n, l))        document.write("Yes");    else        document.write("No"); </script>
Output: 
Yes
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
jit_t
souravmahato348
amartyaghoshgfg
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Sieve of Eratosthenes
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Find minimum number of coins that make a given value