Home Contents


/* Program: unitcon1.c */
/* Converts user input from one set of units of weight to another. */
/* Francis O'Donovan 21-11-97*/

#include <stdio.h>

#define LBS_LBS (1)
#define LBS_GRAMS (453.51473)
#define LBS_KILOGRAMS (0.45351473)
#define GRAMS_LBS (0.002205)
#define GRAMS_GRAMS (1)
#define GRAMS_KILOGRAMS (0.001)
#define KILOGRAMS_LBS (2.205)
#define KILOGRAMS_GRAMS (1000)
#define KILOGRAMS_KILOGRAMS (1)

main()
{
        /* Define variables */

                float weight;
                char unit, convert;

        /* Intro */
 
                printf( "Program: unitcon1.c\n\n" );
                printf( "Converts user input from one set of units of weight to another.\n\n" );
                printf( "Francis O'Donovan 21-11-97\n\n\n" );

        /* Ask user for a weight */

                printf( "Please type in the weight (e.g. 7)\n" );
                printf( "Weight: " );
                scanf( "%f", &weight );

        /* Ask user which unit weight is in */

                printf( "\nPlease type in the unit of weight.\n" );
                printf( "For lbs, type l, for grams, type g, for kilograms, type k.\n" );
                printf( "Unit:" );
                scanf( "%c", &unit );

        /* Ask user which unit to convert to */

                printf( "\nPlease type in the unit of weight to convert to.\n" );
                printf( "Convert to unit:" );
                scanf( "%c", &convert );
                printf( "\n" );

        /* Convert weight and print result */
 
                if( unit == 'l' && convert == 'l' )
                        printf( "%.2f lbs is %.2f lbs.\n\n", weight, weight*LBS_LBS );
                else if ( unit == 'l' && convert == 'g' )
                        printf( "%.2f lbs is %.2f grams.\n\n", weight, weight*LBS_GRAMS );
                else if ( unit == 'l' && convert == 'k' )
                        printf( "%.2f lbs is %.2f kilograms.\n\n", weight, weight*LBS_KILOGRAMS );
                else if( unit == 'g' && convert == 'l' )
                        printf( "%.2f grams is %.2f lbs.\n\n", weight, weight*GRAMS_LBS );
                else if ( unit == 'g' && convert == 'g' )
                        printf( "%.2f grams is %.2f grams.\n\n", weight, weight*GRAMS_GRAMS );
                else if ( unit == 'g' && convert == 'k' )
                        printf( "%.2f grams is %.2f kilograms.\n\n", weight, weight*GRAMS_KILOGRAMS );
                else if( unit == 'k' && convert == 'l' )
                        printf( "%.2f kilograms is %.2f lbs.\n\n", weight, weight*KILOGRAMS_LBS );
                else if ( unit == 'k' && convert == 'g' )
                        printf( "%.2f kilograms is %.2f grams.\n\n", weight, weight*KILOGRAMS_GRAMS );
                else if ( unit == 'k' && convert == 'k' )
                        printf( "%.2f kilograms is %.2f kilograms.\n\n", weight, weight*KILOGRAMS_KILOGRAMS );
                else
                        printf( "Something is wrong." );
}


Home Contents



© Francis O'Donovan 1999.