[转]计算1-100之间的所有奇数的和
class Program{
static void Main(string[] args)
{
int sum= GetOdd(1,100);
Console.WriteLine("1-100之间所有奇数整数和{0}",sum);
Console.ReadKey();
}
/// <summary>
/// 奇数和
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
private static int GetOdd(int p1, int p2)
{
int sum = 0;
for (int i = p1; i <= p2; i++)
{
if (i%2!=0)
{
sum += i;
}
}
return sum;
}
}
页:
[1]