c to fahrenheit
To convert a temperature from Celsius to Fahrenheit in the C programming language, you can use the following formula:
Fahrenheit = (Celsius * 9/5) + 32
Here's an example code snippet that demonstrates this conversion:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
Let's break down the code and explain each step:
We include the
<stdio.h>
header file, which provides input/output functions likeprintf
andscanf
.In the
main
function, we declare two variables:celsius
to store the temperature in Celsius, andfahrenheit
to store the converted temperature in Fahrenheit.We prompt the user to enter the temperature in Celsius using the
printf
function.We read the input temperature from the user using the
scanf
function and store it in thecelsius
variable.We perform the conversion using the formula
(celsius * 9/5) + 32
and store the result in thefahrenheit
variable.Finally, we use the
printf
function to display the converted temperature in Fahrenheit with two decimal places.
That's it! The code takes a temperature in Celsius as input, converts it to Fahrenheit, and displays the result.