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



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

Output :

0 15 30 45 60 75 90

 

 

Last Updated on October 24, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs