The Wayback Machine - https://web.archive.org/web/20240623190535/https://www.geeksforgeeks.org/program-print-numbers-divisible-3-5-given-number/
Open In App

Program to print all the numbers divisible by 3 and 5 for a given number

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ program to print all the numbers
// divisible by 3 and 5 for a given number
#include <iostream>
using namespace std;
 
// Result function with N
void result(int N)
{    
    // iterate from 0 to N
    for (int num = 0; num < N; num++)
    {    
        // Short-circuit operator is used
        if (num % 3 == 0 && num % 5 == 0)
            cout << num << " ";
    }
}
 
// Driver code
int main()
{    
    // input goes here
    int N = 100;
     
    // Calling function
    result(N);
    return 0;
}
 
// This code is contributed by Manish Shaw
// (manishshaw1)


Java




// Java program to print all the numbers
// divisible by 3 and 5 for a given number
 
class GFG{
     
    // Result function with N
    static void result(int N)
    {    
        // iterate from 0 to N
        for (int num = 0; num < N; num++)
        {    
            // Short-circuit operator is used
            if (num % 3 == 0 && num % 5 == 0)
                System.out.print(num + " ");
        }
    }
      
    // Driver code
    public static void main(String []args)
    {
        // input goes here
        int N = 100;
          
        // Calling function
        result(N);
    }
}


Python3




# Python program to print all the numbers
# divisible by 3 and 5 for a given number
 
# Result function with N
def result(N):
     
    # iterate from 0 to N
    for num in range(N):
         
            # Short-circuit operator is used
            if num % 3 == 0 and num % 5 == 0:
                print(str(num) + " ", end = "")
                 
            else:
                pass
 
# Driver code
if __name__ == "__main__":
     
    # input goes here
    N = 100
     
    # Calling function
    result(N)


C#




// C# program to print all the numbers
// divisible by 3 and 5 for a given number
using System;
public class GFG{
     
    // Result function with N
    static void result(int N)
    {    
        // iterate from 0 to N
        for (int num = 0; num < N; num++)
        {    
            // Short-circuit operator is used
            if (num % 3 == 0 && num % 5 == 0)
                Console.Write(num + " ");
        }
    }
     
    // Driver code
    static public void Main (){
        // input goes here
        int N = 100;
        // Calling function
        result(N);
    }
//This code is contributed by ajit.   
}


PHP




<?php
// PHP program to print all the numbers
// divisible by 3 and 5 for a given number
 
// Result function with N
function result($N)
{
    // iterate from 0 to N
    for ($num = 0; $num < $N; $num++)
    {
        // Short-circuit operator is used
        if ($num % 3 == 0 && $num % 5 == 0)
            echo $num, " ";
    }
}
 
// Driver code
     
// input goes here
$N = 100;
 
// Calling function
result($N);
 
// This code is contributed by ajit
?>


Javascript




<script>
 
// Javascript  program to
// print all the numbers
// divisible by 3 and 5
// for a given number
 
// Result function with N
function result(N)
{
    // iterate from 0 to N
    for (let num = 0; num < N; num++)
    {
        // Short-circuit operator is used
        if (num % 3 == 0 && num % 5 == 0)
            document.write( num+ " ");
    }
}
 
// Driver code
     
// input goes here
let N = 100;
 
// Calling function
result(N);
 
// This code is contributed by Bobby
 
</script>


Output

0 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++




// C++ code to print numbers that
// are divisible by 3 and 5
#include <iostream>
using namespace std;
 
int main()
{
  int n = 50;
  for(int i = 0; i < n; i++)
  {
 
    //lcm of 3 and 5 is 15
    if(i % 15 == 0){
      cout << i << " ";
    }
  }
  return 0;
}
 
// This code is contributed by laxmigangarajula03


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
       
       int n = 50;
        for (int i = 0; i < 50; i++) {
 
            // lcm of 3 and 5 is 15
            if (i % 15 == 0) {
                System.out.println(i+" ");
            }
        }
       
    }
}
 
// This code is contributed by laxmigangarajula03


Python3




# python code to print numbers that
# are divisible by 3 and 5
n=50
for i in range(0,n):
  # lcm of 3 and 5 is 15
  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++) {
 
            // lcm of 3 and 5 is 15
            if (i % 15 == 0) {
                Console.Write(i+" ");
            }
        }
    }
}
   
  // This code is contributed by laxmigangarajula03


Javascript




<script>
let n = 50;
  for(let i = 0; i < 50; i++)
  {
 
    //lcm of 3 and 5 is 15
    if(i % 15 == 0){
      document.write(i+" ");
    }
  }
</script>


Output

0 15 30 45 

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




// C++ code to print numbers that are divisible by 3 and 5
#include <iostream>
using namespace std;
 
int main()
{
    int n = 50;
    // lcm of 3 and 5 is 15
    // LCM(3, 5) = 15
 
    // start loop from 0 to n, by increment of +15 every time
    for (int i = 0; i < n; i += 15) {
        cout << i << " ";
    }
    return 0;
}


