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.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
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.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT