In this chapter, you saw an example of how to write an algorithm that determines whether a number is even or odd. Write a program that generates 100 random numbers and keeps a count of how many of those random numbers are even, and how many of them are odd.]

Respuesta :

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Program starts here

public class RandomOddEve {

/** Main Method */

public static void main(String[] args) {

int[] nums = new int[100]; // Declare an array of 100 integers

// Store the counts of 100 random numbers

for (int i = 1; i <= 100; i++) {

nums[(int)(Math.random() * 10)]++;

}

int odd = 0, even = 0; // declare even and odd variables to 0, respectively

// Both variables will serve a counters

// Check for odd and even numbers

for(int I = 0; I<100; I++)

{

if (nums[I]%2 == 0) {// Even number.

even++;

}

else // Odd number.

{

odd++;

}

}

//.Print Results

System.out.print("Odd number = "+odd);

System.out.print("Even number = "+even);

}

Answer:

Complete python code along with comments to explain the whole code is given below.

Python Code with Explanation:

# import random module to use randint function

import random

# counter variables to store the number of even and odd numbers

even = 0

odd = 0

# list to store randomly generated numbers

random_num=[]

# A for loop is used to generate 100 random numbers

for i in range(100):

# generate a random number from 0 to 100 and append it to the list

   random_num.append(random.randint(0,100))

# check if ith number in the list is divisible by 2 then it is even

   if random_num[i] % 2==0:

# add one to the even counter

        even+=1

# if it is not even number then it must be an odd number

   else:

# add one to the odd counter

        odd+=1

# finally print the count number of even and odd numbers

print("Total Even Random Numbers are: ",even)

print("Total Odd Random Numbers are: ",odd)

Output:

Total Even Random Numbers are: 60

Total Odd Random Numbers are: 40

Total Even Random Numbers are: 54

Total Odd Random Numbers are: 46

Ver imagen nafeesahmed