Related Articles
Menu Driven Program to convert MKS to CGS and CGS to MKS
• Last Updated : 16 Jul, 2019

Prerequisite: Switch Case in C/C++

Problem Statement:
Write a menu-driven program using the Switch case to convert the CGS system to MKS system and vice versa.

Approach:
MKS(Metre-Kilogram-Second) System: The MKS System of units refers to the physical measurement system in which the Dimensions length, weight and time are measured in Metres, Kilograms and Seconds respectively.

```MKS System:

Length - metres (m)
Weight - kilograms (kg)
Time - seconds (s)
```

CGS(Centimetre-Gram-Second) System: The CGS System of units refers to the physical measurement system in which the Dimensions length, weight and time are measured in Centimetres, Grams and Seconds respectively.

```CGS System:

Length - centimetres (cm)
Weight - grams (g)
Time - seconds (s)
```

How to convert MKS to CGS?
In order to convert the MKS system to the CGS system, the conversion is done as follows:

```For Length:
Since,
1 metre = 100 centimetre
Therefore,
multiply the given length value by 100.

For Weight:
Since,
1 kilogram = 1000 gram
Therefore,
multiply the given weight value by 1000.

For Time:
Since,
1 second = 1 second
Therefore,
The time value will remain the same.
```

How to convert CGS to MKS?
Inorder to convert the CGS system to MKS system, the conversion is done as follows:

```For Length:
Since,
1 centimetre = 1/100 metre
Therefore,
divide the given length value by 100.

For Weight:
Since,
1 gram = 1/1000 kilogram
Therefore,
divide the given weight value by 1000.

For Time:
Since,
1 second = 1 second
Therefore,
The time value will remain the same.
```

Below is the implementation of the above approach:

