C - Write program to convert celsius to fahrenheit back and forth

Requirements

Allow a user to choose one of two options:

  • Convert a temperature from degrees Celsius to degrees Fahrenheit.
  • Convert a temperature from degrees Fahrenheit to degrees Celsius.

Then read the temperature value and output the new value that results from the conversion.

Hint

To convert from Celsius to Fahrenheit you can multiply the value by 1.8 and then add 32.

To convert from Fahrenheit to Celsius, you can subtract 32 from the value, then multiply by 5, and divide the result by 9.

Demo

#include <stdio.h>

int main(void)
{
  int choice = 0;
  double temperature = 0.0;
  printf("1. Convert from Centigrade to Fahrenheit\n"
         "2. Convert from Fahrenheit to Centigrade\n"
         "Select the conversion (1 or 2): ");
  scanf("%d", &choice);

  printf("Enter a temperature in degrees %s: ", 
       (choice == 1 ?  "Centigrade" : "Fahrenheit"));
  scanf("%lf", &temperature);

  if(choice == 1)
    printf("That is equivalent to %.2lf degrees Fahrenheit\n", 
               temperature*9.0/5.0 + 32.0);
  else/*from  ww w . ja v a 2s .  c  o m*/
    printf("That is equivalent to %.2lf degrees Centigrade\n", 
               (temperature - 32.0)*5.0/9.0);
  return 0;
}

Result