I have no idea why my BMI value is always = to 0. I am programming noob what am i missing? other than that is my if statement alright? what am i missing?
static void Main(string[] args) {
double WeightKg = 0.0, HeightCm = 0.0, Weightlbs = 0.0, WeightOz = 0.0, BMI = 0.0, Feet = 0.0, Inches = 0.0;
int BMIOption;
string AnotherConversion;
string BMIMenu = ("Which Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(BMIMenu);
BMIOption = int.Parse(Console.ReadLine());
if (BMIOption == 1) {
Console.Write("\nPlease Enter your Weight in Kilogram (kg):");
WeightKg = int.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Height in in centimetres (cm):");
HeightCm = int.Parse(Console.ReadLine());
BMI = WeightKg / (HeightCm * HeightCm);
if (BMI >= 35) {
Console.WriteLine("\nYour BMI is {0:C},Severe Obesity", BMI);
} else if (BMI >= 30) {
Console.WriteLine("\nYour BMI is {0:C},Obese", BMI);
} else if (BMI >= 25) {
Console.WriteLine("\nYour BMI is {0:C},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},UnderWeight", BMI);
}//End if
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
Console.ReadKey();
BMI is calculated with meters, not centimeters. So you need to convert the HeightCm to HeightM. If you don't do this, you get really small number, that is then printed as 0.
double HeightM = HeightCm / 100.0;
BMI = WeightKg / (HeightM * HeightM);
Also, when parsing, use double.Parse instead of int.Parse. The way it is right now, you will only parse the number without the decimal part.
In this line:
BMI = WeightKg / (HeightCm * HeightCm);
The result ends up being very small (less than 1). Take an example where WeightKg is 55 and HeightCm is 165. The result is around 0.002.
When you display it as {0:C}, it's being displayed as zero. Use {0:G} to see the actual value.
even so your formula is wrong, as Martin says u must replace the int.Parse with double AND
BMI = WeightKg / ((HeightCm/100) * (HeightCm/100));
use this for calculation it is HEIGHT IN METERS :)
Related
I've been working on a two class BMI Calculator for my class. I finally have managed to debug on get the program to execute. However I noticed that it wont let me enter height or weight as a decimal.
Example
putting 6.4 for height causes it to crash
Putting 6 height and 220 weight always seems to declare underweight and show incorrect bmi reading
example:
Height 6
Weight 220
BMI: score $0.09
It's calculating the BMI wrong. Looking at the code I can't seem to identify why its calculating this wrong.
-Edit- Managed to get it to take decimals! (Thank you everyone!) However it still seems to be messing up calculating the BMI
-Edit- Code Works!
(Code Part 1) Updated Mk2
using System;
namespace Calculator
{
public class BMICalc
{
private const
decimal REQ = 703;
private decimal weightPerson;
private decimal heightPerson;
public BMICalc()
{
}
public BMICalc(decimal weiP, decimal heiP)
{
this.weightPerson = weiP;
this.heightPerson = heiP;
}
public decimal SetBmi()
{
decimal bmi;
bmi = Convert.ToDecimal((this.weightPerson * 0.45m) / ((this.heightPerson * 12 * 0.025m) * (this.heightPerson * 12 * 0.025m)));
if (bmi < 18.5m)
{
Console.WriteLine("UnderWeight");
Console.ReadLine();
}
if (bmi > 18.5m && bmi < 25.0m)
{
Console.WriteLine("Normal");
Console.ReadLine();
}
if (bmi > 25.0m && bmi < 29.9m)
{
Console.WriteLine("OverWeight");
Console.ReadLine();
}
if (bmi > 29.9m && bmi < 40.0m)
{
Console.WriteLine("Obese");
Console.ReadLine();
}
return bmi;
}
public override string ToString()
{
return "\tCalculator" +
"\n\n Weight:" + this.weightPerson +
"\n\n Height:" + this.heightPerson +
"\n\n BMI Score:" + SetBmi().ToString();
}
}
}
Code (Part 2) Updated Mk 2
namespace Calculator
{
public class App
{
public static void Main() //
{
decimal heiP, weiP;
heiP = InputHeight();
weiP = InputWeight();
BMICalc bmiCal = new BMICalc(weiP, heiP);
Console.Clear();
Console.WriteLine(bmiCal.ToString());
Console.ReadKey();
}
public static decimal InputHeight()
{
decimal hNumber;
Console.Write("Please enter your height: ");
hNumber = Convert.ToDecimal(Console.ReadLine());
return hNumber;
}
public static decimal InputWeight()
{
Decimal wNumber;
Console.Write("Please enter your weight: ");
wNumber = Convert.ToDecimal(Console.ReadLine());
return wNumber;
}
}
}
You're input parameters into the constructor expects weight (weiP), height (heiP) (in that order) while you're passing height, weight from BMICalc bmiCal = new BMICalc(heiP, weiP);
That's the reason the value being output is incorrect. Interchange the parameters and it should be fine.
EDIT
Since you're entering the weight in Lbs and height in feet, you would need to convert feet into inches ( multiplying by 12) and then also multiplying the height and weight by the conversion metric.
So the formula would be:
bmi = Convert.ToDecimal((this.weightPerson * 0.45) / ((this.heightPerson * 12 * 0.025) * (this.heightPerson * 12 * 0.025)));
The currency comes in when it uses the ToString() overridden method. The format specified there is for currency.
This question already has answers here:
Division returns zero
(8 answers)
Closed 7 years ago.
I am starting to learn C# and here is a program that calculate percentage
using System;
namespace CheckDegree
{
class Program
{
static void Main(string[] args)
{
int totalmarks=0;
for (int i = 0; i < 5; i++ ) {
Console.WriteLine("Please enter a subject.");
string subject = Console.ReadLine();
Console.WriteLine("Enter the mark for {0}",subject);
int mark = Convert.ToInt32(Console.ReadLine());
totalmarks = totalmarks + mark;
}
double percentage = (totalmarks / 500) * 100;
if (percentage >= 60)
{
Console.WriteLine("You got {0} and has been awarded a first division", percentage );
}
else if (percentage >= 50 && percentage <= 59)
{
Console.WriteLine("You got {0} and has been awarded a second division", percentage);
}
else if (percentage < 40 )
{
Console.WriteLine("You got {0} and thus have failed!!!", percentage);
}
Console.ReadKey();
}
}
}
However percentage always output 0
double percentage = (totalmarks / 500) * 100;
Instead of dividing by 500, divide by 500.0. It's treating it as an integer divided by an integer, which will be 0.#####. Since it's an integer, the decimal gets dropped. Then, 0 times 100 is still 0.
Changing 500 to 500.0 will force the division to be a double, which will preserve your decimal value.
static void Main(string[] args)
{
int choice;
choice = 0;
double length;
length = 0.00;
double height;
height = 0.00; //initiating variables
double base1;
base1 = 0.00;
double width;
width = 0.00;
double radius;
radius = 0.00;
double total;
total = 0.00;
Console.WriteLine("What shape would you like to make?");
Console.WriteLine("Please select one option (1, 2, 3, or 4)");
Console.WriteLine("1. Square.");
Console.WriteLine("2. Triangle."); //menu on the console
Console.WriteLine("3. Rectangle.");
Console.WriteLine("4. Circle.");
choice = int.Parse(Console.ReadLine());
if (choice == 1)
{
Console.WriteLine("Great! You chose Square.");
Console.WriteLine("Let's help you calculate the area of a square.");
Console.WriteLine("But first, we need to know the length of this square");
Console.WriteLine("Enter the length(Integer, or Decimal value is just fine:");
length = double.Parse(Console.ReadLine());
}
else if (choice == 2)
{
Console.WriteLine("Great! You chose Triangle.");
Console.WriteLine("Let's help you calculate the area of a triangle.");
Console.WriteLine("But first, we need to know the base and height of this triangle.");
Console.WriteLine("Enter the base(Integer, or Decimal value is just fine):");
base1 = double.Parse(Console.ReadLine()); //writing to the console depending on what choice
Console.WriteLine("Enter the height(Integer, or Decimal value is just fine):");
height = double.Parse(Console.ReadLine());
}
else if (choice == 3)
{
Console.WriteLine("Great! You chose Rectangle.");
Console.WriteLine("Let's help you calculate the area of a rectangle.");
Console.WriteLine("But first, we need to know the width and height of this rectangle.");
Console.WriteLine("Enter the width(Integer, or Decimal value is just fine):");
width = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the height(Integer, or Decimal value is just fine):");
height = double.Parse(Console.ReadLine());
}
else if (choice == 4)
{
Console.WriteLine("Great! You chose Circle.");
Console.WriteLine("Let's help you calculate the area of a circle.");
Console.WriteLine("But first, we need to know the radius of this circle.");
Console.WriteLine("Enter the radius(Integer, or Decimal value is just fine:");
radius = double.Parse(Console.ReadLine());
}
Main_Calculations(args, total, base1, height, width, choice); //Sending Paramters to the next method
}
static void Main_Calculations(string[] args, double base1, double height, double width, double total, int choice) //bringing in parameters
{
if(choice == 2)
{
total = (base1 * height) * 0.5; //Calculations for triangle
Console.WriteLine("The area of a triangle is: " + total);
}
else if(choice == 3)
{
total = width * height; //calculations for rectangle
Console.WriteLine("The area of a rectangle is: " + total);
}
Main_Calculations(choice, total); //Sending Paramters to the next method
}
static void Main_Calculations(double length, double radius, double total, double choice)
{
if(choice == 1)
{
total = length * length; //Calculations for square
Console.WriteLine("The area of a square is: " + total);
}
else if(choice == 4)
{
total = (radius * radius) * 3.14; //calculations for circle
Console.WriteLine("The area of a circle is " + total); //prints the answer to the console
}
}
}
}
My 2nd sender for (Math_Calculations) is giving me an error for no overload takes 2 arguments, I don't understand why it won't work the way I have it. Seems possibly I am just missing one phrase, I have stepped in and out of the code looking to now avail.
When you call the 2nd method Math_Calculations, you only give 2 arguments to the method when you ask in the method for 4 arguments
So, What you need to do is or, you give 2 arguments extra to the 2dn Math_Calculations call, OR you remove 2 arguments from the method.
This would be easier to answer with line numbers, but you call
Main_Calculations(choice,total);
when there is no method for Main_Calculations that takes two arguments.Just as Habib has pointed out.
In my if statement (LengthCalculatorOption == 1), I want to convert, for example, 187.96cm to feet and inches, such as 6feet 2ins. How do I do that? In my current code, it shows 6.17feet and always 0ins. I have no idea why.
static void Main(string[] args) {
double Centimetres = 0.0, Feet = 0.0, Inches = 0.0;
string AnotherConversion = null;
string LengthCalculatorMenu;
int LengthCalculatorOption;
do {
LengthCalculatorMenu = ("Enter 1) Convert centimetres to feet and inches:"
+ "\nEnter 2) Convert feet and inches to centimetres:");
Console.Write(LengthCalculatorMenu);
LengthCalculatorOption = int.Parse(Console.ReadLine());
if (LengthCalculatorOption == 1) {
Console.WriteLine("Please Enter the Centimetres(cm) that you wish to convert to feet and inches");
Centimetres = double.Parse(Console.ReadLine());
Feet = (Centimetres / 2.54) / 12;
Inches = (Centimetres / 2.54) - (Feet * 12);
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in feet and inches is {0:C} ft {1:G} ins", Feet, Inches);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else if (LengthCalculatorOption == 2) {
Console.WriteLine("Please Enter the Feet");
Feet = double.Parse(Console.ReadLine());
Console.WriteLine("Please Enter the Inches");
Inches = double.Parse(Console.ReadLine());
Centimetres = ((Feet * 12) + Inches) * 2.54;
Console.WriteLine("\nThe equivalent in centimetres is {0:G}cm", Centimetres);
Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");
AnotherConversion = Console.ReadLine();
} else {
Console.WriteLine("\n\a\t Invalid Option!Option Must be 1 or 2");
}
} while (AnotherConversion == "y" || AnotherConversion == "Y");
Try this:
Feet = (Centimetres / 2.54) / 12;
int iFeet = (int)Feet;
inches = (Feet - (double)iFeet) * 12;
To elaborate a bit:
You are defining feet as a double, which means that it will be a decimal value. So since you're dividing by 12, it can become a decimal representation.
What my code does is it converts Feet to integer (which will round it to 6 in this situation). We then subtract the double version of Feet (6.17 in this situation) which equals .17 (The remainder). We multiply that by 12 to convert from .17 feet to inches
Edit
To expand based on Scott's comment, this would be a different way to go
int totalInches = (Centimetres / 2.54); // This will take a floor function of Centimetres/2.54
int Feet = (totalInches - totalInches % 12) / 12; // This will make it divisible by 12
int inches = totalInches % 12; // This will give you the remainder after you divide by 12
To calculate a value in centimeters into feet and inches, you'd likely want to do this:
double centimeters = 187.96;
double inches = centimeters/2.54;
double feet = Math.Floor(inches / 12);
inches -= (feet*12);
Generally-speaking, one should convert down to the most basic level, then calculate your way up. This way, you only do the work of converting one time, instead of having to repeat the conversion calculation. In this insntance, I do the simple conversion of centimeters to inches, and then after that, count the number of feet in inches, then subtract that much from the final inches value.
So, if I had, say, 38 inches, I would have Math.Floor(38 / 12) feet, or 3. Then inches would be set to 38 - (3*12), which is 2, giving a final result of 3 feet, 2 inches.
Keeping it as double, use:
double inp = 12.75; // e.g.
double feet = Math.Floor(inp);
double inches = (inp - feet) * 12.0;
Try this:
double F = Math.Floor(Centimetres * 0.0328084);
Feet = Centimetres * 0.0328084;
Inches = (Feet - F) * 12;
1 ft = 0.30480m
Some general-purpose methods:
public static class Converter
{
public static (int Feet, double Inches) CentimetresToFeetInches(double centimetres)
{
var feet = centimetres / 2.54 / 12;
var iFeet = (int)feet;
var inches = (feet - iFeet) * 12;
return (iFeet, inches);
}
public static string CentimetresToFeetInchesString(double centimetres, string footSymbol = " foot", string inchesSymbol = " inches")
{
(var feet, var inches) = CentimetresToFeetInches(centimetres);
return $"{feet:N0}{footSymbol}, {inches:N0}{inchesSymbol}";
}
}
Usage:
(var feet, var inches) = Converter.CentimetresToFeetInches(178);
//feet == 5
//inches == 10.078740157480315
var feetInchesString = Converter.CentimetresToFeetInchesString(178);
//5 foot, 10 inches
I just started programming a week ago and my first assignment was to code a BMI calculator.
It is supposed to look like this when launched:
BMI Calculator
Your weight in kg: x
Your height in cm: x
Gender (m/f): x
-> You are underweight/normal/overweight
Here is my code so far:
Console.WriteLine("BMI Calculator");
Console.WriteLine("===========");
Console.WriteLine();
Console.Write("Weight in kg: ");
int kg;
kg = Convert.ToInt32(Console.ReadLine());
Console.Write("Height in cm: ");
int m;
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Gender (m/f):");
string Geschlecht = Console.ReadLine();
int BMI;
BMI = kg / (m / 100) * (m / 100);
if (BMI < 19 & Gender == "f")
{ Console.WriteLine("-> Underweight"); }
if (BMI >= 19 & BMI <= 24 & Gender == "f")
{ Console.WriteLine("-> Normal"); }
if (BMI > 24 & Geschlecht == "f")
{ Console.WriteLine("-> Overweight"); }
if (BMI < 20 & Gender == "m")
{ Console.WriteLine("-> Underweight"); }
if (BMI >= 20 & BMI <= 25 & Gender == "m")
{ Console.WriteLine("-> Normal"); }
if (BMI > 25 & Gendert == "m")
{ Console.WriteLine("-> Overweight"); }
Console.ReadLine();
I'm not sure what's wrong with my code but whenever I enter 60kgs, 170cm and male, I get overweight, even though I should get normal. Same thing with anything above 10kgs actually.
PS: I'm really a beginner at programming so I apologize for my command of the programming lingo.
And for your convenience:
http://i.stack.imgur.com/admqr.png
Thanks in advance!
When you do:
BMI = kg / (m / 100) * (m / 100);
And m is an int, you'd be doing integer division, in which case 170 / 100 = 1. And as User1551892 pointed out, you'll need to be a bit more specific about the order of calculations.
Try:
double BMI = kg / ( ( m / 100.0 ) * ( m / 100.0 ) );
That will force it to do floating point division and should get you better results.
Also, you could use Math.Pow to avoid having m / 100.0 twice:
double BMI = kg / Math.Pow( m / 100.0, 2 );
Please check this line:
BMI = kg / (m / 100) * (m / 100);
it should be like this:
BMI = kg / ((m / 100) * (m / 100)) ;
FYI new stack's exchange portal has opened : codereview.stackexchange.com
It should be better place to ask for review
the calculation is off though for BMI. it is (weight/height)/height
so
int bmi = (kg/(m/100))/(m/100)
Check Your gender's variable in each IF statement...