Programming Fundamentals/Conditions/C: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 23:53, 1 February 2017

conditions.c

// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given 
// temperature and displays the result.

#include "stdio.h"

void to_celsius();
void to_fahrenheit();

int main(void) 
{
    char choice;

    printf("Enter F to convert to Fahrenheit or C to convert to Celsius:");
    scanf("%c", &choice);

    if(choice == 'C' || choice == 'c')
    {
        to_celsius();
    }
    else if(choice == 'F' || choice == 'f')
    {       
        to_fahrenheit();
    }
    else
    {
        printf("You must enter C to convert to Celsius or F to convert to Fahrenheit!");
    }

    return 0;
}

void to_celsius()
{
    float f;
    float c;

    printf("Enter Fahrenheit temperature:");
    scanf("%f", &f);
    c = (f - 32) * 5 / 9;
    printf("%f° Fahrenheit is %f° Celsius", f, c);
}

void to_fahrenheit()
{
    float c;
    float f;

    printf("Enter Celsius temperature:");
    scanf("%f", &c);
    f = c * 9 / 5 + 32;
    printf("%f° Celsius is %f° Fahrenheit", c, f);
}

Try It

Copy

See Also