Related Articles

Related Articles

Program to print all the numbers divisible by 5 or 7 for a given number
  • Last Updated : 02 Jun, 2020

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 implementaion :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = " ")

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right



Output:

5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :