REPETITION

1) Write a program in C++ that is capable of displaying how many numbers from 0 to 100 that are divisible by 2 and 5.

2) Write a program in C++ that is able to determine and display the largest and the smallest number among any number of integers entered by the user.

CAN ANYONE HELP ME?????????
Hello blackpanther,

For #1 all of them.

So, what is your problem? What have you done so far? Do you have any code yet?

Work in small steps, compile and test what you have done before moving on to the next step.

Post your code so it can be reviewed and corrected. Without more information you are likely to get examples beyond what you know.

Andy
solution to 1st problem
cout<<72;
@anup30,

42 is better and goes with https://www.youtube.com/watch?v=aboZctrHfK8

Andy
Hello blackpanther,

using 2 for loops I did come up with this:

  2   4   6   8  10  12  14  16  18  20
 22  24  26  28  30  32  34  36  38  40
 42  44  46  48  50  52  54  56  58  60
 62  64  66  68  70  72  74  76  78  80
 82  84  86  88  90  92  94  96  98 100


  5,  10,  15,  20,  25,  30,  35,  40,  45,  50
 55,  60,  65,  70,  75,  80,  85,  90,  95, 100


 Press Enter to continue:


You can do this with or with out the comma.

This is based on the numbers having a remainder of (0)zero.

Hint the (%) helps.

Andy
Last edited on
1)
from 0 to 100 that are divisible by 2 and 5.


I'm reading that the number has to be divisible by both 2 and 5. In this case the answers 11 (0 is both divisible by 2 and 5).

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {
	unsigned cnt {};

	for (unsigned n = 0; n <= 100; ++n)
		if (n % 2 == 0 && n % 5 == 0) {
			++cnt;
			std::cout << n << ' ';
		}

	std::cout << '\n' << cnt << '\n';
}



0 10 20 30 40 50 60 70 80 90 100
11


For 2), perhaps (without input checking):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
	size_t sze {};

	std::cout << "How many numbers to enter: ";
	std::cin >> sze;

	std::cout << "Enter " << sze << " numbers: ";

	std::vector<int> vi(sze);

	std::copy_n(std::istream_iterator<int>(std::cin), sze, vi.begin());

	const auto [small, big] {std::minmax_element(vi.begin(), vi.end())};

	std::cout << "\nThe smallest number is: " << *small;
	std::cout << "\nThe largest number is: " << *big << '\n';
}

Last edited on
math...
50 numbers between 0 and 100 are divisible by 2. this is 100/2
likewise 100/5 is 20.
the question did not ask to print the numbers, but you can do so any number of ways.
for(int i = 2; i <101; i+=2)
cout << i <<" ";
and similar for 5, just say +=5 and start at 5.

or, if as in the case of divisible by BOTH,
do it by 10 (2*5). 100/10 is 10 numbers divisible by both, and the loop pattern to print holds as well. All this is just factorization basics from math if you ever did prime factors in school?

while you CAN compute the values and count them and all, it isnt necessary to do that here. If you want to include 0 which is technically divisible by anything, add 1 to your counts and print loops start at 0
Last edited on
Or even for 1) :

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <numeric>

int main() {
	constexpr unsigned fac1 {2}, fac2 {5}, last {100};
	unsigned cnt {};

	for (unsigned n {}, tst {std::lcm(fac1, fac2)}; n <= last; cnt += n++ % tst == 0);

	std::cout << '\n' << cnt << '\n';
}


Last edited on
1
2
3
4
5
6
7
8
9
void re(int n,int cnt=0) {
	if (n > 100) { cout << "count= " << cnt; return; }
	else if (n % 2) { n++; }
	else if (!(n % 5) && !(n % 2)) { cout << n << "\n"; cnt++; }
	re(n + 2,cnt);
}
int main() {
	re(1);
}
Topic archived. No new replies allowed.