sum of odd squares in csharp

main.cs
int n = 10; // change this value according to your needs
int sum = 0;
for(int i=1; i<=n; i+=2){
    sum += i*i;
}
Console.WriteLine(sum); // prints the sum of odd squares
171 chars
7 lines

Explanation:

  • We start by initializing the variable n to the upper limit of the range of odd numbers to be considered.
  • We initialize the variable sum to 0 before starting the loop.
  • The loop starts with initializing the variable i to 1, and advances by 2 at each step to consider only the odd numbers within the range of 1 to n.
  • At each iteration of the loop, we accumulate the square of the current odd number in the variable sum.
  • After the loop is finished, we print the final value of the variable sum which contains the sum of the squares of odd numbers.

gistlibby LogSnag