Q. Write a program to print all the numbers divisible by 5 or 7 for a given number?

In a given integer N and our task is to print all the numbers less than N, which are divisible by 5 or 7.

For Example :-
Input : 20
Output : 5 7 10 14 15 20

Logic : We have an integer N = 20, now the task is to print all the numbers divisible by 5 and 7 and the limit is 20. For this divide each number from 1 to N by both 5 and 7 and check their remainder. If the remainder is 0 in any of the cases just print that number.

Algorithm

START
Step 1 : Input integer N
Step 2 : Loop Start
            Iterate j from 1 to Num+1
				if j % 5 == 0 || j % 7 == 0
                    print j
		 Loop End
STOP


Program to print all the numbers divisible by 5 or 7

  • C
  • C++
  • Java
  • Python
  • C#
  • #include <stdio.h>
    
    // Result generator with Num
    int NumDiv(int num)
    {
     
        // Iterate from 1 to Num+1
        for(int j = 1; j < num + 1; j++)
        {
     
            // Short-circuit operator is used
            if (j % 5 == 0 || j % 7 == 0)
                printf("%d ",j);
        }
        return num;
    }
     
    int main()
    {
        // Input goes here
        int Num = 50;
         
        // Iterating over generator function
        NumDiv(Num);
         
        return 0;
    }
    
    #include <bits/stdc++.h>
    using namespace std;
     
    // Result generator with Num
    int NumDiv(int num)
    {
     
        // Iterate from 1 to Num+1
        for(int j = 1; j < num + 1; j++)
        {
     
            // Short-circuit operator is used
            if (j % 5 == 0 || j % 7 == 0)
                cout << j << " ";
        }
        return num;
    }
     
    int main()
    {
        // Input goes here
        int Num = 50;
         
        // Iterating over generator function
        NumDiv(Num);
         
        return 0;
    }
    
    class LFC{
         
    // Result generator with Num
    static int NumDiv(int num)
    {
     
        // Iterate from 1 to Num+1
        for(int j = 1; j < num + 1; j++)
        {
     
           // Short-circuit operator is used
           if (j % 5 == 0 || j % 7 == 0)
               System.out.print(j + " ");
        }
        return num;
    }
     
    public static void main(String args[])
    {
        // Input goes here
        int Num = 50;
         
        // Iterating over generator function
        NumDiv(Num);
    }
    }
    
    
    # Result generator with Num
    def NumDiv(num):
         
        # iterate from 1 to Num+1
        for j in range(1, num+1):
     
            # Short-circuit operator is used
            if j % 5 == 0 or j % 7 == 0:
                yield j
     
    if __name__ == "__main__":
           
        # input goes here
        Num = 50
     
        # Iterating over generator function
        for j in NumDiv(Num):
            print(j, end = " ")
         
    
    using System;
    class LFC{
         
    // Result generator with Num
    static int NumDiv(int num)
    {
     
        // Iterate from 1 to Num+1
        for(int j = 1; j < num + 1; j++)
        {
     
           // Short-circuit operator is used
           if (j % 5 == 0 || j % 7 == 0)
               Console.Write(j + " ");
        }
        return num;
    }
     
    // Driver code
    public static void Main()
    {
     
        // Input goes here
        int Num = 50;
         
        // Iterating over generator
        // function
        NumDiv(Num);
    }
    }
    
    #include <stdio.h>
    
    // Result generator with Num
    int NumDiv(int num)
    {
     
        // Iterate from 1 to Num+1
        for(int j = 1; j < num + 1; j++)
        {
     
            // Short-circuit operator is used
            if (j % 5 == 0 || j % 7 == 0)
                printf("%d ",j);
        }
        return num;
    }
     
    int main()
    {
        // Input goes here
        int Num = 50;
         
        // Iterating over generator function
        NumDiv(Num);
         
        return 0;
    }
    
    

    Output

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


    Recommended Programs

       Program to reverse a string.
       Program to print fibonacci series.