Ask Question

Which of the following C+ + statements displays a random number in the range 1 through 10?

a)

cout << 1 + rand () % 10;

b)

cout << 1 + rand () % (10 - 1 + 1);

c)

cout << 1 + rand () % 10 - 1 + 1;

d)

first and second answers are correct

e)

all of the above

+1
Answers (1)
  1. 18 May, 05:58
    0
    Option (e)

    Explanation:

    rand () function results into a random number between 0 to a maximum number RAND_MAX which is a constant. To range it from 1, 1 is added and to decide its maximum value it should be modulo with that number. For example - if a number generated is 56 the 56%10 will be 6 which is in the range of 1 to 10. Option (a) is same as Option (b) because 1 is added and then subtracted which is neutral. Similarly, option (c) is also same.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Which of the following C+ + statements displays a random number in the range 1 through 10? a) cout ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers