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)
Step-by-step 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)