Java




// Java code to print numbers that are divisible by 3 and 5
class GFG {
  public static void main(String[] args) {
    int n = 50;
 
    // lcm of 3 and 5 is 15
    // LCM(3, 5) = 15
 
    // start loop from 0 to n, by increment of +15 every time
    for (int i = 0; i < n; i += 15) {
      System.out.print(i + " ");
    }
  }
}
 
// This code is contributed by ajaymakavana.


C#




// C# code to print numbers that are divisible by 3 and 5
using System;
 
public class GFG {
    public static void Main(string[] args) {
        int n = 50;
 
        // lcm of 3 and 5 is 15
        // LCM(3, 5) = 15
 
        // start loop from 0 to n, by increment of +15 every time
        for (int i = 0; i < n; i += 15) {
            Console.Write(i + " ");
        }
    }
}
 
// This code is contributed by ajaymakvana


Python3




#Python code to print numbers that are divisible by 3 and 5
n = 50
#lcm of 3 and 5 is 15
#LCM(3, 5) = 15
 
#start loop from 0 to n, by increment of +15 every time
for i in range(0,n,15):
  print(i,end=" ")
   
#This code is contributed by Vinay Pinjala


Javascript




//Javascript code to print numbers that are divisible by 3 and 5
//lcm of 3 and 5 is 15
//LCM(3, 5) = 15
 
//start loop from 0 to n, by increment of +15 every time
let n = 50;
for (let i = 0; i <= n; i += 15) {
    console.log(i);
}
// This code is contributed by Edula Vinay Kumar Reddy


Output

0 15 30 45 

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.

  1. 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.
  2. Use a for loop to iterate over all the numbers less than N.
  3. For each number, check if it is divisible by both 3 and 5 using the modulo operator %.
  4. 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




// Java code to print all numbers divisible by 3 and 5 from 0 to N
 
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 + " ");
        }
    }
  }
}


Output

0 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)



Similar Reads

Python Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 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 n
2 min read
Program to print all the numbers divisible by 5 or 7 for a given number
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
4 min read
Print k numbers where all pairs are divisible by m
Given an integer array and two numbers k and m. Print k numbers from the array, such that difference between any two pairs is divisible by m. If there are no k numbers, then print -1.Examples: Input: arr[] = {1, 8, 4} k = 2 m = 3 Output: 1 4 Explanation: Difference between 1 and 4 is divisible by 3. Input: arr[] = {1, 8, 4} k = 3 m = 3 Output: -1 E
12 min read
Smallest K digit number divisible by all numbers in given array
Given an array arr[]. The task is to create the smallest K digit number divisible by all numbers of arr[]. Examples: Input: arr[] = {2, 3, 5}, N = 3Output: 120Explanation: 120 is divisible by 2, 3 and 5 Input: arr[] = {2, 6, 7, 4, 5}, N = 5Output: 10080 Recursive approach: This problem can be solved by using Lowest common multiple. Follow the steps
7 min read
Python Program to Print all Integers that Aren't Divisible by Either 2 or 3
We can input a set of integer, and check which integers in this range, beginning with 1 are not divisible by 2 or 3, by checking the remainder of the integer with 2 and 3. Example: Input: 10 Output: Numbers not divisible by 2 and 3 1 5 7 Method 1: We check if the number is not divisible by 2 and 3 using the and clause, then outputs the number. C/C+
7 min read
Count numbers which are divisible by all the numbers from 2 to 10
Given an integer N, the task is to find the count of numbers from 1 to N which are divisible by all the numbers from 2 to 10. Examples: Input: N = 3000 Output: 1 2520 is the only number below 3000 which is divisible by all the numbers from 2 to 10. Input: N = 2000 Output: 0 Approach: Let's factorize numbers from 2 to 10. 2 = 2 3 = 3 4 = 22 5 = 5 6
3 min read
Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
Given integers A, B, and N, the task is to find the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Examples: Input: N = 2, A = 2, B = 5Output: 5Explanation:The only 2-digit numbers {50, 52, 54, 56, 58} with sum of odd-positioned digits equal to 5 and even-positioned dig
15+ min read
Python Program for Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After step 1, n must be
6 min read
Count of numbers upto M divisible by given Prime Numbers
Given an array arr[] of Prime Numbers and a number M, the task is to count the number of elements in the range [1, M] that are divisible by any of the given prime numbers. Examples: Input: arr[] = {2, 3, 5, 7} M = 100 Output: 78 Explanation:In total there are 78 numbers that are divisible by either of 2 3 5 or 7. Input: arr[] = {2, 5, 7, 11} M = 20
5 min read
Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range
Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5. Example: Input:Enter minimum 100 Enter maximum 200 Output: 105 is divisible by 7 and 5. 140 is divisible by 7 and 5. 175 is divisible by 7 and 5. Input:Input:Enter minimum 29 Enter maximum 36 Output: 35 is divisible by 7 and 5.Algorithm
6 min read
three90RightbarBannerImg