-1

I'm trying to find the sum from (1 to n) or given number. using this code:

        int n;
        int counter = 0;
        int sum = 0;

        Console.Write("Please enter the sum limit number: ");
        n = int.Parse(Console.ReadLine());

        //around here is where code freezes and nothing else happens 
        while(counter <=  n)
        {
            counter = +1;
            sum = sum + counter;
        }

        Console.Write("The sum from 1 - " + n + " =" + sum);

I know I can use:

        int n;
        int counter = 0;
        int sum = 0;

        Console.Write("Please enter the sum limit number: ");
        n = int.Parse(Console.ReadLine());

        var sum = Enumerable.Range(1, n);

        Console.Write("The sum from 1 - " + n + " =" + sum.Sum());

but my next challenge is to only add the numbers that are divisible by 3 or 5, so I'm planning on doing:

        if (sum % 3 == 0 | sum % 5 == 0)
        { 
         total = total + sum;
        }

What is wrong with my method? Also, alternative ways to do this are more than appreciated!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 2
    You need to increment `counter` - try `counter += 1;` instead of `counter = +1;` – eouw0o83hf Oct 25 '18 at 01:19
  • 1
    Note that [`|` is not the same operation as `||` (short-circuiting boolean or)](https://stackoverflow.com/questions/35301/what-is-the-difference-between-the-and-or-operators) – ProgrammingLlama Oct 25 '18 at 01:20
  • 1
    @John `|` can a boolean OR as well, it just does not short-circuit (both sides are evaluated even of the first is true). – D Stanley Oct 25 '18 at 01:22
  • Thanks for the reply, so I got a value not what I was expecting but getting somewhere. I just looked up += operator and to my understanding it's equals to a+=b == a = a+b, but isn't the operation counter = +1; increasing counter + 1 as well? also if my understanding is right could I also do sum += counter;? thank you for you help! – Master Oogway Oct 25 '18 at 01:26
  • @euw0o83hf Thank you so much for your help, I got it working now and managed to make it shorted as well! – Master Oogway Oct 25 '18 at 01:46
  • For anything involving math - in particular indexes - you should be using a for loop. Sometimes foreach. While does not really fit. It makes you intention clear on a glance. Personally while is my least used loop. I tend towards using for, foreach and do...while a few orders of magnitude more. – Christopher Oct 25 '18 at 01:51

2 Answers2

1

To get out of while loop, condition needs to satisfy.First you need increment counter present in while loop.

To increment counter variable either you can try counter++/++counter i.e. post/pre increment operator or you can do counter += 1/ counter = counter + 1.

Something similar to

    //around here is where code freezes and nothing else happens 
    while(counter <=  n)
    {
        counter += 1;  // not counter=+1;
        sum = sum + counter;
    }

Reference: Increment decrement in C#

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

if you want to increment the counter you should either use

counter = counter + 1;

or

counter++;

or

counter += 1;