C++ Program for Conversion Between Kilograms and Grams

Weight Kilogram Gram

C++ programs for conversion between kilograms and grams have been given below. These are the units to calculate mass of an object. To convert kilograms into grams, multiply the number of kilograms by 1000 and to convert grams into kilograms, divide the number of grams by 1000.


Example:

Convert kilograms to grams:

(i) 1 kilogram = (1 x 1000) grams = 1000 grams

(ii) 2.5 kilograms = (2.5 x 1000) grams = 2500 grams

(iii) 1.875 kilograms = (1.875 x 1000) grams = 1875 grams


Convert gram to kilogram:

(i)1 gram = (1 / 1000) kilograms = 0.001 kilograms

(ii)300 grams = (300 / 1000) kilograms = 0.300 kilograms

(iii)1250 grams = (1250 / 1000) kilograms = 1.250 kilograms



The following section covers the C++ programs to solve the above problems are along with the algorithm, pseudocode and time-complexity of the programs.





1. Algorithms for Conversion between Kilograms and Grams




1.1. Algorithm to convert Kilograms to Grams


1. Take value in kilograms k as input.

2. Perform g = k * 1000 to convert kilograms to grams

3. Display value of g as the output.




1.2. Algorithm to convert grams to Kilograms


1. Take value in grams g as input.

2. Perform k = g / 1000 to convert grams to kilograms

3. Display value of k as the output.




2. Pseudocode for Conversion between Kilograms and Grams




2.1. Pseudocode to convert Kilograms to Grams


Input : Value in Kilograms $k$

Output : $k$ in grams

1. Procedure kilogramToGram($k$):

2. $g \leftarrow k \times 1000$

3. Return $g$

4. End Procedure





2.2. Pseudocode to convert grams to Kilograms


Input : Value in Grams $g$

Output : $g$ in Kilograms

1. Procedure gramToKilogram($g$):

2. $k \leftarrow \frac{g}{1000}$

3. Return $k$

4. End Procedure





3. Time complexity for Conversion between Kilograms and Grams


Time Complexity: O(1)




4. Programs for Conversion between Kilograms and Grams




4.1. C++ program to convert Kilograms to Grams

Code has been copied
/*****************************************
           alphabetacoder.com
C++ program to convert Kilograms to Grams
******************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    float g, k;

    // take input
    cout << "Enter values in kilograms: ";
    cin >> k;
    // convert kilograms to grams
    g = k * 1000;
    // display result
    cout << k << " kilograms = " << g << " grams" << endl;

    return 0;
}

Output


Enter values in kilograms: 1.75

1.75 kilograms = 1750 grams





4.2. C++ program to convert Grams to Kilograms

Code has been copied
/****************************************
           alphabetacoder.com
C++ program to convert Grams to Kilograms
*****************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    float g, k;

    // take input
    cout << "Enter values in grams: ";
    cin >> g;
    // convert grams to kilograms
    k = g / 1000;
    // display result
    cout << g << " grams = " << k << " kilograms" << endl;

    return 0;
}

Output


Enter values in grams: 100

100 grams = 0.1 kilograms