c# 计算1-100之间的所有奇数的和

求1-100之间所有奇数整数和:

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5            int sum= GetOdd(1,100);
 6             Console.WriteLine("1-100之间所有奇数整数和{0}",sum);
 7             Console.ReadKey();
 8         }
 9         /// <summary>
10         /// 奇数和
11         /// </summary>
12         /// <param name="p1"></param>
13         /// <param name="p2"></param>
14         /// <returns></returns>
15         private static int GetOdd(int p1, int p2)
16         {
17             int sum = 0;
18             for (int i = p1; i <= p2; i++)
19             {
20                 if (i%2!=0)
21                 {
22                     sum += i;
23                 }
24             }
25             return sum;
26         }
27     }
View Code

 

posted @ 2014-02-16 22:24  土豆哥  阅读(3453)  评论(0编辑  收藏  举报