#region 得到1-n所有的奇数的和
/// <summary>
/// 得到1-targetNum所有奇数的和
/// </summary>
/// <param name="targetNum"></param>
/// <returns>sum返回所有奇数的和</returns>
public static int GetSum(int targetNum)
{
int sum = 0;
for (int i = 1; i <= targetNum; i=i+2)
{
sum += i;
}
return sum;
}
#endregion
----------------------------------------------------------------------------
#region 求1-100所有奇数的和
//int sum = GetSum(100);
//Console.WriteLine(sum);
//Console.ReadKey();
#endregion
原文链接: http://blog.csdn.net/mypc2010/article/details/8009803