Convert Fahrenheit temperature to Celsius equivalent and the Kelvin equivalent - C Operator

C examples for Operator:Arithmetic Operator

Introduction

Here is the formula for converting Fahrenheit to Celsius:

Celsius = 5.0 / 9.0 * (Fahrenheit - 32.0)

Here is the formula for converting Celsius to Kelvin:

Kelvin = Celsius + 273.16

Demo Code

#include <stdio.h>

void Temperatures(double fahr); // prototype declaration of Temperatures

int main(void)
{
  double fahr;/*  w  w w.  j a va2 s .c om*/
  printf("This program converts fahrenheit to celsius and kelvin.\n");
  printf("Enter a temperature in degrees fahrenheit (q to quit): ");
  while (scanf("%lf", &fahr) == 1) // continue executing loop if user enters valid number
  {
    Temperatures(fahr); // convert fahr to celsius and kelvin
    printf("Enter a temperature in degrees fahrenheit (q to quit): ");
  }

  printf("bye\n");
}

void Temperatures(double fahr)
{
  const double FAHR_TO_CEL_SCALE = 5.0 / 9.0;
  const double FAHR_TO_CEL_OFFSET = -32.0;
  const double CEL_TO_KEL_OFFSET = 273.16;

  double celsius = (fahr + FAHR_TO_CEL_OFFSET) * FAHR_TO_CEL_SCALE;
  double kelvin = celsius + CEL_TO_KEL_OFFSET;

  printf("%.2f degrees fahrenheit is %.2f degrees celsius or %.2f degrees kelvin.\n",
      fahr, celsius, kelvin);
}

Result


Related Tutorials