Skip to content

Instantly share code, notes, and snippets.

# CraigRodrigues/degree.c Last active Jul 8, 2016

[2016-06-27] Challenge #273 [Easy] Getting a degree (with bonus)
 /** * degree.c * * Reddit Daily Programmer Challenge #273 [EASY] * Getting a degree * * https://www.reddit.com/r/dailyprogrammer/comments/4q35ip/20160627_challenge_273_easy_getting_a_degree/ * */ #include #include #include #include #include #define pi 3.141592 char units[2]; float num; float conversion; float convert(float* numbers, char* conversion_units); int main(void) { while (scanf("%f%s", &num, units) == 2) { float answer = convert(&num, units); if (answer != -1) printf("%2g%c\n", answer, units[1]); } return 0; } float convert(float* numbers, char* conversion_units) { if (!strcmp(units,"dr")) // degrees to radians { return *numbers * (pi / 180.0); } else if (!strcmp(units,"rd")) // radians to degrees { return *numbers * (180.0 / pi); } else if (!strcmp(units,"cf")) // celsius to farenheit { return *numbers * 1.8 + 32; } else if (!strcmp(units,"fc")) // farenheit to celsius { return (*numbers - 32) * 0.5556; } else if (!strcmp(units,"ck")) // celsius to kelvin { return *numbers + 273.15; } else if (!strcmp(units,"kc")) // kelvin to celsius { return *numbers - 273.15; } else if (!strcmp(units,"fk")) // farenheit to kelvin { return ((convert(numbers, "fc")) + 273.15); } else if (!strcmp(units,"kf")) // kelvin to farenheit { return *numbers * 1.8 - 459.67; } else printf("No candidate for conversion\n"); return -1; return 0; }
Owner Author

### CraigRodrigues commented Jul 8, 2016

 "fk" conversion recursive call doesn't work. Infinite loop.
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.