sum of all numbers in between [Answered]
I'm trying to make it output the sum of two numbers. For example, if I enter 1 and 5, it should show 15 which is 1+2+3+4+5. What I programmed shows 1 2 3 4 5
74 Replies
you haven't done anything related to sum
check your while loop again then
thats the reason I messaged here
cant figure it out
you need something to store your sum value
you don't have it yet
do something like . create a variable total. in your while loop, increase it by premiere
currently you are increasing premiere, not summing anything at all
by adding to "première", you will exit your loop with your maximum number + 1. Try to do it on paper to understand the addition that is being made
still lost
what have you tried so far?
paste your new code here
i tried adding a variable total as u said but when i put it in return it gives a red line
why you remove
🤷♂️
ok let's say your input : première = 1 ; dernière = 5.
it has a red line, wont work
hmhm
hover to your red line
read error description
take a picture and send it here
use of unassigned local variable
'total'
then assign it then
give it 0?
yes
doesnt output anything
of course
take a look at your while loop again
you remove
0 = 0 + 1
doesnt make sense
what
i assigned 0 to total
in what I wrote
this mean you assign a new value to total. 0 + 1 = 1 ==> your total get new value = 1
so I keep this then do total = premiere + premiere?
ok do it step by step
let's say here is your input : première = 1 ; dernière = 5.
first loop your premiere = 1. your total = 0
total = total + première ==> total = 1
2nd loop your premiere =1 . your total = 1
total = 1+ 1==> total = 2
3rd loop your premiere =1 . your total = 2
total = 2+ 1==> total = 3
.....
So on
currently your loop is infinite
it would never stop
true
that why i asked you : why you remove this
is there any reason it did this:
take full picture please
too hard for me with that
has personal info
sec
Could not copy "" to "bin\Debug\net6.0\". Beginning retry 1 in 1000ms. The process cannot access the file 'bin\Debug\net6.0\' because it is being used by another process. The file is locked by: "" C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 5097
close your running program first
the error is clear
well im a beginner idk what half these things mean
yeah it's fine
we did nothing in class related to sum, and the teacher is asking us to do this
thats why im clueless
you are almost there
thats the best I can think of
why not total = total + premiere
ignore right or wrong atm
just tell me why you do that
i have no clue
can you elaborate your while loop again using my example
on this?
yes
loop 1: if I enter 1
1 = 1 + 1
total = 2 + 2
returns 4
loop 2:
2 = 2 + 2
total = 4 + 4
returns 8
loop 3:
3 = 3 + 3
total = 6 + 6
returns 12
idk how the 6 and 10 got here though
loop 2:
2 = 2 + 2
total = 4 + 4
returns 8
why premerie = 2 +2 ? isnt it premerie = 2 + 1 . apply this :
so premiere = 3.
then total = 3 +3 = 6
ah
this the best I can come up with. It gives 15, but it writes the rest of the additions
I want it to only write 15
really simple though
I tend to overlook simple stuff
you are showing result each loop
that's why it show multiple time
ye.
finally
this should work
btw
but why return premiere
😄
thats what I was gonna ask
no clue what to return 😂
return thing you need
u take your time calculate total
and return premier
it doesn't even return premiere at the end
don't you see it weird
no it does
if you call your function like this : int result = CalculerSommeValeurs(1,5)
result = 5 aka your premiere in your function instead of total = 15
idk what var is
ok fixed
you've finished 99%
good luck for your last 1%
if I do this it gives 15 still
I have to go
i mean the return value
not showing result to console
$hello
$helloworld
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
take some times and follow this
it would help you
can we just complete this before you go?
you want me to return total thats it?
kind of. It's not what I want, but you
lol ok
have a good one ty for helping
$close
Use the
/close
command to mark a forum thread as answered✅ This post has been marked as answered!