码迷,mamicode.com
首页 > 其他好文 > 详细

用循环语句求1+2+3+……+100

时间:2014-09-13 21:22:25      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   div   sp   log   on   

1.用while语句

 1 using System;
 2 class Sum100
 3     {
 4         static void Main()
 5         {
 6             int Sum, i;
 7             Sum = 0; i = 1;
 8             while (i<=100)
 9             {
10                 Sum += i;
11                 i++;
12             }
13             Console.WriteLine("Sum is" + Sum);
14         }
15     }

2.do-while语句

 1 using System;
 2 class Sum100
 3     {
 4         static void Main()
 5         {
 6             int Sum, i;
 7             Sum = 0; i = 1;
 8             do 
 9             {
10                 Sum += i;
11                 i++;
12             }
13             while (i<=100)
14             Console.WriteLine("Sum is" + Sum);
15         }
16     }

3.for语句(1)

 1 using System;
 2 
 3 class Sum100
 4     {
 5         static void Main()
 6         {
 7             int Sum, i;
 8             Sum = 0;
 9             for (i = 1; i <= 100;i++ )
10                 Sum += i;
11             Console.WriteLine("Sum is" + Sum);
12 
13             Sum = 0;
14             for (i = 100; i > 0;i-- )
15                 Sum += i;
16             Console.WriteLine("Sum is" + Sum);
17         }
18     }

4.for语句(2)

 1 using System;
 2 
 3 class Sum100
 4     {
 5         static void Main(string[] args)
 6         {
 7             int Sum, i;
 8             
 9             for (Sum = 0,i = 1; i <= 100;i++ )
10                 Sum += i;
11             Console.WriteLine("Sum is" + Sum);
12 
13             
14             for (Sum = 0,i <= 100; Sum+i,i++);
15                
16             Console.WriteLine("Sum is" + Sum);
17         }
18     }

5.for语句(3)

 1 using System;
 2 class Sum100
 3     {
 4         static void Main(string[] args)
 5         {
 6             int Sum, i;
 7 
 8             for (Sum = 0, i = 1; i <= 100; )
 9                 Sum += i++;
10             Console.WriteLine("Sum is" + Sum);
11 
12             
13             for (Sum = 0,i =1; ;Sum+=i,i++)
14                if(i>100)  break;
15             Console.WriteLine("Sum is" + Sum);
16 
17             Sum=0;i=1;
18             for(;;)
19             {
20                 Sum+=i++;
21                 if (i>100)
22                     break;
23             }
24             Console.WriteLine("Sum is" + Sum);
25         }
26     }

 

用循环语句求1+2+3+……+100

标签:style   blog   color   ar   for   div   sp   log   on   

原文地址:http://www.cnblogs.com/zhongliyufei/p/3970234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!