Question

In: Computer Science

Write a program to print random numbers and whether they were even or odd. Ask the...

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.

Solutions

Expert Solution

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....


Related Solutions

Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Please, write a loop to print odd numbers an array a with pointers backwards. You can...
Please, write a loop to print odd numbers an array a with pointers backwards. You can use a variable “size” for array size.
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT