- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiples of 3 or 7 in C++
Given a number n, we need to find the count of multiples of 3 or 7 till n. Let's see an example.
Input
100
Output
43
There are total of 43 multiples of 3 or 7 till 100.
Algorithm
Initialise the number n.
Initialise the count to 0.
Write a loop that iterates from 3 to n.
Increment the count if the current number is divisible by 3 or 7.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int getMultiplesCount(int n) { int count = 0; for (int i = 3; i <= n; i++) { if (i % 3 == 0 || i % 7 == 0) { count++; } } return count; } int main() { cout << getMultiplesCount(100) << endl; }
Output
If you run the above code, then you will get the following result.
43
- Related Articles
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
- The sum of three consecutive multiples of 7 is 63. Find multiples.
- Multiples of 3 and 5 without using % operator in C++
- The sum of the squares of two consecutive multiples of 7 is 637. Find the multiples.
- Write the cubes of 5 natural numbers of which are multiples of 7 and verify the following:‘The cube of a multiple of 7 is a multiple of $7^3$′.
- Find the sum of first 8 multiples of 3.
- Find the sum of first five multiples of $3$.
- Find the mean of first five multiples of 3.
- Find the mean of the first five multiples of 3.
- Print first m multiples of n in C#
- Determine two consecutive multiples of 3 whose product is 270.
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Queries for counts of multiples in an array in C++
- Print multiples of Unit Digit of Given Number in C Program

Advertisements