a) Write a program in C++ using repetition that is capable of displaying how many numbers from 0 to 100 that is divisible by 2 and 5.
b) Write a program in C++ using repetition that is able to determine and display the largest and the smallest number among any number of integers entered by the user.
a) Write a program in C++ using repetition that is capable of displaying how many numbers from 0 to 100 that is divisible by 2 and 5.
#include <iostream>
using namespace std;
int main()
{
int count = 0;
for (int i = 0; i < 100; i++ )
// if a number is divisible by 2 and 5, it is divisible by 10
if (i % 2 == 0 and i % 5 == 0) { // or i % 10 == 0
count++;
}
}
cout << count << endl;
return 0;
}
b) Write a program in C++ using repetition that is able to determine and display the largest and the smallest number among any number of integers entered by the user.
#include <iostream>
using namespace std;
int main()
{
int min = INT_MAX, max = INT_MIN;
int value;
while (cin >> value) {
if (min > value) min = value;
if (max < value) max = value;
}
cout << "min=" << min << endl;
cout << "max=" << max << endl;
return 0;
}
Comments
Leave a comment