0

I have this function as follows:

private int Calc_bmi(int userid, int wt, int ht) {
    int bmi = ((wt / ht) / ht) * 10000;
}

The values of variables wt and ht are correct. I also tried it with using float bmi and also using Convert.ToInt32 but it doesn't seem to work. It returns 0 every time. What am I doing wrong?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • int div int is int. Better cast to float before. Looks like a duplicate. – Uwe Keim Mar 01 '19 at 15:02
  • @UweKeim how do I convert it then? – Priya Verma Mar 01 '19 at 15:03
  • @UweKeim do I need to convert wt and ht to float ? – Priya Verma Mar 01 '19 at 15:04
  • What are your units? Integer division just gives you the whole number result (no remainder) of the division. For example, 5/3 results in 1. Divide it by 3 again and you get 0. Why are you using ints and not floats or decimals – Flydog57 Mar 01 '19 at 15:05
  • To convert them, do this: `(((double)wt/ht) /ht)`. If you divide a floating point number by an integer, you get a floating point number – Flydog57 Mar 01 '19 at 15:06
  • Before convert nothing, I would check why it returns 0 every time... I imagine wt=4 and ht=2, and of course, the result of your operation is not 0 (4 / 2 = 2 / 2 = 1 * 10000 = 10000 ) – Jortx Mar 01 '19 at 15:08

1 Answers1

0

You have to convert wt and ht to float:

class Program
  {
    static void Main(string[] args)
    {

      int bmi = Calc_bmi(12, 10, 20);
      Console.WriteLine(bmi);
      Console.ReadLine();
    }
    private static int Calc_bmi(int userid, int wt, int ht)
    {
      float fWt = (float)wt;
      float fHt = (float)ht;
      float bmi = ((fWt / fHt) / fHt) * 10000;
      return Convert.ToInt32(bmi);
    }
  }
Dave
  • 1,912
  • 4
  • 16
  • 34