(Random Numbers) Write a single statement that prints a number at random from each of
the following sets:
Solution:
the following sets:
a) 2, 4, 6, 8, 10.
b) 3, 5, 7, 9, 11.
c) 6, 10, 14, 18, 22.
Solution:
#include < iostream> using std::cout; using std:: endl; #include < cstdlib> using std::rand; int main() { // seed by time srand( time( 0 )); //output to dislplay cout << "Random number for a) 2, 4, 6, 8, 10 is " << (rand() % 5 + 1) * 2 << endl; cout << "Random number for b) 3, 5, 7, 9, 11 is " << (rand() % 5 + 1) * 2 + 1 << endl; cout << "Random number for c) 6, 10, 14, 18, 22 is " << ((rand() % 5 + 1) * 2 + 1) * 2 << endl; return 0; }
Post A Comment:
0 comments: