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


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.


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.


#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//  w w  w  .j  a v a 2 s .c o m
    printf("That is equivalent to %.2lf degrees Centigrade\n", 
               (temperature - 32.0)*5.0/9.0);
  return 0;