Friday, March 21, 2014

Distance between cities conversion in different units – C Program

Problem Question


The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.

Explanation of Problem


The problem wants the program to ask user for an input. The user enters a number which is interpreted as the distance in km. Our program should be able to convert this distance into the above mentioned units by doing calculations, and displaying the result to the user, since without the output there is no sure-shot way to guarantee the correctness of the result.

Code


#include
/*DISTANCE BETWEEN CITIES*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 21-03-2014**/
int main()
{
    printf("\n\nEnter distance(in km): ");
    float distanceInKm, distanceInM, distanceInFt, distanceInIn, distanceInCm = 0.0;
    scanf("%f", &distanceInKm);
    distanceInM = distanceInKm * 1000;
    distanceInCm = distanceInM * 100;
    distanceInIn = distanceInCm * 2.54;
    distanceInFt = distanceInIn * 12;
    printf("Calculating........\nThe result is:\n\nMeter: %f\nCentimeter: %f\nInches: %f\nFeet: %f\n", distanceInM, distanceInCm, distanceInIn, distanceInFt);
    system("pause");
    return 0;
}

Explanation of Code


#include -> #include calls the C Preprocessor directing it to include the STDIO header file which contains the standard Input Output functions for the C Language. This step is executed before the compilation starts.

int main() -> In ANSI C, we declare main() with return type int. main() is the entry point of the C Program, i.e., the point where the code execution begins.

printf("\n\nEnter distance(in km): "); -> The printf() function of the standard C Library displays the string passed to it on the user’s display. Here we use to prompt the user to enter the distance. Another printf() use appears at the end of the program in the statement:
printf("Calculating........\nThe result is:\n\nMeter: %f\nCentimeter: %f\nInches: %f\nFeet: %f\n", distanceInM, distanceInCm, distanceInIn, distanceInFt);
where we use it to print the result, i.e. display the converted distance on the user’s display.

float distanceInKm, distanceInM, distanceInFt, distanceInIn, distanceInCm = 0.0; -> To make the program more flexible and so that it can accept and display decimal values, I used float instead of int. These variables will be used to hold the input and the results.

scanf("%f", &distanceInKm); -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable ‘distanceInKm’. Since it is a float type variable, we use ‘%f’.

distanceInM = distanceInKm * 1000;
distanceInCm = distanceInM * 100;
distanceInIn = distanceInCm * 2.54;
distanceInFt = distanceInIn * 12;
-> This is the part of the code where the calculation takes place and we store the result in different variables.

Please Note: In this program, we could substitute the lines:

distanceInM = distanceInKm * 1000;
distanceInCm = distanceInM * 100;
distanceInIn = distanceInCm * 2.54;
distanceInFt = distanceInIn * 12;
printf("Calculating........\nThe result is:\n\nMeter: %f\nCentimeter: %f\nInches: %f\nFeet: %f\n", distanceInM, distanceInCm, distanceInIn, distanceInFt);


by the line:

printf("Calculating........\nThe result is:\n\nMeter: %f\nCentimeter: %f\nInches: %f\nFeet: %f\n", distanceInKm * 1000, distanceInKm * 1000 * 100, distanceInKm * 1000 * 100 * 2.54, distanceInKm * 1000 * 100 * 2.54 * 12) ;

This way although the readability of the program is hampered, but we save a nice amount of space since we would no longer need any variables other than ‘distancInKm’.

Output(s)



No comments:

Post a Comment

Need help?