Given a number n , count all multiples of 3 and / or 5 in set { 1 , 2 , 3 , ... n } | Java 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 Code
import java . io . * ; class GFG { static long countOfMultiples ( long n ) { return ( n / 3 + n / 5 - n / 15 ) ; } static public void main ( String [ ] args ) { System . out . println ( countOfMultiples ( 6 ) ) ; System . out . println ( countOfMultiples ( 16 ) ) ; } }