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
<em> except ValueError:</em>
<em> print("Invalid integer! ...")</em>
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
<em> for i in range(num):</em>
<em> sum+=odd</em>
<em> odd+=2</em>
This prints the total
print("Total:",sum)