✔ C Sharp File IO⚡ Find min, max & avg of numbers from .txt file πŸ”₯

using System;
using System.IO;
class Ex{
  public static void Main(string[]args){
    int min = 0, max = 0, sum = 0, num, cnt = 0;
    string line;
    using(StreamReader file = new StreamReader("ex1.txt")){
      while(null != (line = file.ReadLine())){
        cnt ++;
        num = int.Parse(line);
        sum += num;
        if(min > num || 1 == cnt) min = num;
        if(max < num || 1 == cnt) max = num;
      }
    }
    float avg = (sum/(1.0f*cnt));
    Console.WriteLine("min: {0}, max: {1}, avg: {2}, sum: {3}", min, max, avg, sum);
  }
}
Download

Comments

Popular posts from this blog