Answer to Question #206562 in C# for Nkhululeko Chabala

Question #206562

Write a program that reads in 10 midday temperatures for Port Elizabeth, for 10 consecutive days. Only temperatures higher than 0 and less than 45 are valid (working with integer values for temperatures). It must calculate and display the following:  The warmest temperature  The average temperature.  The number of days that the temperature was higher than 30.


1
Expert's answer
2021-06-13T18:18:09-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Q206562
{
    class Program
    {
        static void Main(string[] args)
        {
            int warmestTemperature = 0;
            int sumTemperature = 0;
            int numberDays=0;
            for (int i = 0; i < 10; i++)
            {
                int temperature = getTemperature(i);
                if (temperature > warmestTemperature) {
                    warmestTemperature = temperature;
                }
                if(temperature>30){
                    numberDays++;
                }
                sumTemperature += temperature;


            }
            Console.WriteLine("\nThe warmest temperature: {0}", warmestTemperature);
            Console.WriteLine("The average temperature: {0}", (sumTemperature/10));
            Console.WriteLine("The number of days that the temperature was higher than 30: {0}\n", numberDays);


            Console.ReadLine();
        }


        static int getTemperature(int n)
        {
            int temperature = -1;
            while (temperature < 0 || temperature > 45)
            {
                Console.Write("Enter the temperature {0} [0-45]: ",(n+1));
                if (!int.TryParse(Console.ReadLine(), out temperature))
                {
                    temperature = -1;
                }
            }
            return temperature;
        }


      


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS