1. Use the while statement
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 statement
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 Statement (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 Statement (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 Statement (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 }
Calculate 1 + 2 + 3 + ...... + 100