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.
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;
}
}
}
Comments
Leave a comment