C++ | Length conversion

Introduction

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’.
3. 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.
2. Add void main.
3. Start program by first clearing the screen.
4. Declare the variables as float (name them as you want.)
5. Add cout and cin of variables you want to convert from.(as shown in program)
6. Convert the length from variable and save them in one variable.
7. Cout the converted length variable.

At the end

You learnt creating the c++ program of conversion of lengths from one unit to other unit. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: