C program to convert celsius to fahrenheit

In this blog post, we learn how to write a C program to convert Celsius to Fahrenheit. We will write the C program to convert Celsius to Fahrenheit. Write a C program to input temperature in Centigrade and convert it to Fahrenheit. How to convert temperature from degree centigrade to degree Fahrenheit in C programming. Logic to convert temperature from Celsius to Fahrenheit in C.

Example,

Input: 10
Output: 50


Input : -40
Output : -40

 

Formula to convert Celsius to Fahrenheit:

Below is a formula for how you can convert Celsius to Fahrenheit in your C Code.

 

Celsius to Fahrenheit: (°C × 9/5) + 32 = °F

 

 

C program to convert Celsius to Fahrenheit:

The below program asks the user to enter the temperature in Celsius. After getting the temperature in Celsius from the user program convert it in terms of Fahrenheit. So in simple words, we are following the below steps to convert temperature Celsius to Fahrenheit:

  • Get the user-given temperature in Celsius units.
  • Apply in the formula Fahrenheit = (celsius * (9 / 5)) + 32.
  • Now print the temperature in Fahrenheit.
#include <stdio.h>

int main()
{
    float celsius, fahrenheit;

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

    //celsius to fahrenheit conversion formula
    fahrenheit = (celsius * 9 / 5) + 32;

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

    return 0;
}

Output:

Enter temperature in Celsius: 10
10.00 Celsius = 50.00 Fahrenheit

C program to convert Celsius to Fahrenheit using a function:

The below program asks the user to enter the temperature in Celsius. After getting the temperature in Celsius call a function named convertCelFahrenheit() to convert the temperature from Celsius to Fahrenheit.

#include <stdio.h>

float convertCelFahrenheit(float c)
{
    return ((c * 9.0 / 5.0) + 32.0);
}


int main()
{
    float celsius, fahrenheit;

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

    //called function to convert celsius to fahrenheit
    fahrenheit = convertCelFahrenheit(celsius);

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

    return 0;
}

Output:

Enter temperature in Celsius: 0
0.00 Celsius = 32.00 Fahrenheit

 

Write a generic C program which converts Celsius to Fahrenheit and vice versa:

We already know the formula to convert Celsius to Fahrenheit and Fahrenheit to Celsius. So let’s see a C program that asks the user to choose and converts the temperature unit accordingly.

#include <stdio.h>

int main()
{
    float fh,cl;
    char ch;

    printf("\n\n Press c to convert temperature from Fahrenheit to Celsius.");
    printf("\n\n Press f to convert temperature from Celsius to Fahrenheit.");
    printf("\n\n Enter your choice (c, f): ");
    scanf("%c",&ch);

    if((ch =='c') ||(ch =='C'))
    {
        printf("\n\nEnter temperature in Fahrenheit: ");
        scanf("%f",&fh);
        cl= (fh - 32) / 1.8;
        printf("\n\nTemperature in Celsius: %.2f\n\n",cl);
    }
    else if((ch =='f') ||(ch =='F'))
    {
        printf("\n\nEnter temperature in Celsius: ");
        scanf("%f",&cl);
        fh= (cl*1.8)+32;
        printf("\n\nTemperature in Fahrenheit: %.2f\n\n",fh);
    }
    else
    {
        printf("\n\nInvalid Choice !!!\n\n");
    }

    return 0;
}

Output:

c if else

 

Recommended Post:

One comment

Leave a Reply

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