Write a program asks the user for an integer N and then adds up the first N odd integers. For example, if the user asks the sum of the first 10 odd integers, the program computes: Use a loop for this. Use instructions and pseudo instructions up to chapter 24. Use exception handler services for input and output. How many odd numbers: 10 The sum: 100 If the user enters a negative integer or non-integer, just print a zero. Start your source file with comments that describe it:

Respuesta :

Answer:

The program in Python is as follows:

while True:

   try:

       num = input("Number of odds: ")

       num = int(num)

       break

   except ValueError:

       print("Invalid integer! ...")

sum = 0

odd = 1

if num > 0:

   for i in range(num):

       sum+=odd

       odd+=2

print("Total:",sum)

Explanation:

This is repeated until a valid integer is inputted

while True:

This uses exception

   try:

This gets the number of odd numbers

       num = input("Number of odds: ")

This converts the input to integer

       num = int(num)

       break

If input is invalid, this catches the exception

   except ValueError:

       print("Invalid integer! ...")

This initializes sum to 0

sum = 0

This initializes odd to 1

odd = 1

If input is positive

if num > 0:

This add the first num odd numbers

   for i in range(num):

       sum+=odd

       odd+=2

This prints the total

print("Total:",sum)