Temperature Converter
C
Medium
5 views
Problem Description
Take temperature and unit (C/F) input from user. Handle case-insensitive (c, C, f, F). If invalid unit is there then show error and re-input mango. Convert and show result upto 2 decimal places.
Official Solution
#include <stdio.h>
int main() {
float temp, converted;
char unit;
printf("Enter temperature value: ");
scanf("%f", &temp);
while (1) {
printf("Enter unit (C/F): ");
scanf(" %c", &unit);
if (unit == 'C' || unit == 'c') {
converted = (temp * 9 / 5) + 32;
printf("Temperature in Fahrenheit: %.2f Fn", converted);
break;
}
else if (unit == 'F' || unit == 'f') {
converted = (temp - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f Cn", converted);
break;
}
else {
printf("Invalid unit! Please enter C or F.n");
}
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!