+ 3
A program which could calculate as many numbers as user input
Write a program to calculate and display the sum of numbers given by the user The number could be as many as user wants
14 ответов
+ 18
its not my idea ,
i seen many matrix codes taking order of matrix as input , so that thay can easliy use a 2-d array with two loops to assign unique posn for each element
//then i see this idea being used by gawen in some String sort code in my early days here ...
//then i also started using it ☺😃
+ 15
another idea ,
take 1st input as" number of numbers user wants to add "
//hope it helps☺
+ 12
If it was a "Why isn't this code working?" thread instead of "Write a program.....", I'd be glad to help.
+ 12
Well done, I appreciate your effort :)
While loop checks the condition first, then executes the loop body. But for this particular problem you have to take the input first, and then check the condition. In this case use infinite loop or do while loop instead of normal loop.
Infinite loop: Set 1 as condition, that means the loop will keep running 🏃 until it gets a break. if a is 0, use break.
Do while loop: the loop body is executed first, so at least one input is known. Then check whether it's 0 or not.
Both ways have been shown in attached codes. Hope it helps.
https://code.sololearn.com/cFRieWC8fc2x/?ref=app
https://code.sololearn.com/cew5DPWQqljD/?ref=app
+ 11
You're welcome, Dipankar :) Never give up on trying.
I'm not a teacher, but I have a little experience in taking lab classes. I'm still learning ^_^
+ 10
Sounds like you're doing great :)
The sum prints 0 because your condition is sum!=0. But since the sum is 0 initially, the condition becomes false at first run and it leaves the loop.
As the requirement says, user should be allowed to give input as long as s/he wants. But there should be a point when user would like to stop. We can assume that when user enters 0, it means that he's done with all numbers. So the loop should break if num is 0. If you mention the programming language, I can help you with the loop.
Sample output:
Enter numbers to add (Press 0 to stop):
5
12
-7
35
0 ---> need to stop here
Sum: 45
You can paste your code here for assistance.
+ 9
Gaurav's way will also work. User should enter the number of input first.
Enter number of inputs: 3
2
3
4
Sum: 9
https://code.sololearn.com/cOGcDrGT7b2o/?ref=app
+ 8
@Dipankar, okay let me try to help you on this :)
Steps:
----------
1. Declare a variable (say sum) of int type and initialize it with 0.
2. Declare an int variable (say a)
3. Start a do while loop or infinite loop. In this loop you'll have to take input of a and add it to the sum. The loop should break if a certain condition is true. Like if user enters 0, then you can stop the loop.
4. Print the sum outside the loop.
Try to write the code and share it here if you get stuck :)
+ 3
I completed step 1 ,2 than i used the while loop and declared sum !=0
Inside the loop I defined sum and input num
Outside loop cout the sum but in output its only showing 0
I think i will have to include for loop also after while loop
+ 3
Thank you friend for help I really appreciate it
Ones again thanks
+ 3
are u a teacher?
+ 2
Dude seriously!!
+ 1
I am using c++turbo
I will sent u a link where i will do this program in short plz do tell my mistake