Question: Ask the user to enter an integer between 1 and 2 0 . Use a while loop to calculate the sum of the first N

Ask the user to enter an integer between 1 and 20.
Use a while loop to calculate the sum of the first N even numbers.
Display the calculated sum.
Inside the while loop, you must also count how many times the loop is executed. Display this count after printing the sum.
Note: Make sure to handle the user input gracefully. If the user enters an invalid number, prompt them to enter again.
Please enter an integer between 1 and 20:0
0 is an invalid input! Please enter an integer between 1 and 20: 21
21 is an invalid input! Please enter an integer between 1 and 20:5
The sum of the first 5 even numbers is: 20
The loop executed 5 times.
Ask the user to enter an integer between 1 and 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!