Program:

 `// C++ menu-driven program using the Switch case ` `// to convert the CGS system to MKS system ` `// and vice versa ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// declare mks class pre-hand for reference ` `// to mks class for friend function in cgs. ` `class` `mks; ` ` `  `class` `cgs { ` ` `  `private``: ` `    ``// cgs units: centimetre, gram, second. ` `    ``//(length, weight, time) ` `    ``float` `cm, gm, sec; ` ` `  `public``: ` `    ``// Default constructor ` `    ``cgs() ` `    ``{ ` `        ``cm = 0; ` `        ``gm = 0; ` `        ``sec = 0; ` `    ``} ` ` `  `    ``// Paramterized constructor ` `    ``cgs(``float` `x, ``float` `y, ``float` `z) ` `    ``{ ` `        ``cm = x; ` `        ``gm = y; ` `        ``sec = z; ` `    ``} ` ` `  `    ``void` `disp_cgs() ` `    ``{ ` `        ``cout << cm << ``"\t"` `<< gm ` `             ``<< ``"\t"` `<< sec << ``"\n"``; ` `    ``} ` ` `  `    ``void` `disp_cgs1() ` `    ``{ ` `        ``cout << cm; ` `    ``} ` ` `  `    ``void` `disp_cgs2() ` `    ``{ ` `        ``cout << gm; ` `    ``} ` ` `  `    ``void` `disp_cgs3() ` `    ``{ ` `        ``cout << sec; ` `    ``} ` ` `  `    ``// friend functions: ` `    ``friend` `mks cgs2mks(cgs); ` `    ``friend` `cgs mks2cgs(mks); ` `}; ` ` `  `class` `mks { ` ` `  `private``: ` `    ``// mks units: metre, kilogram, second. ` `    ``//(length, weight, time) ` `    ``float` `m, kg, sec; ` ` `  `public``: ` `    ``// Default constructor ` `    ``mks() ` `    ``{ ` `        ``m = 0; ` `        ``kg = 0; ` `        ``sec = 0; ` `    ``} ` ` `  `    ``// Parameterized constructor ` `    ``mks(``float` `x, ``float` `y, ``float` `z) ` `    ``{ ` `        ``m = x; ` `        ``kg = y; ` `        ``sec = z; ` `    ``} ` ` `  `    ``void` `disp_mks() ` `    ``{ ` `        ``cout << m << ``"\t"` `<< kg ` `             ``<< ``"\t"` `<< sec << ``"\n"``; ` `    ``} ` ` `  `    ``void` `disp_mks1() ` `    ``{ ` `        ``cout << m; ` `    ``} ` ` `  `    ``void` `disp_mks2() ` `    ``{ ` `        ``cout << kg; ` `    ``} ` ` `  `    ``void` `disp_mks3() ` `    ``{ ` `        ``cout << sec; ` `    ``} ` ` `  `    ``friend` `mks cgs2mks(cgs); ` `    ``friend` `cgs mks2cgs(mks); ` `}; ` ` `  `// mks->cgs conversion: ` `mks cgs2mks(cgs c) ` `{ ` `    ``mks t; ` `    ``t.m = c.cm / 100; ` `    ``t.kg = c.gm / 1000; ` `    ``t.sec = c.sec; ` `    ``return` `t; ` `} ` ` `  `// cgs->mks conversion: ` `cgs mks2cgs(mks d) ` `{ ` `    ``cgs t; ` `    ``t.cm = d.m * 100; ` `    ``t.gm = d.kg * 1000; ` `    ``t.sec = d.sec; ` `    ``return` `t; ` `} ` ` `  `// Code for menu ` `void` `menu(``int` `mainMenuChoice, ``int` `parameterChoice) ` `{ ` `    ``float` `distanceValue = 30, weightValue = 300, timeValue = 3000; ` `    ``int` `continueChoice = 0; ` ` `  `    ``cout << ``"MKS <-> CGS converter menu:\n"``; ` `    ``do` `{ ` `        ``cout << ``"Enter 1 for CGS to MKS conversion.\n"` `             ``<< ``"Enter 2 for MKS to CGS conversion.\n"``; ` ` `  `        ``cout << ``"\nYou have chosen: "` `             ``<< mainMenuChoice ` `             ``<< ``"\n\n"``; ` ` `  `        ``switch` `(mainMenuChoice) { ` ` `  `        ``case` `1: { ` `            ``cout << ``"Enter 0 to convert all three parameters"` `                 ``<< ``"- distance, weight and time.\n"` `                 ``<< ``"Enter 1 for converting distance\n"` `                 ``<< ``"Enter 2 for converting weight\n"` `                 ``<< ``"Enter 3 for converting time\n"``; ` ` `  `            ``cout << ``"\nYou have chosen: "` `                 ``<< parameterChoice ` `                 ``<< ``"\n\n"``; ` ` `  `            ``switch` `(parameterChoice) { ` `            ``case` `0: { ` `                ``cout << ``"Enter values for distance unit in centimetre.\n"``; ` `                ``distanceValue; ` `                ``cout << endl; ` `                ``cout << ``"Enter values for weight unit in grams.\n"``; ` `                ``weightValue; ` `                ``cout << endl; ` `                ``cout << ``"Enter values for time unit in seconds.\n"``; ` `                ``timeValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in CGS system are: \n"``; ` `                ``cgs c(distanceValue, weightValue, timeValue); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << ``"Corresponding values in MKS system: \n"``; ` `                ``mks m = cgs2mks(c); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` ` `  `            ``case` `1: { ` ` `  `                ``cout << ``"Enter values for distance unit in centimetre.\n"``; ` `                ``distanceValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in CGS system are: \n"``; ` `                ``cgs c(distanceValue, 0, 0); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << ``"Corresponding values in MKS system: \n"``; ` `                ``mks m = cgs2mks(c); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` `            ``case` `2: { ` ` `  `                ``cout << ``"Enter values for weight unit in grams.\n"``; ` `                ``weightValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in CGS system are: \n"``; ` `                ``cgs c(0, weightValue, 0); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << ``"Corresponding values in MKS system: \n"``; ` `                ``mks m = cgs2mks(c); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` `            ``case` `3: { ` ` `  `                ``cout << ``"Enter values for time unit in seconds.\n"``; ` `                ``timeValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in CGS system are: \n"``; ` `                ``cgs c(0, 0, timeValue); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << ``"Corresponding values in MKS system: \n"``; ` `                ``mks m = cgs2mks(c); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` `            ``} ` `            ``break``; ` `        ``} ` ` `  `        ``case` `2: { ` `            ``cout << ``"Enter 0 to convert all three parameters"` `                 ``<< ``"- distance, weight and time.\n"` `                 ``<< ``"Enter 1 for converting distance\n"` `                 ``<< ``"Enter 2 for converting weight\n"` `                 ``<< ``"Enter 3 for converting time\n"``; ` ` `  `            ``cout << ``"\nYou have chosen: "` `                 ``<< parameterChoice ` `                 ``<< ``"\n\n"``; ` ` `  `            ``switch` `(parameterChoice) { ` `            ``case` `0: { ` `                ``cout << ``"Enter values for distance unit in metre.\n"``; ` `                ``distanceValue; ` `                ``cout << endl; ` `                ``cout << ``"Enter values for weight unit in kilogram.\n"``; ` `                ``weightValue; ` `                ``cout << endl; ` `                ``cout << ``"Enter values for time unit in second.\n"``; ` `                ``timeValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in MKS system are: \n"``; ` `                ``mks m(distanceValue, weightValue, timeValue); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << ``"Corresponding values in CGS system: \n"``; ` `                ``cgs c = mks2cgs(m); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` ` `  `            ``case` `1: { ` `                ``cout << ``"Enter values for distance unit in metre.\n"``; ` `                ``distanceValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in MKS system are: \n"``; ` `                ``mks m(distanceValue, 0, 0); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << ``"Corresponding values in CGS system: \n"``; ` `                ``cgs c = mks2cgs(m); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` ` `  `            ``case` `2: { ` ` `  `                ``cout << ``"Enter values for weight unit in kilogram.\n"``; ` `                ``weightValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in MKS system are: \n"``; ` `                ``mks m(0, weightValue, 0); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << ``"Corresponding values in CGS system: \n"``; ` `                ``cgs c = mks2cgs(m); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` ` `  `            ``case` `3: { ` ` `  `                ``cout << ``"Enter values for time unit in second.\n"``; ` `                ``timeValue; ` `                ``cout << endl; ` ` `  `                ``cout << ``"The entered values in MKS system are: \n"``; ` `                ``mks m(0, 0, timeValue); ` ` `  `                ``m.disp_mks(); ` ` `  `                ``cout << ``"Corresponding values in CGS system: \n"``; ` `                ``cgs c = mks2cgs(m); ` ` `  `                ``c.disp_cgs(); ` ` `  `                ``cout << endl; ` `                ``break``; ` `            ``} ` `            ``} ` `        ``} ` `        ``} ` `        ``cout << ``"Enter 1 to continue, or\n"` `             ``<< ``"Press any other key to exit.\n"``; ` ` `  `    ``} ``while` `(continueChoice == 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``cout << ``"Check for CGS to MKS of all units.\n\n"``; ` `    ``menu(1, 0); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for CGS to MKS of only distance unit.\n\n"``; ` `    ``menu(1, 1); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for CGS to MKS of only weight unit.\n\n"``; ` `    ``menu(1, 2); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for CGS to MKS of only time unit.\n\n"``; ` `    ``menu(1, 3); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"Check for MKS to CGS of all units.\n\n"``; ` `    ``menu(2, 0); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for MKS to CGS of only distance unit.\n\n"``; ` `    ``menu(2, 1); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for MKS to CGS of only weight unit.\n\n"``; ` `    ``menu(2, 2); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``cout << ``"\nCheck for MKS to CGS of only time unit.\n\n"``; ` `    ``menu(2, 3); ` ` `  `    ``cout << ``"\n-------------------------------------------------\n"``; ` ` `  `    ``return` `0; ` `} `

