In: Computer Science
Write a Python program that uses while loops to perform the following steps and post the text code. Thanks
a. Prompt the user to input two positive integers (no zero or
negative). variables: firstNum and
secondNum (firstNum must be less
than secondNum). Validate the user's input; prompt
the user again if firstNum is not less than
secondNum (use while loop).
b. Output all odd numbers between firstNum and
secondNum. (use while loop).
c. Output the sum of all even numbers between
firstNum and secondNum. (use
while loop).
d. Output the sum of the square of the odd numbers between
firstNum and secondNum. (use
while loop)
Program layout for each step:
#A
while loop
#B
while loop
#C
while loop
#D
while loop
***Allow the user to repeat the program. (requires another while loop).
***Do NOT define any functions.
***Your program output must be exactly the same as the output in the OUTPUT section, except font style.
OUTPUT:
- The bold text is the user's input.
*************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number: Enter numbers: 8 a Incorrect Input. Please try again. Enter two positive integer numbers. First number must be less than the second number: Enter numbers:
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number: Enter numbers: a 8 Incorrect Input. Please try again. Enter two positive integer numbers. First number must be less than the second number: Enter numbers:
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number: Enter numbers: 8 2 First number must be less than the second number! Please try again. Enter two positive integer numbers. First number must be less than the second number: Enter numbers:
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number: Enter numbers: -2 8 No negative numbers! Please try again. Enter two positive integer numbers. First number must be less than the second number: Enter numbers:
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number you enter Enter numbers: 2 8 Odd integers between 2 and 8 are: 3 5 7 Sum of even integers between 2 and 8 = 20 Sum of the squares of odd integers between 2 and 8 = 83 Do you want to repeat this program? y/n > y
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number you enter Enter numbers: 1 9 Odd integers between 1 and 9 are: 1 3 5 7 9 Sum of even integers between 1 and 9 = 20 Sum of the squares of odd integers between 1 and 9 = 165 Do you want to repeat this program? y/n > y
*************************************************************************************
Enter two positive integer numbers. First number must be less than the second number you enter Enter numbers: 11 15 Odd integers between 11 and 15 are: 11 13 15 Sum of even integers between 11 and 15 = 26 Sum of the squares of odd integers between 11 and 15 = 515 Do you want to repeat this program? y/n > n Bye!
Note:-
Make your indentation correct before running the code.
Python code for given problem:-
#code starts here
while(1):
firstNum=0
secondNum=0
#A-option
while(1):
print("\n****************************************************************")
print("Enter two positive intiger numbers.")
print("First number must be less than the second number:")
print("Enter numbers:")
firstNum,secondNum=input().split(" ")
#cheack for the string is negative or postive digit or not
if firstNum.lstrip('+-').isdigit() and
secondNum.lstrip('+-').isdigit():
#cheack for 1st number is less than 2nd or not
if(int(firstNum) < int(secondNum)):
#cheack for number is positive or not
if(int(firstNum)<0):
print("\nNo negative numbers!")
print("Please try again.")
else:
break
else:
print("\nFirst number must be less than the second number:")
print("Please try again.")
else:
print("\nIncorrect Input.")
print("Please try again.")
print("\nEnter two positive intiger numbers.")
print("First number must be less than the second number:")
print("Enter numbers:")
#B-option
#print all the odd digit using modulo operation
first=int(firstNum)
second=int(secondNum)
print("\nOdd integers between "+firstNum+" and "+secondNum+"
are:")
while(first<=second):
if first%2 is 1:
print(first,end=" ")
first=first+1
print("")
#C-option
#sum all even numbers between given range
first=int(firstNum)
print("\nSum of even intiger between "+firstNum+" and "+secondNum+"
= ",end="")
sum=0
while(first<=second):
if first%2 is 0:
sum+=first
first=first+1
print(sum)
#D-Option
#sum squares of odd integer between a given range
first=int(firstNum)
print("\nSum of the squares of odd integers between "+firstNum+"
and "+secondNum+" = ",end="")
sum=0
while(first<=second):
if first%2 is 1:
sum+=(first*first)
first=first+1
print(sum)
print("\nDo you want to repeat this program?")
print("y/n")
ch=input()
if(ch=='n' or ch=='N'):
break
print("\nBye!")
#code ends here
Screenshots of code:-


Screenshots of output:-




Like, Please..
Comment if you have any Queries..