Search code examples
c#loopsfor-loopnested-loops

C# loops about exponential numbers


I am new on C#, i just learned loops.

I want to write loops that run this. (75 times)

  1. step1 1^1
  2. step 2 1^2+2^2
  3. step 3 1^3+2^3+3^3
  4. step 4 1^4+2^4+3^4+4^4
  5. ...

I tried this way.

for (int i = 1; i <= 75; i++)
{
   int sum = 0;
   for (int p = 1; p <= i+1; p++)
   {
      Math.Pow(p, (i + 1));
      sum = sum + p;
   }
  Console.WriteLine(sum);
}

Where am I doing wrong and how should I do about it?


Solution

  • Pow will not modify the value of p. You need to get the returned value into another variable. pp for example.

    pp = Math.Pow(p, ...