C: Convert Inches To Yards, Feet, And Inches.


This exercise is taken from Chapter 2 of the book, "Beginning C", 5th edition. This is my solution to the problem.

https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_1/exercise2_1.c

// Exercise 2-1

// Write a program that prompts the user to enter a distance in inches and
// then outputs that distance in yards, feet, and inches.

// There are 12 inches in a foot and 3 feet in a yard.

#include <stdio.h>

int main(int argc, char* argv[])
{
    // Constants for conversion process:
    const int inches_per_foot = 12;
    const int feet_per_yard   = 3;

    // Variables for user input and calculations:
    int input = 0, inches = 0, yards = 0, feet = 0;

    // Prompt user for input in inches:
    printf("Enter number of inches for conversion to yards, feet, inches: ");
    scanf(" %d", &input);

    inches = input;

    // Convert inches into yards, feet, inches:
    feet   =  inches / inches_per_foot;
    yards  =  feet   / feet_per_yard;

    feet   %= feet_per_yard;    // Remainder of feet!
    inches %= inches_per_foot;  // Remainder of inches!

    // Display result:
    printf("User's input: %d inches.\n", input);
    printf("%d yards, %d feet, %d inches.\n", yards, feet, inches);

    return 0;
}
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_1/exercise2_1.txt
==982== Memcheck, a memory error detector
==982== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==982== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==982== Command: ./exercise2_1
==982==
Enter number of inches for conversion to yards, feet, inches: 200
User's input: 200 inches.
5 yards, 1 feet, 8 inches.
==982== 
==982== HEAP SUMMARY:
==982==     in use at exit: 0 bytes in 0 blocks
==982==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==982==
==982== All heap blocks were freed -- no leaks are possible
==982==
==982== For lists of detected and suppressed errors, rerun with: -s
==982== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Popular posts from this blog

C programming and relevancy

Shakespeare AI: My lady is more beauteous than a rose.

C: Temperature Conversion With Main Repeat