You are on page 1of 7

Write a program to convert temperature from Celsius to Fahrenheit and vice-versa

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

void main()
{
clrscr();
int choice;
float ctemp,ftemp;
cout << "1.Celsius to Fahrenheit" << endl;
cout << "2.Fahrenheit to Celsius" << endl;
cout << "Choose between 1 & 2 : " << endl;
cin>>choice;
if (choice==1)
{
cout << "Enter the temperature in Celsius : " << endl;
cin>>ctemp;
ftemp=(1.8*ctemp)+32;
cout << "Temperature in Fahrenheit = " << ftemp << endl;
}
else if(choice==2)
{
cout << "Enter the temperature in Fahrenheit : " << endl;
cin>>ftemp;
ctemp=(ftemp-32)/1.8;
cout << "Temperature in Celsius = " << ctemp << endl;
}
else
{
cout<< “Wrong Choice!”<<endl;
}
getch();
}

Output:

1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
Choose between 1 & 2 : 2
Enter the temperature in Fahrenheit : 98.6
Temperature in Celsius = 37.0000

Write a program to convert volume from Liters to Gallons

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

void main()
{
clrscr();
int choice;
float l,g;
cout << "1.Gallons to Liters" << endl;
cout << "2.Liters to Gallons" << endl;
cout << "Choose between 1 & 2 : " << endl;
cin>>choice;
if (choice==1)
{
cout << "Enter the volume in gallons : " << endl;
cin>>g;
l=g*3.7854;
cout << "Volume in Liters = " << l << endl;
}
else if(choice==2)
{
cout << "Enter the volume in liters : " << endl;
cin>>l;
g=l/3.7854;
cout << "Volume in Gallons = " << g << endl;
}
else
{
cout<< “Wrong Choice!”<<endl;
}
getch();
}

Output:

1.Gallons to Liters
2.Liters to Gallons
Choose between 1 & 2 : 1
Enter the volume in gallons : 2.5
Volume in Liters = 9.4635

Write a program for unary operator overloading

#include<iostream.h>
#include<conio.h>
#include<string.h>
class complex
{
private:
float real;
float imag;
};

complex operator + (complex a,complex b)


{
complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return c;
}
 
void main()
{
clrscr();
complex a,b,c;
int ch;
cout<<"Enter the first complex no:";
cin>>a.real>>a.imag;
cout<<"Enter the second complex no:";
cin>>b.real>>b.imag;
c =a + b;
cout<<"Addition of 2 no’s : "<<c.real<<"+i"<<c.imag;
getche();
}

Output:

Enter the first complex no: 2


2
Enter the second complex no: 3
1
Addition of 2 no’s : 5+i3

Write a program to find sum of ‘N’ natural numbers

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

void main()
{
int i,a=0,n;
cout<<"Enter the no. of elements in the series : ";
cin>>n;
for(i=1;i<=n;i++)
{
a=a+i;
}
cout<<"Sum of “<<n<<” natural numbers is"<<a;
getch();
}

Output:

Enter the no. of elements in the series : 5


Sum of 5 natural numbers is : 15

Program to swap two numbers using pointers

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

void swap(int *d1,int*d2)


{
Int temp;
temp=*d1;
*d1=*d2;
*d2=temp;
}

void main()
{
clrscr();
int a,b;
cout<<”Enter number A : “;
cin>>a;
cout<<”Enter number B : “;
cin>>b;
cout<<”Before swapping : “<<endl;
cout<<”A = “<<a;
cout<<”B = “<<b;
swap(&a,&b);
cout<<”After swapping : “<<endl;
cout<<”A = “<<a;
cout<<”B = “<<b;
getche();
}

Output:

Enter number A : 1
Enter number B : 2
Before swapping :
A=1
B=2
After swapping :
A=2
B=1

Program to add two times using objects as arguments

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

class time
{
private:
int hh;
int mm;
int ss;
public:
void enter();
void disp();
friend time add(time, time);
};

void time::enter()
{
cout<<"Hours : ";
cin>>hh;
cout<<"Minutes: ";
cin>>mm;
cout<<"Seconds: ";
cin>>ss;
};

void time::disp()
{
cout<<"\n\nResultant Time :";
cout<<"\nHours : "<<hh;
cout<<"\nMinutes: "<<mm;
cout<<"\nSeconds: "<<ss;
};

time add(time t1, time t2)


{
time t;
int h, m, s;
s=t1.ss+t2.ss;
m=t1.mm+t2.mm+s/60;
t.ss=s%60;
t.hh=t1.hh+t2.hh+m/60;
t.mm=m%60;
return(t);
};

void main()
{
time o,o1, o2;
clrscr();
cout<<"\nEnter the First Time :\n";
o1.enter();
cout<<"\n\nEnter the Second Time :\n";
o2.enter();
o=add(o1,o2);
o.disp();
getch();

You might also like