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

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

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

  • 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. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

  • Task 5.2 Numerical Analysis Using Nested Loops (13 pts) Consider the following program: void setup() {...

    Task 5.2 Numerical Analysis Using Nested Loops (13 pts) Consider the following program: void setup() { //Read a positive integer from the user //Your code starts here } Complete this program such that it calculates all prime numbers between 1 and the value that is assigned to variable num and outputs them on the screen. A prime number is a positive integer that has no other factors other than itself and 1. You should use a nested loop, i.e., write...

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

  • Create A MultiplicationTable program using nested for loops which will display a multiplication table for the...

    Create A MultiplicationTable program using nested for loops which will display a multiplication table for the hexadecimal numbers 1 - F. Nested for Hint: if you use the System.out.printf method with a format specifier "%X" it will display an integer value in hexadecimal format.

  • ***** JAVA ONLY ***** Write a program that uses nested loops to collect data and calculate...

    ***** JAVA ONLY ***** Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. Frist 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. After all iterations, the program should display the...

  • Using loops, write a C# program that asks the user to enter repeatedly an integer number,...

    Using loops, write a C# program that asks the user to enter repeatedly an integer number, it stops when the user enters -1, then the program should display how many numbers were entered, their the sum and their average

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