Home > OS >  C# Numbers from 10 sum each other with sum < 1000 (WHILE)
C# Numbers from 10 sum each other with sum < 1000 (WHILE)

Time:11-25

So I need to make it to sum numbers after 10 and add after number is summed 1 like after 10 to sum the result before with 11 and the final result should be lower than 1000 I did mistake and typed = its only <. At the end it needs to console writeline the last number which was summed for example 35 to make the last number.

int i = 10;
int a = 10;

while (i < 1000)
{
a = a   i;
}

Console.WriteLine(a);

I have tried to sum all of them in the while but it just give me 1280

CodePudding user response:

This is an infinite loop because i is never changing from its inital value of 10

Based on your description, you intend to do

int i = 10;
int a = 10;

while (a < 1000)
{
    a  = i  ;
}

Console.WriteLine(a); //sum is 1000
Console.WriteLine(i); //last addition is 46

CodePudding user response:

            int i = 11;
            int a = 10;

            while (a < 1000)
            {
                a  = i  ;
            }

            i -= 1;
            a -= i;

            Console.WriteLine(a);
            Console.WriteLine(i);
  • Related