C
C#14mo ago
alexlehoi

❔ Trying to find a way so it shows the sum of all the numbers entered and the also the average.

Modifier votre code Q2 afin de
calculer/afficher la somme et la moyenne des nombres saisis
*/
//inscrire votre code ici
i = 1;

while (i < 5)
{
Console.WriteLine("Entrer un chiffre ");
ligne = Console.ReadLine();

boolok = int.TryParse(ligne, out NbrEntier);
if (boolok == true)
{
if (NbrEntier <= min)
{
min = NbrEntier;


}
i++;
}

}
Console.WriteLine("nombre min : " + min);
Console.ReadKey();
Modifier votre code Q2 afin de
calculer/afficher la somme et la moyenne des nombres saisis
*/
//inscrire votre code ici
i = 1;

while (i < 5)
{
Console.WriteLine("Entrer un chiffre ");
ligne = Console.ReadLine();

boolok = int.TryParse(ligne, out NbrEntier);
if (boolok == true)
{
if (NbrEntier <= min)
{
min = NbrEntier;


}
i++;
}

}
Console.WriteLine("nombre min : " + min);
Console.ReadKey();
36 Replies
mtreit
mtreit14mo ago
Well, how do you think you might approach that?
alexlehoi
alexlehoiOP14mo ago
by creating a static string that contains the sum of all numbers
mtreit
mtreit14mo ago
Why a string? And why static? Wouldn't a numeric variable of some kind make more sense?
alexlehoi
alexlehoiOP14mo ago
something like that?
No description
mtreit
mtreit14mo ago
I think you just need a local variable. Not a static field. If you want to add numbers in C# you use a type like int
MODiX
MODiX14mo ago
mtreit
REPL Result: Success
int a = 1;
int b = 5;
int sum = a + b;
Console.WriteLine(sum);
int a = 1;
int b = 5;
int sum = a + b;
Console.WriteLine(sum);
Console Output
6
6
Compile: 578.810ms | Execution: 69.356ms | React with ❌ to remove this embed.
alexlehoi
alexlehoiOP14mo ago
hmm yea that i know but its the numbers i have entered that i want to add up together
mtreit
mtreit14mo ago
What if you declare a variable sum outside of your loop?
alexlehoi
alexlehoiOP14mo ago
like that?
No description
mtreit
mtreit14mo ago
You should assign it an initial value.
int intsum = 0;
int intsum = 0;
alexlehoi
alexlehoiOP14mo ago
int intsum=0;
mtreit
mtreit14mo ago
Now you just need to write the code so that at the end that variable contains the sum of all the input values.
alexlehoi
alexlehoiOP14mo ago
i think it has to do something with this
No description
No description
mtreit
mtreit14mo ago
Uh...
MODiX
MODiX14mo ago
mtreit
REPL Result: Success
int.MinValue
int.MinValue
Result: int
-2147483648
-2147483648
Compile: 354.892ms | Execution: 23.888ms | React with ❌ to remove this embed.
mtreit
mtreit14mo ago
I don't know what you're trying to do there.
alexlehoi
alexlehoiOP14mo ago
No description
alexlehoi
alexlehoiOP14mo ago
basically all these numbers and make a sum of all the entered numbers
mtreit
mtreit14mo ago
I understand that part, just not what you were trying to show with int.MinValue and int.MaxValue You just need to add to the sum variable...
alexlehoi
alexlehoiOP14mo ago
the min is the show the lowest value of all the numbers entered then the second loop is just to show up the max value out of all numbers entered
mtreit
mtreit14mo ago
Oh you are also getting min and max, as well as sum and average.
alexlehoi
alexlehoiOP14mo ago
yea
mtreit
mtreit14mo ago
Why would you add the min value to the entered value?
alexlehoi
alexlehoiOP14mo ago
for this part?
No description
mtreit
mtreit14mo ago
Yes...
alexlehoi
alexlehoiOP14mo ago
i think its a static int
mtreit
mtreit14mo ago
I don't think you made it static but anyway that's not really relevant to what I was trying to ask. Why are you adding min into the sum?
alexlehoi
alexlehoiOP14mo ago
No description
alexlehoi
alexlehoiOP14mo ago
No description
alexlehoi
alexlehoiOP14mo ago
i guess it didnt work
alexlehoi
alexlehoiOP14mo ago
No description
alexlehoi
alexlehoiOP14mo ago
nevermind it works now
alexlehoi
alexlehoiOP14mo ago
didnt understand the logic on how it worked here .. just my moving the line inside the if
No description
alexlehoi
alexlehoiOP14mo ago
also thanks for helping !
mtreit
mtreit14mo ago
Sure
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server