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

17 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// C++ program to print all the numbers// divisible by 3 and 5 for a given number#include <iostream>using namespace std; // Result function with Nvoid 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 codeint main(){        // input goes here    int N = 100;         // Calling function    result(N);    return 0;} // This code is contributed by Manish Shaw// (manishshaw1)
// 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);    }}
# Python program to print all the numbers# divisible by 3 and 5 for a given number # Result function with Ndef 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 codeif __name__ == "__main__":         # input goes here    N = 100         # Calling function    result(N)
// C# program to print all the numbers// divisible by 3 and 5 for a given numberusing 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 program to print all the numbers// divisible by 3 and 5 for a given number // Result function with Nfunction 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 functionresult($N); // This code is contributed by ajit?>
<script> // Javascript  program to// print all the numbers// divisible by 3 and 5// for a given number // Result function with Nfunction 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 herelet N = 100; // Calling functionresult(N); // This code is contributed by Bobby </script>
Output : 
0 15 30 45 60 75 90 
 
manishshaw1
ukasp
jit_t
arorakashish0911
divisibility
Python
Python Programs
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary