Write a program in C++ and python to convert temperature from Fahrenheit to Celsius and vice-versa

By | August 1, 2019

In this program first, we will ask the user whether he wants to change the temperature from Fahrenheit to Celsius or vice versa and then accept a temperature and convert it.

C++:

#include<iostream.h>
#include< conio.h>
#include<stdio.h>
#include<math.h>

void main()
{
clrscr();
float ut,ct;
int ch;
cout<<"Enter 1 to change temperature from Fahrenheit to Celsius\n";
cout<<"Enter 2 to change temperature from Celsius to Fahrenheit\n";
cin>>ch;
if (ch==1)
{cout<<"Enter Temperature in Fahrenheit: ";
cin>>ut;
ct= (ut-32)/1.8;
cout<<ct<<" C";
}
if (ch==2)
{
cout<<"Enter Temperature in Celsius: ";
cin>>ut;
ct=(1.8*ut)+32;
cout<<ct<<" F";
}
getch();
}

Output:

Enter 1 to change temperature from Fahrenheit to Celsius
Enter 2 to change temperature from Celsius to Fahrenheit
2
Enter Temperature in Celsius: 12
53.6 F

Python:

print("Enter 1 to change temperature from Fahrenheit to Celsius")
print("Enter 2 to change temperature from Celsius to Fahrenheit")
ch = int(input(""))
if ch==1:
    ut= float(input("Enter Temperature in Fahrenheit: " ))
    ct=(ut-32)/1.8
    print(ct,"C")
if ch==2:
    ut= float(input("Enter Temperature in Celsius: " ))
    ct=(1.8*ut)+32
    print(ct,"F")

Output:

Enter 1 to change temperature from Fahrenheit to Celsius
Enter 2 to change temperature from Celsius to Fahrenheit
1
Enter Temperature in Fahrenheit: 23
-5.0 C

Leave a Reply

Your email address will not be published. Required fields are marked *