A simple Celsius to Fahrenheit to Celsius Converter

Another good way to practice your programing skills- Celsius to Fahrenheit converter. There is a good formula for the conversion which makes it easy to create converter programs. We have a basic console program…

The cyclone named “Mohasen“ (or Mahasen) is crossing Bangladesh. Unfortunately 4 people were dead because of this. Where I live it is a rainy weather here, but no sign of Mohasen here. But the weather is a excuse for today’s post. I have a Thermometer in my room. Due to the rainy nature of weather it is 21 degree Celsius and 65 degree Fahrenheit here.

I have a thermometer, that’s why I got both Celsius and Fahrenheit temperature. But what happens when you got one of the units? Either Celcius or Fahrenheit …and you need the other? That’s why the converters. Celsius (or Centigrade) to Fahrenheit to Celsius converters are interesting programs. They use a simple formula and manage to convert from one temperature unit to another. It is pretty amazing!

Celsius and Fahrenheit

Where I live, Celsius is usually used to measure the temperature of the climate/weather, and Fahrenheit is used to measure the temperature of the body. They have their different scales, so one temperature cannot be the same in both Celsius and Fahrenheit. So we have to convert between them to get the appropriate amount.

The Conversion

The converter formula is as follows:

In the above formula  C refers to the Celsius and F refers to Fahrenheit. We can convert Celsius to Fahrenheit and back with the above function/formula.

Celsius to Fahrenheit example:

1
2
ReadLn(Cel);
WriteLn('The converted Fahrenheit is: ' + FloatToStr((Cel * 1.8) + 32));

9/5 is replaced by 1.8. It’s the same thing, because 9/5=1.8.

Fahrenheit to Celsius example:

Similarly, Fahrenheit to Celsius conversion:

1
2
ReadLn(Fah);
WriteLn('The converted Celsius is: ' + FloatToStr((Fah - 32) / 1.8));

This is also a modification of the formula. Notice that the use of 1.8 is here as well. The 1.8 is divided instead of multiplying as the formula says. The division has reversed the 5/9 to 9/5. (Fah - 32) * 5/9 is the same as (Fah - 32) / 9/5. So we use the same 1.8 because 9/5=1.8. This way, it’s easier to remember. Just remember 1.8 and you’ll be ok.

Example Program Code

The program below is a simple code example of how to implement the formula. Create a new Program Project (Project -> New Project -> Program -> OK). Then copy-paste the code below then Run the Project (F9 or Run->Run).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
program proj_cel_fah_converter;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  sysutils;

var
  userinput: string;
  inputint: Integer;
  Code: Integer;

label
  intro;

begin
  WriteLn('Temperature Converter Program.');
  WriteLn('-----------------------------------------');

intro:
  WriteLn();
  WriteLn('Choose an option:');
  WriteLn('1: Celsius to Fahrenheit');
  WriteLn('2: Fahrenheit to Celsius');
  WriteLn('3: Exit the program');
  WriteLn();
  Write('Your choice: ');
  ReadLn(userinput);

  if userinput = '1' then begin
    WriteLn('Celsius to Fahrenheit Converter');
    Write('Enter Celsius: ');
    ReadLn(userinput);

    Val(userinput, inputint, Code);
    if Code <> 0 then
      goto intro
    else
      WriteLn('Fahrenheit: '+FloatToStr((inputint*1.8)+32));

    ReadLn;

  end else if userinput = '2' then begin
    WriteLn('Fahrenheit to Celsius Converter');
    Write('Enter Fahrenheit: ');
    ReadLn(userinput);

    Val(userinput, inputint, Code);
    if Code <> 0 then
      goto intro
    else
      WriteLn('Celsius: '+FloatToStr((inputint-32)/1.8));

    ReadLn;

  end else if userinput = '3' then begin
    WriteLn('Exiting...');
    Exit;

  end else begin
    WriteLn('Wrong input!');
    goto intro;

  end;

  WriteLn();
  WriteLn('Press ENTER to exit...');
  ReadLn;
end.

Explanation

1
2
3
4
5
label
  intro;
...
intro:
...

We use a label so that we can get back to this point in code when we need to start over again. According to the code if the user enters an invalid input, the program starts from the beginning:

1
2
3
  end else begin
    WriteLn('Wrong input!');
    goto intro;

The user is given 3 options with this code:

1
2
3
4
5
6
7
  WriteLn('Choose an option:');
  WriteLn('1: Celsius to Fahrenheit');
  WriteLn('2: Fahrenheit to Celsius');
  WriteLn('3: Exit the program');
  WriteLn();
  Write('Your choice: ');
  ReadLn(userinput);

The first 2 would let the user select either Celsius to Fahrenheit converter or Fahrenheit to Celsius converter. The third option is to quit the program.

1
2
3
4
5
    Val(userinput, inputint, Code);
    if Code <> 0 then
      goto intro
    else
      WriteLn('Celsius: '+FloatToStr((inputint-32)/1.8));

The Val() procedure tries to convert the input string to inputint (integer). If it cannot convert the input string to Integer then it returns non-zero number in the Code variable, so it’s an error. If an error occurs, we return to choices menu. That’s why we have used a intro Label. If there is no error in the input then the conversion will take place.

Ref:
http://www.freepascal.org/docs-html/ref/refsu48.html
http://www.freepascal.org/docs-html/ref/refse7.html
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=259&lngWId=3

Downloads

You can download the source code for the tutorial project and executable/EXE files from the links below: