Celsius to Fahrenheit: flowchart, algorithm and program
This program reads a temperature in Celsius and prints the same temperature in Fahrenheit. It applies the standard formula f = c * 9 / 5 + 32 and shows the result with 2 decimal places.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read c (Celsius).
- Step 3: Set f = c * 9 / 5 + 32.
- Step 4: Print f (Fahrenheit).
- Step 5: Stop.
Pseudocode
BEGIN
READ c (Celsius)
f ← c * 9 / 5 + 32
PRINT f (Fahrenheit)
END
Program in C, Python and Java
Celsius to Fahrenheit program in C
#include <stdio.h>
int main() {
float c, f;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
f = c * 9 / 5 + 32;
printf("Fahrenheit = %.2f\n", f);
return 0;
}Celsius to Fahrenheit program in Python
c = float(input("Enter temperature in Celsius: "))
f = c * 9 / 5 + 32
print("Fahrenheit = {:.2f}".format(f))Celsius to Fahrenheit program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
double c = sc.nextDouble();
double f = c * 9 / 5 + 32;
System.out.printf("Fahrenheit = %.2f%n", f);
}
}
Sample input: c = 37
Output: Enter temperature in Celsius: Fahrenheit = 98.60
How it works
- Declare two float variables, c for the Celsius value and f for the Fahrenheit result. Float is used so inputs like 36.6 work.
- scanf("%f", &c) reads the temperature typed by the user into c.
- The line f = c * 9 / 5 + 32 does the conversion. For c = 37: 37 * 9 = 333, 333 / 5 = 66.6, 66.6 + 32 = 98.6.
- printf("%.2f") prints f rounded to 2 decimal places, so the output is 98.60.
- Because c is a float, c * 9 / 5 is calculated with decimals and nothing is lost.
Dry run (trace table)
| Step | Line | Box | c | f | Output |
|---|---|---|---|---|---|
| 1 | 3 | Start | |||
| 2 | 6 | Read c (Celsius) | 37 | Enter temperature in Celsius: | |
| 3 | 7 | f = c * 9 / 5 + 32 | 37 | 98.6 | |
| 4 | 8 | Print f (Fahrenheit) | 37 | 98.6 | Fahrenheit = 98.60 |
| 5 | 9 | Stop | 37 | 98.6 |
Time and space complexity
Time: O(1) · Space: O(1). The program does a fixed number of operations regardless of input value.
Common mistakes
- Writing 9 / 5 first with integers. In C, 9 / 5 gives 1, not 1.8, so the answer is wrong. Keep c * 9 / 5, or write 9.0 / 5.
- Forgetting the & in scanf("%f", &c), which can crash the program or give garbage values.
- Using %d to print a float. Use %f (or %.2f) for float variables.
- Forgetting to add 32 after the multiplication, or adding it before dividing.
Viva questions
Q1. What does this program do, and what formula does it use?
It reads a temperature in Celsius from the user and converts it to Fahrenheit using f = c * 9 / 5 + 32, then prints the result.
Q2. Why have you declared c and f as float and not int?
Temperatures can have decimal parts like 36.6, and the result of the conversion is often fractional. An int would drop the decimal part.
Q3. What is the purpose of & in scanf("%f", &c)?
scanf needs the address of c so it can store the typed value directly into that variable. Without & it would try to write to a wrong memory location.
Q4. What does %.2f do in the printf on line 8?
It prints the float with exactly 2 digits after the decimal point, rounding the value. It only changes how it is displayed, not the stored value of f.
Q5. What would go wrong if the formula were written as c * (9 / 5) + 32?
9 / 5 is integer division and gives 1, so the formula would become c + 32 and the answer would be wrong. In c * 9 / 5, c is a float, so c * 9 is a float and the division keeps its decimals.
Q6. What output do you get for inputs 0, 100 and -40?
0 gives 32.00, 100 gives 212.00, and -40 gives -40.00, which is the point where the two scales are equal. Negative inputs work fine because nothing in the formula restricts the sign.
Q7. What is the time complexity of this program?
It is O(1), constant time, because it has no loops and does a fixed number of operations whatever the input value is.
Q8. What would you change to convert Fahrenheit to Celsius instead?
Read the value as f and use c = (f - 32) * 5 / 9, then print c. Note the subtraction must be in brackets so it happens before the multiplication.
Q9. What happens if the user types a letter like 'a' instead of a number?
scanf fails to read a float and returns 0, leaving c uninitialised, so the output is garbage. To handle this, check the return value of scanf and show an error message if it is not 1.
Frequently asked questions
What is the formula to convert Celsius to Fahrenheit in C?
Use f = c * 9 / 5 + 32. In C, make sure at least one value is a float, such as c, so the division 9 / 5 does not get truncated to 1.
Why does the program use float instead of int?
Temperatures often have decimals, and the result of the formula usually does too (37°C gives 98.6°F). An int would drop the fractional part.
Why is the output 98.60 and not 98.6?
The format %.2f tells printf to always show exactly 2 digits after the decimal point, so 98.6 is printed as 98.60.