Write a program that requests the user for a year (you can assume the year will always be at
least 2000), the population of Brazil in that year (in terms of millions of people) and the annual
population growth rate (always as a real number in the range 0 – 1), and then uses the two
methods written above to determine and display the year in which Brazil’s population first
exceeded/exceeds 18
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter year:");
int year = ValidateYear();
Console.Write("Enter population of Brazil(in terms of millions of people):");
double people = double.Parse(Console.ReadLine());
Console.Write("Enter population growth rate (in the range 0 – 1):");
double populatationGrowRate = ValidateRate();
Calculate(year, people, populatationGrowRate);
Console.ReadKey();
}
static void Calculate(int year, double people, double rate)
{
while (true)
{
if (people >= 18)
{
Console.WriteLine($"The number of people in Brazil has reached 18 million in {year} with annual population growth {rate}");
return;
}
people += people * (rate/100);
year++;
}
}
static int ValidateYear()
{
while(true)
{
int year = int.Parse(Console.ReadLine());
if (year >= 2000)
return year;
Console.Write("Error! Enter again: ");
}
}
static double ValidateRate()
{
while (true)
{
double rate = double.Parse(Console.ReadLine());
if (rate >= 0 && rate <= 1)
return rate;
Console.Write("Error! Enter again: ");
}
}
}
Comments
Leave a comment