Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Mathematical

Silver Coder
Simply because I was bored and because I felt like it, I have converted @Master Yoda's code into C. If you prefer C mainly due to how lightweight, fast, and cleaner it is, then you can use this version that I've written. The major difference in this one, is that it uses the metric system instead of the imperial system. Here's the code:

C:
/*                                                                                                                      
 * BMI Calculator                                                                                                       
 * Originally written by Master Yoda on Codeforum.org                                                                   
 * Converted to C by Mathematical on Codeforum.org                                                                      
*/                                                                                                                      
                                                                                                                        
                                                                                                                        
#include <stdio.h>                                                                                                      
                                                                                                                        
int main(void) {                                                                                                        
     float Height, BMI;                                                                                                 
                                                                                                                        
     int Weight;                                                                                                        
                                                                                                                        
                                                                                                                        
     printf("Welcome to the BMI calculator.\n");                                                                        
                                                                                                                        
     // Ask the user to enter both their weight and height.                                                             
     printf("Please enter your weight(kg): ");                                                                          
     scanf("%d", &Weight);                                                                                              
     printf("Please enter your height(m): ");                                                                           
     scanf("%f", &Height);                                                                                              
                                                                                                                        
     printf("Weight: %d kg\n", Weight);                                                                                 
     printf("Height: %f m\n", Height);                                                                                  
                                                                                                                        
     // Perform the calculations and print off the result.                                                              
     BMI = Weight / (Height * Height);                                                                                  
     printf("BMI: %f\n", BMI);                                                                                          
                                                                                                                        
     // Determine to see if the user's BMI matches up with a certain class.                                             
     if(BMI <= 18.5)                                                                                                    
          printf("Underweight\n");                                                                                      
     else if(BMI <= 24.9)                                                                                               
          printf("Normal\n");                                                                                           
     else if(BMI <= 29.9)                                                                                               
          printf("Overweight\n");                                                                                       
     else                                                                                                               
          printf("Obese\n");                                                                                            
                                                                                                                        
     return 0;                                                                                                          
}

The only differences in this version, is that of course, it's written in C, so there is a change in the syntax. Not only that though, I have commented the code so that it can be understood(So if you lose this post but still have the code, you can refer to these comments to learn how the code works). Along with that, I have also simply cleaned the code up, as I've spaced things out and have removed the braces that are generally unnecessary in the if-else if-statement.

If at any point you want to modify the C version to use the imperial system, refer to these sites:

@Master Yoda's original copy that was written in C++ can be found here: https://www.codeforum.org/threads/how-i-built-my-bmi-calculator-in-c.809/
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom