write a program to print first 50 multiple of 7. Q basic
Answers
Answer:
Given a positive integer n, find count of all multiples of 7 less than or equal to n.
Examples :
Input : n = 10
Output : Count = 4
The multiples are 3, 6, 7 and 9
Input : n = 25
Output : Count = 10
The multiples are 3, 6, 7, 9, 12, 14, 15, 18, 21 and 24
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
A Simple Solution is to iterate over all numbers from 1 to n and increment count whenever a number is a multiple of 3 or 7 or both.
// A Simple C++ program to find count of all
// numbers that multiples
#include<iostream>
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
int res = 0;
for (int i=1; i<=n; i++)
if (i%3==0 || i%7 == 0)
res++;
return res;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Output :
Count = 10
Time Complexity : O(n)
An efficient solution can solve the above problem in O(1) time. The idea is to count multiples of 3 and add multiples of 7, then subtract multiples of 21 because these are counted twice.
count = n/3 + n/7 - n/21
// A better C++ program to find count of all
// numbers that multiples
#include<iostream>
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
return n/3 + n/7 -n/21;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Explanation:
Hope this helps uh
Mark me as brainliest ❤
Answer:
first 50 multiple of 7. Q
Explanation:
..... ....... ..... ...