C++ | Mass conversion

Introduction

This programs will convert Mass from one unit to other unit. The First Program will convert gram to milligram. The next one lets you convert kilogram to gram. The next one will convert Tonne to kilogram. And the last Program will convert Kilogram to Pound. So you can make more programs by using more units for conversion of mass by just modifying the conversion formula. Go enjoy the program. Lets begin……..

Program to convert Gram to Milligram.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the Mass in given unit save them in 'a'
cout<<"Enter the Mass in Gram"<<endl;
cin>>a;
//convert and save it in 'b'
b=a*1000;
//show the output 'b'
cout<<"Mass in Milligram is "<<b;
//get character
getch();
}

Output

Enter the Mass in Gram
9
Mass in Milligram is 9000

Program to convert Kilogram to Gram.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the Mass in given unit save them in 'a'
cout<<"Enter the Mass in Kilogram"<<endl;
cin>>a;
//convert and save it in 'b'
b=a*1000;
//show the output 'b'
cout<<"Mass in Gram is "<<b;
//get character
getch();
}

Output

Enter the Mass in Kilogram
9
Mass in Gram is 9000

Program to convert Tonne to Kilogram.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the Mass in given unit save them in 'a'
cout<<"Enter the Mass in Tonne"<<endl;
cin>>a;
//convert and save it in 'b'
b=a*1000;
//show the output 'b'
cout<<"Mass in Kilogram is "<<b;
//get character
getch();
}

Output

Enter the Mass in Tonne
6
Mass in Kilogram is 6000

Program to convert Tonne to Kilogram.

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type float
float a,b;
//Input the Mass in given unit save them in 'a'
cout<<"Enter the Mass in Kilogram"<<endl;
cin>>a;
//convert and save it in 'b'
b=a/2.204;
//show the output 'b'
cout<<"Mass in Pound is "<<b;
//get character
getch();
}

Output

Enter the Mass in Kilogram
8
Mass in Pound is 3.6297

How does it work

  1. You enter the Mass in given unit and is saved in variable ‘a’.
  2. The Mass 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 masses. Like wise you can also use following unit for conversion :-

  • Milligram
  • Gram
  • Kilogram
  • Tonne
  • Ton(UK)
  • Tons(US)
  • Grain
  • Ounce
  • Pound
  • Stone

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 Mass from variable and save them in one variable.
  7. Cout the converted Mass variable.

At the end

You learnt creating the c++ program of conversion of  Mass 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s