In this example, we’ll learn how to calculate sum and average of n numbers in c# using While Loop.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
static void Main() { int count = 0; double sum = 0; double number; Console.Write("Count of Numbers: "); double n = Convert.ToDouble(Console.ReadLine()); while(true) { if(count==n) { break; } Console.Write("Number {0} : ",count+1); number= Int32.Parse(Console.ReadLine()); sum += number; count++; } Console.WriteLine("==============================="); Console.WriteLine("Sum of Numbers is {0} ",sum); Console.WriteLine("Average of numbers is {0} ",(sum/n)); Console.ReadKey(); } |
Output: