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 the limit, then the program should print all numbers less than 20 that are divisible by 3 and 5. To do this, divide each number from 0 to N by 3 and 5 and check their remainder ... If the remainder is 0 in both cases, just print that number.
Below is the implementation:
C++
// C++ program for printing all numbers // divisible by 3 and 5 for 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 used if (num% 3 == 0 & amp; & amp; num% 5 == 0) cout "& lt; num "& lt; "" ; } } // Driver code int main () { // login goes here int N = 100; // Calling function result (N); return 0; } // This code is provided by Manish Shaw // (manishshaw1) |
Java
// Java program for printing all 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 & lt; N; num ++) { // short-circuit operator used if (num% 3 == 0 & amp; & amp; num% 5 == 0 ) System.out.print (num + " " ); } } // Driver code public static void main (String [] args) { // login goes here int N = 100 ; // Calling function result (N); } } |
python3
# Python program to print all 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 used if num % 3 = = 0 and num % 5 = = 0 : print ( str (num) + " " , end = " ") else : pass # Driver code if __ name__ = = "__ main__" : # login goes here N = 100 # Function call result (N) |
C #
// C # program for printing all 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 ++) { // use short-circuit statement if ( num% 3 == 0 & amp; & amp; num% 5 == 0) Console.Write (num + "" ); } } // Driver code static public void Main () { // login goes here int N = 100; // Calling function result (N); } // This code is provided by ajit. } |
PHP
& lt;? php // PHP program to print all 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 & lt; $ N ; $ num ++) { // short-circuit operator is used if ( $ num % 3 == 0 & amp; & amp; $ num % 5 == 0) echo $ num , "" ; } } // Driver code // login goes here $ N = 100; // Calling function result ( $ N ); // This code is provided by ajit ?" |
Output:
0 15 30 45 60 75 90