In this example, I’ll show How to Calculate average of even & odd of N numbers entered by user.
Firstly, we will ask the user how many numbers to enter.
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 28 29 30 31 | static void Main(string[] args) { int n,number, oddCount = 0,evenCount=0, oddSum=0,evenSum=0; Console.Write("N : "); n = Convert.ToInt32(Console.ReadLine()); for(int i=1;i<=n;i++) { Console.Write("Number " + i+" : "); number = Convert.ToInt32(Console.ReadLine()); if(number%2==0) { evenCount++; evenSum += number; } else { oddCount++; oddSum += number; } } if(evenCount!=0) Console.Write("Average of Even Numbers : " + (evenSum/evenCount)); Console.WriteLine(); if(oddCount!=0) Console.Write("Average of Odd Numbers : " + (oddSum/oddCount)); Console.ReadKey(); } |
Output: