Question

In: Computer Science

Write a Python program that uses while loops to perform the following steps and post the...

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!

Solutions

Expert Solution

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..


Related Solutions

C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. 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 for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
Write a program with class name ForLoops that uses for loops to perform the following steps:1....
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which are less than 100. Your loop initially starts from 0. 2. Consider B to be a list of alphabetically ordered strings. Using while loops, write a program that only prints words which start with letter A. B = [Alex, Airplane, Alpha, Australia, Ben, Book, Bank, Circuit, Count, Dark] 3. Using while loop, create a program that only prints first 18 multiples of 7.Hint: Your...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Who knows to do this topic with python code? Write a program to perform the following...
Who knows to do this topic with python code? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT