Answer to Question #334953 in C# for kun

Question #334953

a) Write a method PopulationTotal that accepts 2 positive values, namely the current





population and the growth rate . The method determines and returns the total population based on the current





population and growth rate.




b) Write a method Over180Million that accepts a positive value representing the population. The method determines whether the population is over 180 million people, and returns




a value of true if this is so, otherwise returns a value of false.




c) 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 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 180 million people. The program must also display Brazil’s total population




in this year.

1
Expert's answer
2022-04-28T13:21:21-0400
class Program
{
    static void Main()
    {
        int year = Read("Enter the year (2000+): ", 2000, 3000),
            population = Read("Enter the population of the year above: ", 0, int.MaxValue),
            growthRate = Read("Enter the growth rate: ");


        if (growthRate == 0)
        {
            Console.WriteLine("Growth rate too low!");
            return;
        }


        int total = population;
        while (!Over180Million(total))
        {
            total += PopulationTotal(total, growthRate);
            year++;
        }


        Console.WriteLine($"Population is over 180 million will be in {year}, with total population: {total}");
    }


   static int PopulationTotal(int population, int growthRate)
    {
        if (population * growthRate < 0)
        {
            Console.WriteLine("Values should be positive!");
            return 0;
        }


        return population + growthRate;
    }


    static bool Over180Million(int population)
    {
        if (population < 0)
        {
            Console.WriteLine("Values should be positive!");
            return false;
        }


        return population > 18 * 1_000_000;
    }


    static int Read(string message, int min = 0, int max = 100)
    {
        while (true)
        {
            Console.Write(message);
            if (int.TryParse(Console.ReadLine(), out int result) &&
                result >= min && result <= max)
                return result;
            Console.WriteLine("Incorrect value of mark, pleae, try again!");
        }
    }
}

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
New on Blog
APPROVED BY CLIENTS