In this tutorial, you will learn how to write a program to convert temperature from Celsius to Fahrenheit using the if-else statement. This post is suitable for various topics like Convert Temperature From Celsius To Fahrenheit using if-else in C Programmings.
Problem Statement
The problem is to write a C program that will convert the given temperature in Celsius to Fahrenheit. The user will give input the temperature in celsius and then return the output in Fahrenheit temperature.
Example
Enter temperature in Celsius: 25
Output: 25.00 Celsius = 77.00 Fahrenheit
Algorithm
- Start the program.
- Declare variables
celsius
andfahrenheit
typefloat
. - Prompt the user to enter the temperature in Celsius.
- Read the input temperature from the user and store it in the
celsius
variable usingscanf()
. - Use an if-else statement to check if the entered temperature is below absolute zero (-273.15 Celsius). If true, print an error message stating that the temperature is invalid.
- If the entered temperature is valid (not below absolute zero), perform the temperature conversion from Celsius to Fahrenheit using the formula
(celsius * 9 / 5) + 32
and store the result in thefahrenheit
variable. - Print the original temperature in Celsius and the converted temperature in Fahrenheit using
printf()
. - End the program.
C Program to Convert Temperature from Celsius to Fahrenheit
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
if (celsius < -273.15) {
printf("Invalid temperature: Absolute zero cannot be reached.\n");
}
else {
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
}
return 0;
}
Output
Enter temperature in Celsius: 25
25.00 Celsius = 77.00 Fahrenheit
Program Explanation
- The program starts by including the necessary header file
stdio.h
. - The
main()
function is defined. - Inside the
main()
function, two variablescelsius
andfahrenheit
of typefloat
are declared to store the input temperature in Celsius and the converted temperature in Fahrenheit, respectively. - The program prompts the user to enter the temperature in Celsius by printing the message “Enter temperature in Celsius: “.
- The
scanf()
function is used to read a floating-point number from the user, and the input is stored in thecelsius
variable using the&
operator. - The program uses an if-else statement to check if the entered temperature is valid. The condition
celsius < -273.15
checks if the temperature is below absolute zero (-273.15 Celsius). If the condition is true, it prints the message “Invalid temperature: Absolute zero cannot be reached.” - If the entered temperature is valid (not below absolute zero), the program performs the temperature conversion from Celsius to Fahrenheit using the formula
(celsius * 9 / 5) + 32
. The result is stored in thefahrenheit
variable. - The program prints the original temperature in Celsius and the converted temperature in Fahrenheit using the
printf()
function. The format specifier%.2f
is used to display the floating-point numbers with two decimal places. - The
return 0
statement ends the program.
Conclusion
This C program allows the user to select a temperature conversion type (Celsius to Fahrenheit) and performs the conversion using if-else statements. It covers all possible cases and provides the appropriate output based on the user’s choice and input temperature.