Convert Fahrenheit to Celsius

bumsfeld 1 Tallied Votes 277 Views Share

A simple program to show you how to create a table of Fahrenheit and Celsius values using a for loop. You may be able to learn from this code.

/* create a table of Fahrenheit/Celcius values */

#include <stdio.h>

int main()
{
  int fahr;

  printf( "Fahr\t Celsius\n" );
  for ( fahr = 300; fahr >= 0; fahr = fahr - 10 )
  {
    printf( "%3d \t%6.1f\n", fahr, (5.0/9.0) * (fahr-32.0) );
  }

  getchar();  /* wait for key */
  return 0;
}
fozis 0 Newbie Poster

The formula Fahrenheit to Celsius is: C = (F - 32)* 5/9

#include <stdio.h>
int main()
{
  float fahrenheit,celsius;

  printf("Give temperature in fahrenheit:");
  scanf("%f",&fahrenheit);

  celsius = (fahrenheit - 32)* 5/9;


  // one decimal
  printf( "%.1f fahrenheit = %.1f celsius", fahrenheit, celsius);

  getchar();  /* wait for key */
  getchar();
  return 0;
}
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

@fozis
Bumsfeld's printf() expression is also correct - (5.0/9.0) * (fahr-32.0). You can reverse the expressions and still have the same result. IE. (5.0/9.0) * (fahr-32.0) == (fahr-32.0) * (5.0/9.0). To some, this is orthogonality - the order of expression is irrelevant. Now, I wait for someone more mathematically knowlegeable to tell me how full of it I am... :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.