C Program to Convert Kilograms to Grams

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.

Kilogram and Gram Relation

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.

Kilogram to Gram in C

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:

int main()
    float kg, g;
    printf("Enter weight in Kilogram: ");
    scanf("%f", &kg);
    g = kg*1000;
    printf("Equivalent weight in Gram = %0.2f", g);
    return 0;

The program was written under Code::Blocks IDE, therefore after successful build and run, here is the snapshot of the sample run:

c program convert kilogram to gram

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:

convert kilogram to gram c

Let's take another sample run. Enter weight in kilogram say 24.78 to see its equivalent weight in gram:

convert kilogram to gram program c

Here are some of the main steps used in above program:

  • Receive weight input (in Kilogram) from user at run-time
  • As 1 kilogram is equal to 1000 gram
  • Therefore to convert 1 kg into gram, we have to multiply the given value (in Kilogram) with 1000 to get the same weight value in gram
  • That is kg*1000 (kg is the value of weight given by user in kilogram) gets initialized to any variable say g (holds equivalent weight in gram)
  • Print the value of g at last of the program
  • For example, if user enters weight in kilogram as 12.43, therefore kg*1000 or 12.43*1000 or 12430 gets initialized to g
  • Print the value of g which will be 12430 as output
  • The %0.2f format specifier is used to print the value upto to 2 decimal places only

C Online Test

« Previous Program Next Program »