Program that sums as many numbers as user wants

I need to write a program that displays the sum of user-provided numbers. The user should be able to provide as many numbers as they like, and when they are done providing numbers, the sum of those numbers needs to be displayed. I am super confused on this so please help! Thanks

5 Comments

What have you done so far? What specific problems are you having with your code? Do you know how to write a loop? Do you know how to use the input( ) function?
What is the actual wording of the assignment? Are you required to ask a yes/no question and also get the user input? Or can you just keep asking the user for input until they enter a 0?
Rik
Rik on 25 Mar 2022
Edited: Rik on 25 Mar 2022
Why did you change your username and remove the question? Are you afraid of being accused of plagiarism? I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer. The current state of the page is also now archived.
Program that sums as many numbers as user wants
I need to write a program that displays the sum of user-provided numbers. The user should be able to provide as many numbers as they like, and when they are done providing numbers, the sum of those numbers needs to be displayed. I am super confused on this so please help! Thanks
Deleted comments:
OP on 11 Feb 2021
I honestly am not sure where to start. I know how to do loops and use input but I’m confused. Do I have to do two inputs, the first one asking if they want to select a number and the second what the number is? I was also trying to make it so if the user said yes to the first question that it would then ask the second one and go into a loop, but I kept getting a bunch of errors so I don’t think I’m doing it right. I’m not really sure how to set it up so if you could offer any help I would appreciate it
OP on 12 Feb 2021
"Write a program that displays the sum of user-provided numbers. The user should be able to provide as many numbers as they like, and when they are done providing numbers, the sum of those numbers needs to be displayed."
It doesn't specify, so I would assume I can do whatever is easiest. Thanks!
In some jurisdictions you have the legal right to ask that certain publicly identifiable information about you not be publicly available. Your ability to change your username and remove profile information about your interests satisfies that legal right.
However, when you posted your Question, you agreed to the Terms and Conditions of the website... which gives Mathworks the right to retain the content .
You not only changed your profile picture and username (and perhaps also removed other information from your profile), but you also edit away your question. I had to manually reverse that and now you're suggesting you had every right to do so, just because you wanted to keep private information private. That is the problem.

Sign in to comment.

 Accepted Answer

An outline of the code could be:
total = 0;
while( true )
% insert code here to get a number from the user
% insert code here to see if the number is 0. If it is 0, then break out of the loop
% insert code here to add the number to total
end
% insert code here to display the total

3 Comments

true is a built-in function in MATLAB that returns logical true.
while true
effectively means to loop until something happens to actively stop the loop -- in this case the "if it is 0, then break out of the loop" is indicating that you should break from the loop
A modified version of Jame's outline, that permits 0 as an input:
total = 0;
while( true )
% insert code here to get a number from the user
% insert code here to see if the number is empty. If it is empty then break out of the loop
% insert code here to add the number to total
end
% insert code here to display the total
That is, when you use input() if the user just presses return, then MATLAB will receive the empty value into the variable.
deleted comment:
OP on 12 Feb 2021
I figured it out now! Thank you so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 11 Feb 2021

Commented:

Rik
on 25 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!