C Language program to convert kilograms to grams

Here’s a C program that converts between kilograms and grams:

				
					#include <stdio.h>

int main() {
    double kilograms, grams;

    printf("Enter the weight in kilograms: ");
    scanf("%lf", &kilograms);

    grams = kilograms * 1000;

    printf("Weight in grams: %.2lf\n", grams);

    return 0;
}
				
			
Output
C program to convert kilograms to grams
  • Explanation:

    • The program starts by including the stdio.h header file, which provides the necessary functions for input and output operations in C.
    • Inside the main function, we declare the variables kilograms and grams as doubles to store the respective values.
    • We prompt the user to enter the weight in kilograms using a printf statement, and we read their input using a scanf statement.
    • The weight is converted from kilograms to grams by multiplying it by 1000, since there are 1000 grams in one kilogram.
    • Finally, we use a printf statement to display the weight in grams with two decimal places.

    When you run this program, it will prompt you to enter the weight in kilograms. After you provide the input, it will calculate and display the equivalent weight in grams.