C program to convert Celsius to Fahrenheit

C program to convert celsius to fahrenheit

This C program converts temperature in degree Celsius into degree Fahrenheit.

Prerequisite knowledge:

Here you’re going to solve a problem consisting of a mathematical calculation, therefore you must know the relation between degree Celsius and degree Fahrenheit which is mentioned below.

Celsius to Fahrenheit formula in C:

The formula to convert Celsius to Fahrenheit is F = 9/5C + 32 , but while coding this into a c statement, keep the precedence of operators in mind and best practice to avoid any typical error is using parentheses while writing formulae in c statements.

Program: C program to convert Celsius to Fahrenheit

#include<stdio.h>
int main()
{
    float tF,tC;
    printf("Temperature in Celsius: ");
    scanf("%f",&tC);
    tF=((9*tC)/5)+32;
    printf("\nTemperature in Fahrenheit: %.2f\n",tF);
    return 0;
}

Output:

Temperature in Celsius: 37

Temperature in Fahrenheit: 98.60

Program 2: C program to convert Celsius to Fahrenheit using function

This code snippet uses the exactly same logic to convert temperature in degree Celsius to degree Fahrenheit using function. This is very easy to implement if you know the formula and the basics of functions in the C programming language.

#include<stdio.h>
float CtoF(float);    //function prototype
int main()
{
    float tF,tC;
    printf("Temperature in Celsius: ");
    scanf("%f",&tC);
    tF = CtoF(tC);
    printf("\nTemperature in Fahrenheit: %.2f\n",tF);
    return 0;
}
float CtoF(float c)
{
    float f = ((9*c)/5)+32;
    return f;
}

Output: The output remains the same as it does the same thing.

Temperature in Celsius: -40

Temperature in Fahrenheit: -40.00

Program 3: C program to convert Celsius to Fahrenheit and Fahrenheit to Celsius

Now as the heading suggests, this C program converts Celsius to Fahrenheit and Fahrenheit to Celsius using functions. Using function in this case will be a better choice. If you don’t want to use functions, just copy the code from the functions inside the if-else statements.

#include<stdio.h>
float CtoF(float);
float FtoC(float);
int main()
{
    char choice;
    float tF,tC;
    printf("\n\n   1 for Celsius to Fahrenheit    ||   2 for Fahrenheit to Celsius\n\t\t\tEnter your choice: ");
    scanf("%c", &choice);
    if(choice == '1')
    {
        printf("Enter Temperature in Celsius: ");
        scanf("%f",&tC);
        tF = CtoF(tC);
        printf("\nEnter Temperature in Fahrenheit: %.2f\n",tF);
    }
    else
        if(choice=='2')
        {
            printf("Enter Temperature in Fahrenheit: ");
            scanf("%f",&tF);
            tC = FtoC(tF);
            printf("\nEnter Temperature in Celsius: %.2f\n",tC);
        }
        else 
            printf("\n\tEnter a valid choice/Try again!\n");
    
    return 0;
}
float CtoF(float c)
{
    float f = (9*c/5)+32;
    return f;
}
float FtoC(float f)
{
    float c = (f-32)*5/9;
    return c;
}

Challenge:

Write program to convert degree Fahrenheit into degree Celsius in the comment section(with indentation). This will help other beginners in a way you can’t even imagine.

Summary:

In this article, we learnt to write a C program to convert Celsius to Fahrenheit with and without using functions. Then we wrote a program to convert Celsius to Fahrenheit and vice-versa.

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

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