i'm trying to create a program that calculates Body mass index in a for loop with 5kg gaps. BMI formula is: weight(kg)/height(meters squared) = BMI. How would this work? this is the code i got so far:

Console.Write("Write your height in meters: ");
double t1 = double.Parse(Console.ReadLine());
Console.Clear();

Console.Write("Write starting point of weight in kg: ");
double t2 = double.Parse(Console.ReadLine());
Console.Clear();

Console.Write("Write ending point of weight in kg: ");
double t3 = double.Parse(Console.ReadLine());
Console.Clear();

double i = t1;
double t1s = t1 * t1;
double BMI = t2 / t1s;

for (i = BMI; t2 <= t3; i++)

Console.WriteLine(i);
t2 += 5;
} Console.ReadKey();

i want the result to look somewhat like this:
BMI for height 1,6m is:
weight | BMI
60 | 23.44
65 | 25.39
70 | 27.34
...skipping to 125(ending point)
125 | 48.83

i'm really lost in how this can work, if anyone knows how this could work please answer.

Dani AI

Generated

a few things are tripping you up: you increment i (an index) but never update the weight you are basing BMI on, you do not recompute BMI each step, and your missing braces mean t2 += 5; runs outside the loop. Also, stick to meaningful names and precompute 1/(height*height) so you are not doing the same math repeatedly. is right about clearer names; I would also move the +5 step into the for header for readability. Finally, do not Console.Clear() between prompts or you will erase what the user just typed.

Here is a compact pattern you can drop in. It validates input, prints a simple table, and walks weights in 5 kg steps.

using System;
using System.Globalization;

double height;
int startKg, endKg;

Console.Write("Height (m): ");
if (!double.TryParse(Console.ReadLine(), NumberStyles.Float, CultureInfo.CurrentCulture, out height) || height <= 0)
{ Console.WriteLine("Enter a positive height in meters."); return; }

Console.Write("Start weight (kg): ");
if (!int.TryParse(Console.ReadLine(), out startKg)) { Console.WriteLine("Enter an integer weight."); return; }

Console.Write("End weight (kg): ");
if (!int.TryParse(Console.ReadLine(), out endKg) || endKg < startKg)
{ Console.WriteLine("End must be >= start."); return; }

const int stepKg = 5;
double invH2 = 1.0 / (height * height);

Console.WriteLine($"\nBMI for height {height:F2} m");
Console.WriteLine("weight | BMI");
for (int w = startKg; w <= endKg; w += stepKg)
{
    double bmi = w * invH2;
    Console.WriteLine($"{w,6} | {bmi,5:F2}");
}

Notes:

  • If your locale uses a comma for decimals (e.g., 1,60), TryParse with CurrentCulture will handle it.
  • In ASP.NET (tagged here), read values from controls and use TryParse the same way; the loop logic is identical.
  • if you are still stuck, post your inputs and expected output so we can check the math and formatting.

Recommended Answers

All 2 Replies

I recommend using some useful names so it is easier to follow your code. Use something like "height", "startWeight", "endWeight" instead of "t1", "t2", "t3".

I suggest that you use the following form of the for loop:

double startWeight = 60;
double endWeight = 125;

double weight = startWeight;

for (weight = startWeight; weight <= endWeight; )
{
    //rest of your code here


    //increment weight
    weight = weight + 5;
}//for

body mass index problem programing c#

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.