C program to convert kilograms to grams

In this article, we will learn how to create a program in C that will ask the user to enter a weight in kilograms and then convert and print its equivalent weight in grams. But, before we get into the program, let's first understand the difference between a kilogram and a gram.

The Kilogram and Gram Relationship

The relationship between a kilogram and a gram is:

1 kilogram = 1000 grams

Kilogram is abbreviated or represented as kg, whereas gram is represented as g. As a result, the relationship between these two can be written as follows:

1 kg = 1000 g

Now let's move on to the program.

Kilogram to Gram Conversion in C

The question is: write a program in C that converts the given weight (by the user at run-time) from kilograms to grams. 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;

Because the program was written in the Code::Blocks IDE, here is a snapshot of the sample run after a successful build and run:

c program convert kilogram to gram

Supply any input or weight in kilograms (say 5) and press the ENTER key to see the same weight in grams as shown in the second snapshot given here:

convert kilogram to gram c

Let's take another sample run. Enter the weight in kilograms, say 24.78, to see its equivalent weight in grams:

convert kilogram to gram program c

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

C Quiz

« Previous Program Next Program »

Liked this post? Share it!