This blog is under construction

Sunday 7 July 2013

C program to convert kilograms to pounds and grams

Write a C program to convert kilograms to pounds and grams.


  #include <stdio.h>
  #define POUNDTOGRAM 453.592
  #define KGTOGRAM 1000

  int main() {
        float kilogram, pound, gram;

        /* get the no of kilograms from the user */
        printf("Enter the number of kilograms:");
        scanf("%f", &kilogram);

        printf("%.2f kilograms = ", kilogram);
        gram = kilogram * KGTOGRAM;

        /* calculate the number of pounds */
        if (gram >= POUNDTOGRAM) {
                while (1) {
                        pound++;
                        gram = gram - POUNDTOGRAM;
                        if (gram < POUNDTOGRAM)
                                break;
                }
        }

        /* print the no of pounds and grams */
        printf("%.2f pounds and %.2f grams\n", pound, gram);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of kilograms:4
  4.00 kilograms = 8.00 pounds and 371.26 grams





See Also:

No comments:

Post a Comment