Write a C program to convert Celsius to Fahrenheit

Description: Take a temperature in Celsius and print it in Fahrenheit.

Hint:

  • Fahrenheit = (Celsius x 9 / 5) + 32. Write 9.0 / 5 to avoid integer division.

C program to convert Celsius to Fahrenheit

#include <stdio.h>

int main(void) {
    float celsius, fahrenheit;

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

    fahrenheit = celsius * 9.0f / 5 + 32;
    printf("%.1f C = %.1f F\n", celsius, fahrenheit);
    return 0;
}

Output:

Output of the above program is

Enter temperature in Celsius: 37
37.0 C = 98.6 F

How it works

  • 37 * 9.0 / 5 is 66.6, and adding 32 gives 98.6.
  • If we write 9 / 5 with integers, the result is 1 and the answer is wrong.

Read C Type Casting.


Learn more about the similar topics:
Tutorials
C Keywords
C Variables
C Data Types
C Constants
C printf and scanf
C Escape Sequences
C Format Specifiers
Exercises & Assignments
Write a C program to swap the values of two variables using a third variable
Write a C program to print words using spacing and new line characters
Write a C program to get the salary from the user and print it
Write a C program to find the size of int, float, long, double and char
Write a C program to get the marks of 5 subjects and find the total and average
Write a C program to calculate the perimeter of a rectangle
Write a C program to convert minutes into hours and minutes
Interview Questions & Answers
What is the difference between a variable and a constant in C?
What are the data types in C?
What is the difference between = and == in C?
What is the difference between ++i and i++ in C?
What is the use of the sizeof operator in C?
How to swap two numbers in C?