Open In App

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

Last Updated : 18 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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
// 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
# 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#
// 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
JavaScript
<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)


Practice Tags :

Similar Reads