SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Value Returning Functions And Modules
Exercise:
Programming Exercises
Question:7 | ISBN:9780132576376 | Edition: 2

Question

Odd/Even Counter
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 are odd.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Odd/Even Counter program code:

#import generator
import random

# Initialize counters
evenCount = 0
oddCount = 0

# Generate 100 random numbers and caclulate and display 
#count of how many of those random numbers are even and how many are odd.
print("Generated Random numbers are:")
for _ in range(100):
    number = random.randint(1, 100)
    print(" ",number)
    if number % 2 == 0:
        evenCount += 1
    else:
        oddCount += 1

Executed Output 1:


main.py
#import generator
import random


 
 Ln: 1, Col: 1 (426 selected)
Command Line Arguments
   
Generated Random numbers are:
  51
  79
  50
  38
  76
  37
  100
  83
  43
  93
  5
  71
  83
  21
  3
  76
  20
  18
  47
  83
  48
  22
  55
  93
  1
  76
  46
  23
  16
  39
  11
  96
  65
  46
  99
  56
  3
  59
  76
  75
  68
  5
  37
  34
  66
  67
  8
  65
  8
  36
  80
  76
  68
  59
  17
  21
  47
  3
  43
  19
  69
  40
  72
  70
  96
  41
  75
  66
  76
  39
  58
  59
  28
  81
  31
  88
  51
  86
  82
  39
  96
  27
  46
  9
  19
  8
  8
  28
  89
  32
  7
  53
  80
  70
  23
  2
  48
  44
  60
  44
Number of even numbers: 49
Number of odd numbers: 51

Executed Output 2:


Command Line Arguments
   
Generated Random numbers are:
  55
  19
  27
  11
  72
  71
  94
  87
  99
  39
  16
  21
  20
  69
  2
  33
  97
  9
  20
  78
  15
  95
  22
  38
  6
  7
  86
  59
  85
  85
  17
  91
  21
  40
  31
  1
  9
  72
  89
  84
  22
  47
  19
  21
  51
  78
  40
  80
  38
  98
  20
  40
  56
  58
  81
  19
  47
  81
  30
  17
  49
  41
  59
  95
  41
  54
  48
  42
  76
  47
  18
  24
  28
  67
  47
  53
  51
  93
  66
  80
  38
  40
  30
  75
  24
  30
  89
  81
  62
  22
  44
  75
  29
  61
  81
  75
  14
  62
  28
  63
Number of even numbers: 45
Number of odd numbers: 55

 

0 0

Discussions

Post the discussion to improve the above solution.