Output:

```Check for CGS to MKS of all units.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 1

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 0

Enter values for distance unit in centimetre.

Enter values for weight unit in grams.

Enter values for time unit in seconds.

The entered values in CGS system are:
30    300    3000
Corresponding values in MKS system:
0.3    0.3    3000

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for CGS to MKS of only distance unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 1

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 1

Enter values for distance unit in centimetre.

The entered values in CGS system are:
30    0    0
Corresponding values in MKS system:
0.3    0    0

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for CGS to MKS of only weight unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 1

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 2

Enter values for weight unit in grams.

The entered values in CGS system are:
0    300    0
Corresponding values in MKS system:
0    0.3    0

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for CGS to MKS of only time unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 1

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 3

Enter values for time unit in seconds.

The entered values in CGS system are:
0    0    3000
Corresponding values in MKS system:
0    0    3000

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------
Check for MKS to CGS of all units.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 2

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 0

Enter values for distance unit in metre.

Enter values for weight unit in kilogram.

Enter values for time unit in second.

The entered values in MKS system are:
30    300    3000
Corresponding values in CGS system:
3000    300000    3000

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for MKS to CGS of only distance unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 2

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 1

Enter values for distance unit in metre.

The entered values in MKS system are:
30    0    0
Corresponding values in CGS system:
3000    0    0

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for MKS to CGS of only weight unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 2

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 2

Enter values for weight unit in kilogram.

The entered values in MKS system are:
0    300    0
Corresponding values in CGS system:
0    300000    0

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------

Check for MKS to CGS of only time unit.

Enter 1 for CGS to MKS conversion.
Enter 2 for MKS to CGS conversion.

You have chosen: 2

Enter 0 to convert all three parameters- distance, weight and time.
Enter 1 for converting distance
Enter 2 for converting weight
Enter 3 for converting time

You have chosen: 3

Enter values for time unit in second.

The entered values in MKS system are:
0    0    3000
Corresponding values in CGS system:
0    0    3000

Enter 1 to continue, or
Press any other key to exit.

-------------------------------------------------
```

My Personal Notes arrow_drop_up
Recommended Articles
Page :