In: Computer Science
Write a program to print random numbers and whether they were
even or odd. Ask the user to enter a positive number to indicate
how many random numbers to pull (verify number is positive). Zero
is positive.
Declare two variables and initialized to zero.
int evens = 0, odds = 0;
Use them to count how many evens and how many odds were
pulled.
Enter how many random numbers to pull : 3
41
odd
18467 odd
6334 even
1 even and 2 odd random numbers were generated.
CODE:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int min=0, max=2000, evens=0,odds=0,i,n;
cout<<"Enter how many random numbers to pull : ";
cin>> n;
srand(time(0));
for (i = 0; i < n; i++) {
int num = (rand() % (max - min + 1)) + min;
if(num%2==0)
{
evens=evens+1;
cout<<num<<" even "<<endl;
}
else
{
odds=odds+1;
cout<<num<<" odd "<<endl;
}
}
cout<< evens <<" even and "<<odds <<" odd
random numbers were generated";
return 0;
}
OUTPUT:
If you have any doubts please COMMENT....
If you understand the answer please give THUMBS UP....