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  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