May 27, 2021 at 10:50am UTC
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?????????
May 27, 2021 at 11:15am UTC
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
May 27, 2021 at 11:25am UTC
solution to 1st problem
cout<<72;
May 27, 2021 at 11:53am UTC
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 May 27, 2021 at 11:55am UTC
May 27, 2021 at 12:17pm UTC
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 May 27, 2021 at 12:23pm UTC
May 27, 2021 at 12:27pm UTC
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 May 27, 2021 at 12:29pm UTC
May 27, 2021 at 12:40pm UTC
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 May 27, 2021 at 1:05pm UTC