< All Topics
Print

Write a Program to Convert Celsius To Fahrenheit and Vice Versa.

Aim

To write a C program to convert Celsius to Fahrenheit and vice versa.

Procedure/ Algorithm

Step-1: Start the program execution.

Step-2: Declare f, c, ch.

Step-3: Get the value of choice whether Celsius to Fahrenheit conversion or vice versa.

Step-4: If choice=1, input the value of Fahrenheit and convert that to Celsius using the formula (f-32.0)/1.8).

Step-5: If choice=2, input the value of Fahrenheit and convert that to Celsius using the formula (c*1.8)+32.0).

Step-6: Print the results of f and c.

Step-7: Stop the program execution.

Code


// author milndbhatt
#include<stdio.h>
#include<conio.h>
int main()
{
float f,c;
int ch;

printf("\nEnter the choice:\n1.Fahrenheit to celcius\n2.celcius to fahreheit: choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter fahrenheit:"); scanf("%f",&f);
printf("\nEquivalent celcius value is %f",((f-32.0)/1.8));
break;

case 2: printf("\nEnter celcius:"); scanf("%f",&c);
printf("\nEquivalent fahrenheit value is %f",((c*1.8)+32.0));
break;

default: printf("\nEnter the correct choice:"); break;
}
return 0;
} // end main

Output

ROUND-1:
Enter the choice:
1.Fahrenheit to celcius
2.celcius to fahreheit: choice:1

Enter fahrenheit:230

Equivalent celcius value is 110.000000
--------------------------------
Process exited after 8.733 seconds with return value 0
Press any key to continue . . .

=================================================================================

ROUND-2:


Enter the choice:
1.Fahrenheit to celcius
2.celcius to fahreheit: choice:2

Enter celcius:40

Equivalent fahrenheit value is 104.000000
--------------------------------
Process exited after 5.406 seconds with return value 0
Press any key to continue . . .

Run Directly on in-built C-Compiler

Result

Thus, the C program has been written, executed successfully to calculate temperature conversion from Fahrenheit to Celsius or Celsius to Fahrenheit.


Recommended Readings

Leave a comment

Table of Contents