[Tutor] small program in Python and in C++

Rob rob@uselesspython.com
Tue, 02 Jul 2002 15:44:31 -0500


This is a multi-part message in MIME format.
--------------060709020104040202020000
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Someone here found a formula in the newspaper to tell if you are obese, 
but she couldn't follow it (even worse math skills than my own, it 
seems), so I wrote her a Python program to run the calculation. I wound 
up writing a C++ equivalent for someone else a few minutes later (and he 
promises me he'll send me a PHP version tonight to return the favor).

I thought I'd pass it all along to the Tutor list in case it makes a 
good example for anyone (or if anyone cared to point out other ways to 
do it, etc.). If you know a little C++ and are looking into Python, or 
vice versa, you might like it on some level.

Rob
http://uselesspython.com
-- 
"Giving the Linus Torvalds Award to the Free Software Foundation is a 
bit like giving the Han Solo Award to the Rebel Alliance."
--Richard Stallman at the 1999 LinuxWorld show

--------------060709020104040202020000
Content-Type: text/plain;
 name="obesity.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="obesity.py"

# fetch height and weight from user
height = float(raw_input("\nWhat is your height in inches? "))
weight = float(raw_input("\nWhat is your weight in pounds? "))

# calculate body mass index
bodyMassIndex = 703 * ( weight / ( height * height ) )

# print obesity judgement
print "\nYour Body Mass Index is " + str(bodyMassIndex)
if bodyMassIndex >= 30:
    print "\nYou are obese.\n"
else:
    print "\nYou are not obese.\n"

--------------060709020104040202020000
Content-Type: text/plain;
 name="obesity.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="obesity.cpp"

#include <iostream>
using namespace std;

int main()
{
    // fetch data from the user
    cout << endl << "What is your height in inches? ";
    float height;
    cin >> height;
    cout << endl << "What is your weight in pounds? ";
    float weight;
    cin >> weight;
    
    // calculate body mass index
    float bodyMassIndex;
    bodyMassIndex = 703 * ( weight / ( height * height ) );
    
    // display obesity judgement
    cout << "\nYour Body Mass Index is " << bodyMassIndex << endl;
    if (bodyMassIndex >= 30)
        cout << "\nYou are obese." << endl;
    else
        cout << "\nYou are not obese." << endl;
        
    return 0;
}

--------------060709020104040202020000--