Question

Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the to
Lab 5-2 Nested Loops Part B: The program from part A will run several times, as long as the user answers y or Y when aske
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Code to copy along with screenshots of code and output are provided.
  • If you have any doubts or issues. Feel free to ask in comments
  • Please give this answer a like, or upvote. This will be very helpful for me.

============================= PART - A =================

Screenshots of Code :

1 ll<<<<<<-------PART-A-- ->>>> 2 3 4 # taking input for first time num = int(input (Enter a positive number: )) 5 # while

Screenshots of Output :

Enter a positive number: -5 wrong input, try again Enter a positive number: -9 wrong input, try again Enter a positive number

11 Enter a positive number: 3 The sum from 1 to 3 is 6

Code to copy:

'''<<<<<<-------PART -A------->>>>>'''


# taking input for first time
num = int(input("Enter a positive number: "))

# while loop for input validation
while num <= 0:
    print("wrong input, try again")
    num = int(input("Enter a positive number: "))

# sum varaible to store sum
total = 0
# variable to control loop
i = 1

#loop to calculate sum
while i<=num:
    total = total + i
    i = i + 1

# printing sum
print("The sum from 1 to ",num, "is ",total)

=============================== PART-B ======================

Screenshots of Code :

1 ------PART -B-- 2 3 # ansver variable to store answer answer = y 4 5 6 7 # loop until ansver is y while answer == y o

Screenshots of Output :

Y Enter a positive number: -2 wrong input, try again Enter a positive number: 3 The sum from 1 to 3 is 6 Do you want to enter

Code to copy:

'''<<<<<<-------PART -B------->>>>>'''

# answer variable to store answer
answer = 'y'

# loop until answer is 'y'
while answer == 'y' or answer == 'Y':
    # taking number input for first time
    num = int(input("Enter a positive number: "))

    # while loop for input validation
    while num <= 0:
        print("wrong input, try again")
        num = int(input("Enter a positive number: "))

    # sum varaible to store sum
    total = 0
    # variable to control loop
    i = 1

    # loop to calculate sum
    while i <= num:
        total = total + i
        i = i + 1

    # printing sum
    print("The sum from 1 to ", num, "is ", total)

    # taking input for whether want to enter another number
    answer = input("Do you want to enter another number?(y/n):  ")

=======================================================

Add a comment
Know the answer?
Add Answer to:
Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Create a java application that uses nested loops to collect data and calculate the average...

    5. Create a java application that uses nested loops to collect data and calculate the average rainfall over a peniod of years First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month, the program should display the number of months, the total...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

  • Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops...

    Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations,...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • In this assignment, you will write a program in C++ which uses files and nested loops...

    In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...

  • Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by...

    Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by the user. The user should enter each number at the command prompt. The user will indicate that he or she is finished entering the number by entering the number 0.   Example This program will sum a series of numbers. Enter the next number (enter 0 when finished) > 5 Enter the next number (enter 0 when finished) > 2 Enter the next number (enter...

  • two part question - answered in Python 3 2). Decision Structure: A travel agency is checking...

    two part question - answered in Python 3 2). Decision Structure: A travel agency is checking whether its tours are good for customers. It follows the table below to label each tour. (e.g., if a tour has 6 destinations and its price is $600, then it should be labelled as ‘expensive’.) Number of Destinations Price Label <5 <300 or >=300 good or expensive >=5 <500 or >=500 Good or Expensive Write a program that asks the user to enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT