Programming

Program to Convert the Temperature in Degree Celsius to Degree Fahrenheit

Write a Program to convert the temperature in degree Celsius to degree Fahrenheit and vice-versa using concept of class and object.

Source Code:

#include<conio.h>
#include<iostream.h>
#include<process.h>

class Temp
{
       private:
               float f;
               float c;
       public:
               void read_c()
               {
                       cin>>c;
               }
               void read_f()
               {
                       cin>>f;
               }
               void write_c()
               {
                       cout<<c;
               }
               void write_f()
               {
                       cout<<f;
               }
               void c_to_f()
               {
                       f=(9*c)/5+32;
               }
               void f_to_c()
               {
                       c=(5*(f-32))/9;
               }
};

void main()
{
       Temp t;
       int c;
       clrscr();
       do{
       cout<<endl<<"Menu"<<endl<<"1. Convdert degree Celsius to Fahrenheit"
           <<endl<<"2. Convert degree Fahrenheit to Celsius"<<endl<<"3. Exit";
       cout<<endl<<"Enter your choice: ";        cin>>c;
       switch(c)
       {
               case 1:
                       cout<<endl<<"Enter temperature in degree Celsius: ";
                       t.read_c();
                       t.c_to_f();
                       cout<<"Equivalent temperature in degree Fahrenheit is: ";
                       t.write_f();
                       break;
               case 2:
                       cout<<endl<<"Enter temperature in degree Fahrenheit: ";
                       t.read_f();
                       t.f_to_c();
                       cout<<"Equivalent temperature in degree Celcius is: ";
                       t.write_c();
                       break;
               case 3:
                       exit(1);
                       break;
               default:
                       cout<<endl<<"Invalid Choice!!!";
                       break;
       }
      } while(c!=3);
       getch();

}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.