Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When input is 0 stop taking values

I need code, that takes inputs from user and then adds them together-simple. But I can't find a way, to take inputs until 0 is pressed and than add the numbers together.. So far, I made it take 10 values, but like I said it needs to be custom.. Thanks for your help.

int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
    myarray[i] = Convert.ToInt32(Console.ReadLine());
}

int a = 0;
for (int j = 0; j < 10; j++)
{
    a = a + myarray[j];
}

Console.WriteLine(a);
Console.ReadLine();
like image 840
gx15 Avatar asked Aug 01 '26 10:08

gx15


1 Answers

The code below is not limited to 10 inputs, you can give as many input as you like

int sum=0, input;
do
{
    input = Convert.ToInt32(Console.ReadLine());
    sum += input;
}
while(input != 0);

Console.WriteLine(sum);
Console.ReadLine();
like image 138
Sarwar Erfan Avatar answered Aug 02 '26 23:08

Sarwar Erfan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!