28Adding Numbers

In previous programs, you asked the user for repeated input by writing the input statements multiple times. But it’s more efficient to use loops to deal with repeated input.

Write a program that prompts the user for five numbers and computes the total of the numbers.

Example Output

 
Enter a number: 1
 
Enter a number: 2
 
Enter a number: 3
 
Enter a number: 4
 
Enter a number: 5
 
The total is 15.

Constraints

  • The prompting must use repetition, such as a counted loop, not three separate prompts.

  • Create a flowchart before writing the program.

Challenges

  • Modify the program to prompt for how many numbers to add, instead of hard-coding the value. Be sure you convert the input to a number before doing the comparison.

  • Modify the program so ...

Get Exercises for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.