Temperature Converter in C

#include <stdio.h>
#include <stdlib.h>

void printUsage();
float toCelsius(float fahrenheit);
float toFahrenheit(float celsius);


int main(int argc, char ** argv){
  float value = 0.0f;
  if(argc < 3 || argc > 3){
    printUsage();
    return -1;
  }
  switch(argv[1][0]){
    case 'c':
    value = toCelsius(atof(argv[2]));
    printf("%s Degrees Fahrenheit is equal to %f Degrees Celsius\n",argv[2],value);
    break;
    case 'f':
    value = toFahrenheit(atof(argv[2]));
    printf("%s Degrees Celsius is equal to %f Degrees Fahrenheit\n",argv[2],value);
    break;
    default:
    printUsage();
    return -1;
    break;
  }
  return 0;
}

void printUsage(){
  printf("Usage: temp [c/f] value\n");
}

float toCelsius(float fahrenheit){
  return (fahrenheit -32) * (5.0f/9.0f);
}
float toFahrenheit(float celsius){
  return (celsius) * (9.0f/5.0f) + 32;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s