Program to print all the numbers divisible by 5 or 7 for a given number - GeeksforGeeks

18 Nov, 2021

Given the integer N, the task is to print all the numbers less than N, which are divisible by 5 or 7.
Examples : 
Input : 20
Output : 5 7 10 14 15 20 

Input: 50
Output: 5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50
Approach: For example, let’s take N = 20 as a limit, then the program should print all numbers less than 20 which are divisible by both 5 or 7. For this divide each number from 0 to N by both 5 and 7 and check their remainder. If the remainder is 0 in both cases then simply print that number.
Below is the implementation : 
C++
Java
Python3
C#
Javascript
 

 
 










// C++ program to print all the numbers 
// divisible by 5 or 7 for a given number 
# include<bits/stdc++.h>
using namespace std;
 
// Result generator with N 
int NumGen(int n)
{
 
    // Iterate from 0 to N
    for(int j = 1; j < n + 1; j++)
    {
 
        // Short-circuit operator is used 
        if (j % 5 == 0 || j % 7 == 0)
            cout << j << " ";
    }
    return n;
}
 
// Driver code 
int main()
{ 
    // Input goes here 
    int N = 50;
     
    // Iterating over generator function
    NumGen(N);
     
    return 0;
}
 
// This code is contributed by Code_Mech






 

 
 










// Java program to print all the numbers 
// divisible by 5 or 7 for a given number 
import java.util.*;
 
class GFG{
     
// Result generator with N 
static int NumGen(int n)
{
 
    // Iterate from 0 to N
    for(int j = 1; j < n + 1; j++)
    {
 
       // Short-circuit operator is used 
       if (j % 5 == 0 || j % 7 == 0)
           System.out.print(j + " ");
    }
    return n;
}
 
// Driver code 
public static void main(String args[])
{ 
    // Input goes here 
    int N = 50;
     
    // Iterating over generator function
    NumGen(N);
}
}
 
// This code is contributed by AbhiThakur






 

 
 










# Python3 program to print all the numbers 
# divisible by 5 or 7 for a given number 
 
# Result generator with N 
def NumGen(n):
     
    # iterate from 0 to N
    for j in range(1, n+1):
 
        # Short-circuit operator is used 
        if j % 5 == 0 or j % 7 == 0:
            yield j
 
# Driver code 
if __name__ == "__main__": 
       
    # input goes here 
    N = 50
 
    # Iterating over generator function
    for j in NumGen(N):
        print(j, end = " ")






 

 
 










// C# program to print all the numbers 
// divisible by 5 or 7 for a given number 
using System;
 
class GFG{
     
// Result generator with N 
static int NumGen(int n)
{
 
    // Iterate from 0 to N
    for(int j = 1; j < n + 1; j++)
    {
 
       // Short-circuit operator is used 
       if (j % 5 == 0 || j % 7 == 0)
           Console.Write(j + " ");
    }
    return n;
}
 
// Driver code 
public static void Main()
{ 
 
    // Input goes here 
    int N = 50;
     
    // Iterating over generator
    // function
    NumGen(N);
}
}
 
// This code is contributed by Code_Mech






 

 
 










<script>
 
// JavaScript program to print all the numbers
// divisible by 5 or 7 for a given number
// Result generator with N
function NumGen(n)
{
 
    // Iterate from 0 to N
    for(let j = 1; j < n + 1; j++)
    {
 
        // Short-circuit operator is used
        if (j % 5 == 0 || j % 7 == 0)
            document.write(j + " ");
    }
    return n;
}
 
// Driver code
 
    // Input goes here
    let N = 50;
     
    // Iterating over generator function
    NumGen(N);
     
</script>






Output: 
5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 
 Time Complexity: O(N)
Auxiliary Space: O(1)
abhaysingh290895
Code_Mech
arorakashish0911
vaibhavrabadiya3
rohitkumarsinghcna

python-utility


Python

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python