In: Computer Science
Loop Introduction Assignment
Please write a program in c#
Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3.
Your program will contain three functions:
Function #1:
Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows:
BMI = Weight * 703 / Height2
Your function will do this using a for loop and must display the BMI for 3 people, one at a time. In other words, display the BMI for person
before asking for the next person’s information.
Function #2:
This function will do the exact same thing as function #1, except you will use a do-while loop.
Function #3:
Using a while loop, write the same steps as function #1, but as an indefinite loop where we continue to ask the user if they would like to calculate the BMI again. Continue to do so until the user replies with either an ‘N’ or an ‘n’
Reserve Point Opportunity!!
Complete the above program calling a fourth function that will calculate and return the BMI.
The below program calculates BMI of a person after taking user's height and weight as input. It contains four functions, the first three functions (i.e. Function1, 2 and 3) uses for, do-while and infinite while loop to perform a same specific task (i.e. takes users input and provide BMI as output). And the last Function4 calculates the BMI after taking weight and height as argument from Function1, 2 and 3 and then finally returns the BMI to the respective functions.
Source Code -
using System.IO;
using System;
class BMI
{
static void Main()
{
double weight=0;
double height=0;
Function1(weight,height);
Function2(weight,height);
Function3(weight,height);
}
static void Function1(double w, double h)
{
Console.WriteLine(" Inside Function 1 : ");
for(int i=1; i<4; ++i)
{
Console.WriteLine("For person "+ i +" :");
Console.WriteLine("Enter weight in pounds :");
w=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter height in inches :");
h=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("BMI :" + Function4(w,h));
Console.WriteLine();
}
}
static void Function2(double w, double h)
{
Console.WriteLine(" Inside Function 2 : ");
int i=1;
do
{
Console.WriteLine("For person "+ i +" :");
Console.WriteLine("Enter weight in pounds :");
w=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter height in inches :");
h=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("BMI :" + Function4(w,h));
Console.WriteLine();
++i;
}
while(i<4);
}
static void Function3(double w, double h)
{
Console.WriteLine(" Inside Function 3 : ");
int i=1;
while(true)
{
Console.WriteLine("For person "+ i +" :");
Console.WriteLine("Enter weight in pounds :");
w=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter height in inches :");
h=Convert.ToDouble(Console.ReadLine());
Console.WriteLine("BMI :" + Function4(w,h));
Console.WriteLine();
++i;
Console.WriteLine("Enter n or N to exit or else press any other
character to continue : ");
char[] ch=Console.ReadLine().ToCharArray();
if(ch[0]=='n' || ch[0]=='N')
{
break;
}
Console.WriteLine();
}
}
static double Function4(double w, double h)
{
double BMI=(w*703)/(h*h);
return BMI;
}
}