Program to print all the numbers divisible by 5 or 7 for a given number
Last Updated :
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++
# include<bits/stdc++.h>
using namespace std;
int NumGen( int n)
{
for ( int j = 1; j < n + 1; j++)
{
if (j % 5 == 0 || j % 7 == 0)
cout << j << " " ;
}
return n;
}
int main()
{
int N = 50;
NumGen(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int NumGen( int n)
{
for ( int j = 1 ; j < n + 1 ; j++)
{
if (j % 5 == 0 || j % 7 == 0 )
System.out.print(j + " " );
}
return n;
}
public static void main(String args[])
{
int N = 50 ;
NumGen(N);
}
}
|
Python3
def NumGen(n):
for j in range ( 1 , n + 1 ):
if j % 5 = = 0 or j % 7 = = 0 :
yield j
if __name__ = = "__main__" :
N = 50
for j in NumGen(N):
print (j, end = " " )
|
C#
using System;
class GFG{
static int NumGen( int n)
{
for ( int j = 1; j < n + 1; j++)
{
if (j % 5 == 0 || j % 7 == 0)
Console.Write(j + " " );
}
return n;
}
public static void Main()
{
int N = 50;
NumGen(N);
}
}
|
Javascript
<script>
function NumGen(n)
{
for (let j = 1; j < n + 1; j++)
{
if (j % 5 == 0 || j % 7 == 0)
document.write(j + " " );
}
return n;
}
let N = 50;
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)
Share your thoughts in the comments
Please Login to comment...