Introduction
In this lab, you will learn how to write a C program to convert temperature in Celsius to Fahrenheit.
In this lab, you will learn how to write a C program to convert temperature in Celsius to Fahrenheit.
In the first step, you need to declare the variables to store the temperature values. In this program, we will use two variables to store the temperature values: celsius
and fahrenheit
.
float celsius, fahrenheit; //declaring two variables to store temperature values
In the second step, take input temperature value from the user. Using scanf
function, you can get input from user.
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius); //getting temperature input from user
In the third step, apply formula to convert Celsius to Fahrenheit. For this, we use the formula: (1.8 * celsius) + 32
.
fahrenheit = (1.8 * celsius) + 32; //converting temperature from Celsius to Fahrenheit
In the fourth step, display the Fahrenheit value using printf
function.
printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); //displaying temperature in fahrenheit
Here is the full code for the program you have written. Copy the code and paste it in the main.c file.
#include<stdio.h>
int main ()
{
float celsius, fahrenheit; //declaring two variables to store temperature values
printf("Enter temperature in Celsius: ");
scanf("%f",&celsius); //getting temperature input from user
fahrenheit = (1.8 * celsius) + 32; //converting temperature from Celsius to Fahrenheit
printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); //displaying temperature in fahrenheit
return 0;
}
You have successfully learned how to write a C program to convert temperature in Celsius to Fahrenheit. This program will help you in future projects where you need to convert temperature units.