22.9k views
1 vote
Write a code that will generate one random integer between 300 and 400 and will print whether the random integer is an even or an odd number. (Hint: an even number is divisible by2, whereas an odd number is not, so check the remainder after dividing by 2.)

User WellBloud
by
2.9k points

2 Answers

3 votes

Answer:

import random

x = random.randrange(300, 400)

if x%2 == 0:

print(x, "is an even number")

else:

print(x, "is an odd number")

Step-by-step explanation:

The code is written in python

The question ask us to generate a random number and confirm if the generated number is an even number or odd number.

import random

This line of code is use to import the function random . The function is use to generate a random number.

x = random.randrange(300, 400)

The code generate a random number between the range 300 and 400.

if x%2 == 0:

If x(x is the generated random number) divided by 2 has no remainder the value is an even number.

print(x, "is an even number")

The code means print the generated number is an even number if the number is even.

else:

otherwise

print(x, "is an odd number")

The code means print the generated number is an odd number if the number is an odd number.

2 votes

Python Code with Explanation:

# import random module to use randint function

import random

# generate a random number between 300 to 400

num = random.randint(300,400)

# check whether the generated random number is divisible by 2 then it is even

if num % 2==0:

print("The generated number is even: ", num)

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

else:

print("The generated number is odd: ", num)

Output Results:

The generated number is odd: 303

The generated number is even: 386

Write a code that will generate one random integer between 300 and 400 and will print-example-1
Write a code that will generate one random integer between 300 and 400 and will print-example-2
User Paolo Broccardo
by
3.4k points