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