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.
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. C program to convert Kilograms to Grams
/*************************************** alphabetacoder.com C program to convert Kilograms to Grams ****************************************/ #include <stdio.h> int main() { // declare variables float g, k; // take input printf("Enter values in kilograms: "); scanf("%f", & k); // convert kilograms to grams g = k * 1000; // display result printf("%f kilograms = %f grams", k, g); return 0; }
Output
Case 1:
Enter values in kilograms: 0.565
0.565000 kilograms = 565.000000 grams
Case 2:
Enter values in kilograms: 2.010
2.010000 kilograms = 2010.000000 grams
4.2. C program to convert Grams to Kilograms
/*************************************** alphabetacoder.com C program to convert Grams to Kilograms ****************************************/ #include <stdio.h> int main() { // declare variables float g, k; // take input printf("Enter values in grams: "); scanf("%f", & g); // convert grams to kilograms k = g / 1000; // display result printf("%f grams = %f kilograms", g, k); return 0; }
Output
Case 1:
Enter values in grams: 1
1.000000 grams = 0.001000 kilograms
Case 2:
Enter values in grams: 2350
2350.000000 grams = 2.350000 kilograms