Given a number n, count all multiples of 3 and/or 5 in set of numbers from 1 to n.

Examples:

Input: n = 6 Output: 3 There are three multiples of 3 and/or 5 in {1, 2, 3, 4, 5, 6} Input: n = 16 Output: 7 There are two multiples of 7 and/or 5 in {1, 2, .. 16} The multiples are 3, 5, 6, 9, 10, 12, 15

**We strongly recommend to minimize your browser and try this yourself first.**

The value of n/3 gives us number of multiples of 3, the value of n/5 gives us number of multiples of 5. But the important point is there are may be some common multiples which are multiples of both 3 and 5. We can get such multiples by using n/15. Following is the program to find count of multiples.

## C++

`// C++ program to find count of multiples of 3 and 5 in {1, 2, 3, ..n} ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `unsigned countOfMultiples(unsigned n) ` `{ ` ` ` `// Add multiples of 3 and 5. Since common multples are ` ` ` `// counted twice in n/3 + n/15, subtract common multiples ` ` ` `return` `(n/3 + n/5 - n/15); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `cout << countOfMultiples(6) << endl; ` ` ` `cout << countOfMultiples(16) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find count of multiples ` `// of 3 and 5 in {1, 2, 3, ..n} ` `import` `java .io.*; ` `class` `GFG { ` ` ` ` ` `static` `long` `countOfMultiples(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Add multiples of 3 and 5. ` ` ` `// Since common multples are ` ` ` `// counted twice in n/3 + n/15, ` ` ` `// subtract common multiples ` ` ` `return` `(n/` `3` `+ n/` `5` `- n/` `15` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `System.out.println(countOfMultiples(` `6` `)); ` ` ` `System.out.println(countOfMultiples(` `16` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# python program to find count ` `# of multiples of 3 and 5 in ` `# {1, 2, 3, ..n} ` ` ` `def` `countOfMultiples(n): ` ` ` `# Add multiples of 3 and 5. ` ` ` `# Since common multples are ` ` ` `# counted twice in n/3 + n/15, ` ` ` `# subtract common multiples ` ` ` `return` `(` `int` `(n` `/` `3` `) ` `+` `int` `(n` `/` `5` `) ` `-` `int` `(n` `/` `15` `)); ` ` ` ` ` `# Driver program to test ` `# above function ` `print` `(countOfMultiples(` `6` `)) ` `print` `(countOfMultiples(` `16` `)) ` ` ` `# This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find count of multiples ` `// of 3 and 5 in {1, 2, 3, ..n} ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `uint` `countOfMultiples(` `uint` `n) ` ` ` `{ ` ` ` `// Add multiples of 3 and 5. ` ` ` `// Since common multples are ` ` ` `// counted twice in n/3 + n/15, ` ` ` `// subtract common multiples ` ` ` `return` `(n/3 + n/5 - n/15); ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `// function ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `Console.WriteLine(countOfMultiples(6)); ` ` ` ` ` `Console.WriteLine(countOfMultiples(16)) ; ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find count of ` `// multiples of 3 and 5 in ` `// {1, 2, 3, ..n} ` ` ` `function` `countOfMultiples(` `$n` `) ` `{ ` ` ` ` ` `// Add multiples of 3 and 5. ` ` ` `// Since common multples are ` ` ` `// counted twice in n/3 + n/15, ` ` ` `// subtract common multiples ` ` ` `return` `floor` `(` `floor` `(` `$n` `/ 3) + ` ` ` `floor` `(` `$n` `/ 5) - ` ` ` `floor` `(` `$n` `/ 15)); ` `} ` ` ` `// Driver Code ` `echo` `countOfMultiples(6),` `"\n"` `; ` `echo` `countOfMultiples(16); ` ` ` `// This code is contributed by nitin mittal ` `?> ` |

*chevron_right*

*filter_none*

Output:

3 7

This article is contributed by **Asif**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nth number in a set of multiples of A , B or C
- Kth number from the set of multiples of numbers A, B and C
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Count of common multiples of two numbers in a range
- Count common elements in two arrays containing multiples of N and M
- Count of Multiples of A ,B or C less than or equal to N
- Count of multiples in an Array before every element
- Sum of multiples of Array elements within a given range [L, R]
- Sum of multiples of a number up to N
- Multiples of 3 or 7
- N-th multiple in sorted list of multiples of two numbers
- Queries for counts of multiples in an array
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Sum of multiples of A and B less than N
- Minimum sum after subtracting multiples of k from the elements of the array
- Sum of the multiples of two numbers below N
- Sort elements of the array that occurs in between multiples of K
- Find numbers which are multiples of first array and factors of second array
- Generate first K multiples of N using Bitwise operators