Sai A
Updated date Nov 29, 2023
In this blog, we will learn to convert Celsius to Fahrenheit in C. Explore various methods, from basic formulas to advanced techniques using functions and user-defined constants.

## Introduction:

Temperature conversion is a common task in programming, especially when dealing with weather data or scientific applications. One of the most frequent conversions is between Celsius and Fahrenheit. In this blog, we will explore multiple methods to convert Celsius to Fahrenheit using the C programming language.

## Method 1: Basic Formula

`F = 9 / 5​C +32`

Here's the C program for this method:

``````#include <stdio.h>

int main() {
float celsius, fahrenheit;

// Input temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

// Convert to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;

// Display the result
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}
``````

### Output:

``````Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
``````

The program takes a temperature in Celsius as input, applies the formula, and outputs the equivalent temperature in Fahrenheit.

## Method 2: Using a Function

In this method, we encapsulate the conversion logic into a separate function for better code organization and reusability.

``````#include <stdio.h>

// Function to convert Celsius to Fahrenheit
float celsiusToFahrenheit(float celsius) {
return (celsius * 9 / 5) + 32;
}

int main() {
float celsius, fahrenheit;

// Input temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

// Convert using the function
fahrenheit = celsiusToFahrenheit(celsius);

// Display the result
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}
``````

### Output:

``````Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
``````

This method introduces a function `celsiusToFahrenheit` that encapsulates the conversion logic, making the code more modular.

## Method 3: Using User-Defined Constants

In this method, we use user-defined constants for the conversion formula, making the code more readable and allowing for easy adjustments if needed.

``````#include <stdio.h>

#define SCALE_FACTOR 9.0 / 5.0
#define FREEZING_POINT 32.0

// Function to convert Celsius to Fahrenheit
float celsiusToFahrenheit(float celsius) {
return (celsius * SCALE_FACTOR) + FREEZING_POINT;
}

int main() {
float celsius, fahrenheit;

// Input temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

// Convert using the function
fahrenheit = celsiusToFahrenheit(celsius);

// Display the result
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}
``````

### Output:

``````Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
``````

Using constants enhances code readability and allows for easy modification of conversion factors.

## Conclusion:

In this blog, we have explored multiple methods to convert Celsius to Fahrenheit using the C programming language. We have started with the basic formula and gradually introduced improvements such as using functions and user-defined constants.