From 7d03f56fe9a8cfa75c7bb3cb4729ab7beaa377a7 Mon Sep 17 00:00:00 2001 From: Sandro Welte Date: Thu, 8 Feb 2024 17:38:22 +0100 Subject: [PATCH] added Celsius to Fahrenheit converter --- src/convert_C_to_F.c | 19 +++++++++++++++++++ src/convert_C_to_F.h | 7 +++++++ 2 files changed, 26 insertions(+) create mode 100644 src/convert_C_to_F.c create mode 100644 src/convert_C_to_F.h diff --git a/src/convert_C_to_F.c b/src/convert_C_to_F.c new file mode 100644 index 0000000..ddbdd3e --- /dev/null +++ b/src/convert_C_to_F.c @@ -0,0 +1,19 @@ +#include "stdio.h" +#include "convert_C_to_F.h" + +float convert_temperature(float value, char from_unit, char to_unit) { + float result; + + if (from_unit == 'c' && to_unit == 'f') { + result = (value * 9 / 5) + 32; // Celsius to Fahrenheit + } else if (from_unit == 'f' && to_unit == 'c') { + result = (value - 32) * 5 / 9; // Fahrenheit to Celsius + } else { + printf("Invalid units or conversion not supported.\n"); + result = -1; // Error code + } + + return result; +} + + diff --git a/src/convert_C_to_F.h b/src/convert_C_to_F.h new file mode 100644 index 0000000..3981936 --- /dev/null +++ b/src/convert_C_to_F.h @@ -0,0 +1,7 @@ + +#ifndef THEADMIRALS_CONVERT_C_TO_F_H +#define THEADMIRALS_CONVERT_C_TO_F_H + +float convert_temperature(float value, char from_unit, char to_unit); + +#endif //THEADMIRALS_CONVERT_C_TO_F_H