Summation Example
Description
This script shows how a mathematical operation, in this case a summation, can be applied in terms of code. In this instance we've got
, where
is the input parameter.You can see the mathematics at WolframAlpha. Moreover, you can view the Python Version or C Version of the script.
Usage
Modify Line 5 changing n to any value you wish to test.
With the default parameter of int n = 5 we get the following output:
Summation of (i^2) from i = 0 to n = 5 is 55
Code
SquareToN.cs
class SquareToN {
public static void Main(){
int n = 5;
System.Console.WriteLine( "Summation of (i^2) from i = 0 to n = {0} is {1}", n, DoSquaring( n ) );
}
public static int DoSquaring( int n ){
int x = 0;
for( int i = 0; i <= n; i++ )
x = x + ( i * i );
return x;
}
}

