Program to Convert Celsius to Fahrenheit

Problem Statement: Write a C program to convert a temperature in Celsius to Fahrenheit

Description:
This program converts a given temperature from Celsius to Fahrenheit using the formula: Fahrenheit = (Celsius * 9/5) + 32
It demonstrates basic input/output functions and arithmetic operations in C.

Usage:
The user inputs a temperature in Celsius, and the program converts and displays the equivalent temperature in Fahrenheit.

Code:

C
#include <stdio.h>

int main() {
    float celsius, fahrenheit;

    // Asking user to input temperature in Celsius
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

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

    printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);

    return 0;
}

Explanation:

The program is designed to take a temperature input in Celsius from the user and convert it to Fahrenheit using the formula:

Breakdown of the Code:

#include 
  • This line includes the standard input/output library, which allows the program to display output to the screen and accept input from the user.
int main()
  • This defines the main function, where the execution of the program starts.
    float celsius, fahrenheit;
  • Two floating-point variables are declared:
    • celsius: to store the temperature input by the user in Celsius.
    • fahrenheit: to store the converted Fahrenheit temperature.
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
  • The program prompts the user to enter a temperature in Celsius and stores the input in the variable celsius.
    fahrenheit = (celsius * 9 / 5) + 32;
  • This line converts the temperature from Celsius to Fahrenheit using the standard conversion formula.
    • First, the Celsius value is multiplied by 9 and divided by 5 to scale it to the Fahrenheit scale.
    • Then, 32 is added to account for the difference between the two temperature scales’ zero points.
    printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
  • The program displays the Celsius value entered by the user and its corresponding Fahrenheit equivalent. The values are formatted to two decimal places for precision.
    return 0;
  • This indicates that the program executed successfully and is returning a value of 0.

Sample Input/Output:

Input:

Enter temperature in Celsius: 25

Output:

25.00 Celsius = 77.00 Fahrenheit

Summary:

The program reads a temperature in Celsius, converts it to Fahrenheit using a mathematical formula, and displays the result to the user. It demonstrates basic arithmetic and input/output operations in C programming.

Test here:

Leave a Reply

Your email address will not be published. Required fields are marked *