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)1500 grams = (1500 / 1000) kilograms = 1.500 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
*****************************************/

using System;
namespace GramKilogram {
    class Program {
        static void Main(string[] args) {
            // declare variables
            float g, k;

            // take input
            Console.Write("Enter values in kilograms: ");
            k = float.Parse(Console.ReadLine());

            // convert kilograms to grams
            g = k * 1000;

            // display result upto 3 decimal places
            Console.WriteLine("{0:F3} kilograms = {1:F3} grams", k, g);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter values in kilograms: 1.7

1.700 kilograms = 1700.000 grams





4.2. C# program to convert Grams to Kilograms

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

using System;
namespace GramKilogram {
    class Program {
        static void Main(string[] args) {
            // declare variables
            float g, k;

            // take input
            Console.Write("Enter values in grams: ");
            g = float.Parse(Console.ReadLine());

            // convert grams to kilograms
            k = g / 1000;

            // display result upto 3 decimal places
            Console.WriteLine("{0:F3} grams = {1:F3} kilograms", g, k);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter values in grams: 4750

4750.000 grams = 4.750 kilograms