In this tutorial, we will learn about how to create a program in C that will ask from user to enter weight in Kilogram to convert and print its equivalent weight in gram. But before going through the program given below, let's first understand about the relation between kilogram and gram.
The relation between kilogram and gram is:
1 kilogram = 1000 gram
Kilogram is abbreviated or represented as kg, whereas gram is represented as g. Therefore the relation between these two can be written as:
1 kg = 1000 g
Now let's move on to the program.
The question is, write a program in C that converts the given weight (by user at run-time) from kilogram to gram. The answer to this question is given below:
// Kilogram into Gram Conversion in C // -----------codescracker.com----------- #include<stdio.h> #include<conio.h> int main() { float kg, g; printf("Enter weight in Kilogram: "); scanf("%f", &kg); g = kg*1000; printf("Equivalent weight in Gram = %0.2f", g); getch(); return 0; }
The program was written under Code::Blocks IDE, therefore after successful build and run, here is the snapshot of the sample run:
Supply any input or weight in kilogram (say 5) and press ENTER
key to see the same weight in gram as shown in
the second snapshot given here:
Let's take another sample run. Enter weight in kilogram say 24.78 to see its equivalent weight in gram:
Here are some of the main steps used in above program: