- In this program, we declare two float variables
celsius
and fahrenheit
. We use printf()
and scanf()
to get the value of the temperature in Celsius from the user.
- We then convert the temperature in Celsius to Fahrenheit using the formula:
- Fahrenheit = Celsius * 9 / 5 + 32
- Finally, we print out the result using
printf()
. Note that we use the %.2f
format specifier to print out the floating point values of the temperature values. This limits the output to 2 decimal places.
#include <stdio.h>
int main() {
float celsius, fahrenheit;
// Get the temperature in Celsius from the user
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
// Print the result
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
OUTPUT
- Enter temperature in Celsius: 25 25.00
- Celsius = 77.00 Fahrenheit