Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.
Examples :
Input : 50
Output : 0 15 30 45
Input : 100
Output : 0 15 30 45 60 75 90
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 3 and 5. For this divide each number from 0 to N by both 3 and 5 and check their remainder. If remainder is 0 in both cases then simply print that number.
Below is the implementation :
C++
#include <iostream>
using namespace std;
void result( int N)
{
for ( int num = 0; num < N; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << num << " " ;
}
}
int main()
{
int N = 100;
result(N);
return 0;
}
|
Java
class GFG{
static void result( int N)
{
for ( int num = 0 ; num < N; num++)
{
if (num % 3 == 0 && num % 5 == 0 )
System.out.print(num + " " );
}
}
public static void main(String []args)
{
int N = 100 ;
result(N);
}
}
|
Python3
def result(N):
for num in range (N):
if num % 3 = = 0 and num % 5 = = 0 :
print ( str (num) + " " , end = "")
else :
pass
if __name__ = = "__main__" :
N = 100
result(N)
|
C#
using System;
public class GFG{
static void result( int N)
{
for ( int num = 0; num < N; num++)
{
if (num % 3 == 0 && num % 5 == 0)
Console.Write(num + " " );
}
}
static public void Main (){
int N = 100;
result(N);
}
}
|
PHP
<?php
function result( $N )
{
for ( $num = 0; $num < $N ; $num ++)
{
if ( $num % 3 == 0 && $num % 5 == 0)
echo $num , " " ;
}
}
$N = 100;
result( $N );
?>
|
Javascript
<script>
function result(N)
{
for (let num = 0; num < N; num++)
{
if (num % 3 == 0 && num % 5 == 0)
document.write( num+ " " );
}
}
let N = 100;
result(N);
</script>
|
Output0 15 30 45 60 75 90
Time Complexity: O(N)
Auxiliary Space: O(1)
Method: This can also be done by checking if the number is divisible by 15, since the LCM of 3 and 5 is 15 and any number divisible by 15 is divisible by 3 and 5 and vice versa also.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 50;
for ( int i = 0; i < n; i++)
{
if (i % 15 == 0){
cout << i << " " ;
}
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int n = 50 ;
for ( int i = 0 ; i < 50 ; i++) {
if (i % 15 == 0 ) {
System.out.println(i+ " " );
}
}
}
}
|
Python3
n = 50
for i in range ( 0 ,n):
if i % 15 = = 0 :
print (i,end = " " )
|
C#
using System;
public class GFG {
static public void Main()
{
int n = 50;
for ( int i = 0; i < 50; i++) {
if (i % 15 == 0) {
Console.Write(i+ " " );
}
}
}
}
|
Javascript
<script>
let n = 50;
for (let i = 0; i < 50; i++)
{
if (i % 15 == 0){
document.write(i+ " " );
}
}
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3 : we noticed that the LCM of 3 & 5 is 15, so we do not need to iterate the whole loop from 0 to n but we need to iterate from 0 and every time increase i by 15 so this way we can decrease time complexity as we do not iterate from 0 to n by step of +1 but we iterate from 15 to n by step of +15
C++
#include <iostream>
using namespace std;
int main()
{
int n = 50;
for ( int i = 0; i < n; i += 15) {
cout << i << " " ;
}
return 0;
}
|
Java
class GFG {
public static void main(String[] args) {
int n = 50 ;
for ( int i = 0 ; i < n; i += 15 ) {
System.out.print(i + " " );
}
}
}
|
C#
using System;
public class GFG {
public static void Main( string [] args) {
int n = 50;
for ( int i = 0; i < n; i += 15) {
Console.Write(i + " " );
}
}
}
|
Python3
n = 50
for i in range ( 0 ,n, 15 ):
print (i,end = " " )
|
Javascript
let n = 50;
for (let i = 0; i <= n; i += 15) {
console.log(i);
}
|
Time Complexity: O(n/15) ~= O(n) (it’s far better than above both method as we need to iterate i for only n/15 times)
Auxiliary Space: O(1) (constant extra space required)
Method 4: “for loop” approach in Python to print all numbers less than a given number that is divisible by both 3 and 5.
- Take the input for the value of N from the user using the input() function and convert it to an integer using the int() function.
- Use a for loop to iterate over all the numbers less than N.
- For each number, check if it is divisible by both 3 and 5 using the modulo operator %.
- If the remainder is 0, print the number using the print() function and the end parameter to ensure that the numbers are printed on the same line with a space in between them.
C++
#include <iostream>
using namespace std;
int main() {
int N = 100;
for ( int i = 0; i<N; i++){
if (i%3 == 0 && i%5 == 0){
cout << i << " " ;
}
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
int N = 100 ;
for ( int i = 0 ; i<N; i++){
if (i% 3 == 0 && i% 5 == 0 ){
System.out.print(i + " " );
}
}
}
}
|
Python3
N = 100
for i in range (N):
if i % 3 = = 0 and i % 5 = = 0 :
print (i, end = ' ' )
|
Javascript
let N = 100;
for (let i = 0; i<N; i++){
if (i%3 == 0 && i%5 == 0){
console.log(i+ " " );
}
}
|
C#
using System;
class Gfg {
public static void Main ( string [] args) {
int N = 100;
for ( int i = 0; i<N; i++){
if (i%3 == 0 && i%5 == 0){
Console.Write(i + " " );
}
}
}
}
|
Output0 15 30 45 60 75 90
The time complexity is O(N) where N is the given input number.
The auxiliary space is O(1)