C
C#3y ago
Krispy

sum of all numbers in between [Answered]

int première = LireValeur();
int dernière = LireValeur();
CalculerSommeValeurs(première, dernière);

static int CalculerSommeValeurs(int première, int dernière)
{
while (première <= dernière)
{
Console.WriteLine(première);
première = première + 1;
}
return première;
}

static int LireValeur()
{
Console.Write("Donnez une valeur : ");
return int.Parse(Console.ReadLine());
}
int première = LireValeur();
int dernière = LireValeur();
CalculerSommeValeurs(première, dernière);

static int CalculerSommeValeurs(int première, int dernière)
{
while (première <= dernière)
{
Console.WriteLine(première);
première = première + 1;
}
return première;
}

static int LireValeur()
{
Console.Write("Donnez une valeur : ");
return int.Parse(Console.ReadLine());
}
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
FroH.LVT
FroH.LVT3y ago
you haven't done anything related to sum check your while loop again then
Krispy
KrispyOP3y ago
thats the reason I messaged here cant figure it out
FroH.LVT
FroH.LVT3y ago
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
Olivier Bélanger
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
Krispy
KrispyOP3y ago
still lost
FroH.LVT
FroH.LVT3y ago
what have you tried so far? paste your new code here
Krispy
KrispyOP3y ago
i tried adding a variable total as u said but when i put it in return it gives a red line
static int CalculerSommeValeurs(int première, int dernière)
{
int total;
while (première <= dernière)
{
total = total + première;
}
return total;
}
static int CalculerSommeValeurs(int première, int dernière)
{
int total;
while (première <= dernière)
{
total = total + première;
}
return total;
}
FroH.LVT
FroH.LVT3y ago
why you remove
première = première + 1;
première = première + 1;
Krispy
KrispyOP3y ago
🤷‍♂️
FroH.LVT
FroH.LVT3y ago
ok let's say your input : première = 1 ; dernière = 5.
Krispy
KrispyOP3y ago
it has a red line, wont work
FroH.LVT
FroH.LVT3y ago
hmhm hover to your red line read error description take a picture and send it here
Krispy
KrispyOP3y ago
use of unassigned local variable 'total'
FroH.LVT
FroH.LVT3y ago
then assign it then
Krispy
KrispyOP3y ago
give it 0?
FroH.LVT
FroH.LVT3y ago
yes
Krispy
KrispyOP3y ago
Krispy
KrispyOP3y ago
doesnt output anything
FroH.LVT
FroH.LVT3y ago
of course take a look at your while loop again you remove
première = première + 1;
première = première + 1;
Krispy
KrispyOP3y ago
0 = 0 + 1 doesnt make sense Hmm
FroH.LVT
FroH.LVT3y ago
what
Krispy
KrispyOP3y ago
i assigned 0 to total in what I wrote
FroH.LVT
FroH.LVT3y ago
total = total + première;
total = total + première;
this mean you assign a new value to total. 0 + 1 = 1 ==> your total get new value = 1
Krispy
KrispyOP3y ago
so I keep this then do total = premiere + premiere?
FroH.LVT
FroH.LVT3y ago
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
Krispy
KrispyOP3y ago
true
FroH.LVT
FroH.LVT3y ago
that why i asked you : why you remove this
première = première + 1;
première = première + 1;
Krispy
KrispyOP3y ago
is there any reason it did this:
FroH.LVT
FroH.LVT3y ago
take full picture please too hard for me with that
Krispy
KrispyOP3y ago
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
FroH.LVT
FroH.LVT3y ago
close your running program first the error is clear
Krispy
KrispyOP3y ago
well im a beginner idk what half these things mean
FroH.LVT
FroH.LVT3y ago
yeah it's fine
Krispy
KrispyOP3y ago
we did nothing in class related to sum, and the teacher is asking us to do this thats why im clueless
FroH.LVT
FroH.LVT3y ago
you are almost there
Krispy
KrispyOP3y ago
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
première = première + 1;
total = première + première;
}
return total;
}
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
première = première + 1;
total = première + première;
}
return total;
}
thats the best I can think of
FroH.LVT
FroH.LVT3y ago
total = première + première;
total = première + première;
why not total = total + premiere ignore right or wrong atm just tell me why you do that
Krispy
KrispyOP3y ago
i have no clue
FroH.LVT
FroH.LVT3y ago
FroH.LVT
FroH.LVT3y ago
can you elaborate your while loop again using my example
Krispy
KrispyOP3y ago
on this?
FroH.LVT
FroH.LVT3y ago
yes
Krispy
KrispyOP3y ago
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
Krispy
KrispyOP3y ago
idk how the 6 and 10 got here though
FroH.LVT
FroH.LVT3y ago
loop 2: 2 = 2 + 2 total = 4 + 4 returns 8 why premerie = 2 +2 ? isnt it premerie = 2 + 1 . apply this :
première = première + 1;
première = première + 1;
so premiere = 3. then total = 3 +3 = 6
Krispy
KrispyOP3y ago
ah
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
Console.WriteLine(total);
}
return première;
}
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
Console.WriteLine(total);
}
return première;
}
this the best I can come up with. It gives 15, but it writes the rest of the additions
Krispy
KrispyOP3y ago
Krispy
KrispyOP3y ago
I want it to only write 15
FroH.LVT
FroH.LVT3y ago
really simple though
Krispy
KrispyOP3y ago
I tend to overlook simple stuff
FroH.LVT
FroH.LVT3y ago
you are showing result each loop that's why it show multiple time
Krispy
KrispyOP3y ago
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
}
Console.WriteLine(total);
return première;
}
static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
}
Console.WriteLine(total);
return première;
}
FroH.LVT
FroH.LVT3y ago
ye.
Krispy
KrispyOP3y ago
finally
FroH.LVT
FroH.LVT3y ago
this should work
Krispy
KrispyOP3y ago
btw
FroH.LVT
FroH.LVT3y ago
but why return premiere 😄
Krispy
KrispyOP3y ago
thats what I was gonna ask no clue what to return 😂
FroH.LVT
FroH.LVT3y ago
return thing you need u take your time calculate total and return premier
Krispy
KrispyOP3y ago
it doesn't even return premiere at the end
FroH.LVT
FroH.LVT3y ago
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
Krispy
KrispyOP3y ago
idk what var is
FroH.LVT
FroH.LVT3y ago
ok fixed
int result = CalculerSommeValeurs(1,5)
int result = CalculerSommeValeurs(1,5)
you've finished 99%
Krispy
KrispyOP3y ago
CalculerSommeValeurs(1, 5);

static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
}
Console.WriteLine(total);
return première;
}
CalculerSommeValeurs(1, 5);

static int CalculerSommeValeurs(int première, int dernière)
{
int total = 0;
while (première <= dernière)
{
total = total + première;
première = première + 1;
}
Console.WriteLine(total);
return première;
}
FroH.LVT
FroH.LVT3y ago
good luck for your last 1%
Krispy
KrispyOP3y ago
if I do this it gives 15 still
FroH.LVT
FroH.LVT3y ago
I have to go i mean the return value not showing result to console $hello $helloworld
FroH.LVT
FroH.LVT3y ago
take some times and follow this it would help you
Krispy
KrispyOP3y ago
can we just complete this before you go? you want me to return total thats it?
FroH.LVT
FroH.LVT3y ago
kind of. It's not what I want, but you
Krispy
KrispyOP3y ago
lol ok have a good one ty for helping $close
MODiX
MODiX3y ago
Use the /close command to mark a forum thread as answered
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server