Celsius to Fahrenheit Conversion

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to write a C program to convert temperature in Celsius to Fahrenheit.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/comments -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/variables -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/data_types -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/operators -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/user_input -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/memory_address -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/pointers -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} c/function_declaration -.-> lab-123211{{"`Celsius to Fahrenheit Conversion`"}} end

Define Variables

In the first step, you need to declare the variables to store the temperature values. In this program, we will use two variables to store the temperature values: celsius and fahrenheit.

float celsius, fahrenheit; //declaring two variables to store temperature values

Take Input Celsius Value

In the second step, take input temperature value from the user. Using scanf function, you can get input from user.

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

Convert Celsius to Fahrenheit

In the third step, apply formula to convert Celsius to Fahrenheit. For this, we use the formula: (1.8 * celsius) + 32.

fahrenheit = (1.8 * celsius) + 32; //converting temperature from Celsius to Fahrenheit

Display the Fahrenheit Value

In the fourth step, display the Fahrenheit value using printf function.

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); //displaying temperature in fahrenheit

Full Code

Here is the full code for the program you have written. Copy the code and paste it in the main.c file.

#include<stdio.h>
int main ()
{
    float celsius, fahrenheit; //declaring two variables to store temperature values

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

    fahrenheit = (1.8 * celsius) + 32; //converting temperature from Celsius to Fahrenheit

    printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);  //displaying temperature in fahrenheit

    return 0;
}

Summary

You have successfully learned how to write a C program to convert temperature in Celsius to Fahrenheit. This program will help you in future projects where you need to convert temperature units.

Other C Tutorials you may like