Python 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) 1.5 kilograms = (1.5 x 1000) grams = 1500 grams
(iii) 4.350 kilograms = (4.350 x 1000) grams = 4350 grams
Convert gram to kilogram:
(i)1 gram = (1 / 1000) kilograms = 0.001 kilograms
(ii)700 grams = (700 / 1000) kilograms = 0.700 kilograms
(iii)2250 grams = (2250 / 1000) kilograms = 2.250 kilograms
The following section covers the Python programs to solve the above problems are along with the algorithm, pseudocode and time-complexity of the programs.
Page content(s):
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.
3.
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.
3.
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. Python program to convert Kilograms to Grams
############################################### # alphabetacoder.com # Python program to convert Kilograms to Grams ############################################### # take input k = float(input("Enter values in kilograms: ")) # convert kilograms to grams g = k * 1000 # display result print(k, "kilograms =", g, "grams")
Output
Enter values in kilograms: 4.255
4.255 kilograms = 4255.0 grams
4.2. Python program to convert Grams to Kilograms
############################################### # alphabetacoder.com # Python program to convert Grams to Kilograms ############################################### # take input g = float(input("Enter values in grams: ")) # convert grams to kilograms k = g / 1000 # display result print(g, "grams =", k, "kilograms")
Output
Enter values in grams: 5775
5775.0 grams = 5.775 kilograms