[Solved] Daily Calorie Intake - C# Task

  

2
Topic starter

Kalinko is a junior software developer, who is mostly doing freelance work for random contractors. He has just been hired by a fitness instructor and is been tasked to create a software, which will assist the instructor in preparing healthy diets for his customers. In order to prepare the diets, the fitness instructor needs to know the necessary daily calorie intake for each of his clients.

The daily calorie intake (DCI), is calculated by multiplying the Basal Metabolic Rate (BMR) of the client, by a constant, which is determined by the number of workouts that the person does per week.

The BMR is calculated with the following formula:

Men: BMR = 66.5 + (13.75 x weight in kg) + (5.003 x height in cm) – (6.755 x age in years)

Women: BMR = 655 + (9.563 x weight in kg) + (1.850 x height in cm) – (4.676 x age in years)

Once the BMR is calculated, we can get the person's DCI, using the following table:

bmr table

Also, the fitness instructor that hired Kalinko, lives in the United States, which means that the weight and the height of his clients, will be given in an Imperial format – pounds (lbs.) for the weight and inches for the height. In order to make the BMR formulas work, Kalinko will have to convert Imperial values into Metric values.

Assume that 1 inch has 2.54cm and 1kg has 2.2lbs.

Your job is to help Kalinko with his first big contract and create the software for him. You will be given a person's weight in pounds (lbs.), height in inches, age, gender and number of weekly workouts, each at a separate line. Your only output, should be the person's daily calorie intake (DCI). The DCI should be rounded down to the nearest integer number.

Input

The input should be read from the console. It will consist of exactly 5 input values, each at a separate line.

  1. W – weight in pounds (lbs.)
  2. H – height in inches
  3. A – age
  4. G – gender
  5. E – workouts per week

Output

  • The output should be the calculated DCI. It should be a single number, rounded down to the nearest integer number.

Constraints

  • The W, H, A and E inputs will be valid integers, in the range [-2,147,483,648 … 2,147,483,647]
  • The G input will be a single character – 'm' for male or 'f' for female
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16MB.

Examples

c sharp task - daily calorie intake 3

c sharp task - daily calorie intake 2

c sharp task - daily calorie intake 1

1 Answer
2

I was solving this task for 2+ hours! Really big chalange! First I've tried to use the switch case - and it was not working! After that I've realized that the input can be a negative number as well! So I've changed the swith case with if statements... Good task - interesting solution! And be also careful with converting pounds and inches into kilograms and sentimeters (line 14 and 15)! Here is the code:

using System;
using System.Numerics;
 
class DailyCalorieIntake
{
    static void Main()
    {
        decimal weightPounds = decimal.Parse(Console.ReadLine());//W – weight in pounds (lbs.)
        decimal heightInches = decimal.Parse(Console.ReadLine());//H – height in inches
        decimal age = decimal.Parse(Console.ReadLine());//A – age
        string gender = Console.ReadLine();//G – gender
        decimal workoutsPerWeek = decimal.Parse(Console.ReadLine());//E – workouts per week
 
        decimal weightKg = (weightPounds / 2.2m);//1kg = 2.2 lbs.
        decimal heightSm = (heightInches * 2.54m);//1 sm. = 0.3937007874 inches
 
        decimal menBMR = 0;
        decimal femaleBMR = 0;
        if (gender == "m")
        {
            menBMR = (66.5m + (13.75m * weightKg) + (5.003m * heightSm) - (6.755m * age));
        }
        else if (gender == "f")
        {
            femaleBMR = (655 + (9.563m * weightKg) + (1.850m * heightSm) - (4.676m * age));
        }
        else
        {
            Console.WriteLine("WRONG INPUT! Please enter \"f\" for female or \"m\" for men");
        }
 
        decimal menDCI = 0;
        if (workoutsPerWeek <= 0)
        {
            menDCI = (menBMR * 1.2m);
        }
        else if (workoutsPerWeek == 1 || workoutsPerWeek == 2 || workoutsPerWeek == 3)
        {
            menDCI = (menBMR * 1.375m);
        }
        else if (workoutsPerWeek == 4 || workoutsPerWeek == 5 || workoutsPerWeek == 6)
        {
            menDCI = (menBMR * 1.55m);
        }
        else if (workoutsPerWeek == 7 || workoutsPerWeek == 8 || workoutsPerWeek == 9)
        {
            menDCI = (menBMR * 1.725m);
        }
        else
        {
            menDCI = (menBMR * 1.9m);
        }
 
        if (gender == "m")
        {
            Console.WriteLine("{0}", Math.Floor(menDCI));
        }
 
        decimal femaleDCI = 0;
        if (workoutsPerWeek <= 0)
        {
            femaleDCI = (femaleBMR * 1.2m);
        }
        else if (workoutsPerWeek == 1 || workoutsPerWeek == 2 || workoutsPerWeek == 3)
        {
            femaleDCI = (femaleBMR * 1.375m);
        }
        else if (workoutsPerWeek == 4 || workoutsPerWeek == 5 || workoutsPerWeek == 6)
        {
            femaleDCI = (femaleBMR * 1.55m);
        }
        else if (workoutsPerWeek == 7 || workoutsPerWeek == 8 || workoutsPerWeek == 9)
        {
            femaleDCI = (femaleBMR * 1.725m);
        }
        else
        {
            femaleDCI = (femaleBMR * 1.9m);
        }
 
        if (gender == "f")
        {
            Console.WriteLine("{0}", Math.Floor(femaleDCI));
        }
    }
}
Share: