# Techtopz

(@techtopz)

Techtopz is a blog on programming and tech stuff.

# C++ | Length conversion

These program will convert length from one unit to another. The first one lets you convert meter to kilometer. The next one lets you convert Centimeter to Meter. And the third one lets you convert Meter to Centimeter. The fourth program will convert Centimeter to Millimeter. So you can convert more units of length by just adding the conversion formula to it.Go enjoy the program. Lets begin……..

Program to convert Meter to Kilometer.

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the length save it in 'a'
cout<<"Enter the Length in Meter"<<endl;
cin>>a;
//calculate and save them in 'b'
b=a/1000;
//show the output 'b'
cout<<"Length in Kilometer is "<<b;
//get character
getch();
}
``````

output

Enter the Length in Meter
9069
Length in Kilometer is 9.069

Program to convert Centimeter to Meter.

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the length save it in 'a'
cout<<"Enter the Length in Centimeter"<<endl;
cin>>a;
//calculate and save them in 'b'
b=a/100;
//show the output 'b'
cout<<"Length in Meter is "<<b;
//get character
getch();
}
``````

Output

Enter the Length in Centimeter
50
Length in Meter is 0.50

Program to convert Meter to Centimeter

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the length save it in 'a'
cout<<"Enter the Length in Meter"<<endl;
cin>>a;
//calculate and save them in 'b'
b=a*100;
//show the output 'b'
cout<<"Length in Centimeter is "<<b;
//get character
getch();
}
``````

Output

Enter the Length in Meter
9
Length in Centimeter is 900

Program to convert Centimeter to Millimeter.

``````#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the length save it in 'a'
cout<<"Enter the Length in Centimeter"<<endl;
cin>>a;
//calculate and save them in 'b'
b=a*10;
//show the output 'b'
cout<<"Length in Millimeter is "<<b;
//get character
getch();
}
``````

Output

Enter the Length in Centimeter
85
Length in Millimeter is 850

How does it work
1. You enter the length in given unit and is saved in variable ‘a’.
2. The length is converted according to conversion formula and is saved in ‘b’.
4. Variable ‘b’ is shown by cout.

Extending it
The Program can be extended by adding more unit to conversion and by adding their Conversion formula for other unit and more variables can be added to program to convert multiple lengths. Like wise you can also use following unit for conversion :
- nanometer
- picometer
- millimeter
- centimeter
- meter
- kilometer
- terameter

Explanation
1. Include ‘iostream.h’ and ‘conio.h’ files.