ccplusplus.com
Learn C, C++ Concepts
Thursday, September 29, 2011
convert miles to Inches
/*********************************************************************** * File : distance-converter.c * Author : Saurabh Gupta * Desc : c program to convert miles to Inches * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 08:25 29 September 2011 * Note : optimize the code remove the function call ************************************************************************/ #include <stdio.h> /* * local function signatures */ void MilesToAll (double x); /* * global variables */ double dMiles, dKilometer, dMeter, dCentiMeter, dInche, dFeet; int main(){ printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX Distance Converter XXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); int nLoop = 1; int nDistanceChoice; // double dMiles, dKilometer, dMeter, dCentiMeter, dInche, dFeet; while(nLoop == 1){ printf("\nPlease choose a conversion:\n" "1: Miles to Kilometer\n" "2: Miles to Meter\n" "3: Miles to Feet\n" "4: Miles to Inches\n" "5: Miles to Centimeter\n" "6: Exit Program\n"); scanf("%i", &nDistanceChoice); switch (nDistanceChoice){ case 1: printf("Enter the number of Miles : "); scanf("%lf", &dMiles); MilesToAll(dMiles); printf("XXXX\n%.2lf Miles = %.2lf kilometers\nXXXX\n", dMiles, dKilometer); break; case 2: printf("Enter the number of Miles : "); scanf("%lf", &dMiles); MilesToAll(dMiles); printf("XXXX\n%.2lf Miles = %.2lf Meter\nXXXX\n", dMiles, dMeter); break; case 3: printf("Enter the number of Miles : "); scanf("%lf", &dMiles); MilesToAll(dMiles); printf("XXXX\n%.2lf Miles = %.2lf Feet\nXXXX\n", dMiles, dFeet); break; case 4: printf("Enter the number of Miles : "); scanf("%lf", &dMiles); MilesToAll(dMiles); printf("XXXX\n%.2lf Miles = %.2lf Inches\nXXXX\n", dMiles, dInche); break; case 5: printf("Enter the number of Miles : "); scanf("%lf", &dMiles); MilesToAll(dMiles); printf("XXXX\n%.2lf Miles = %.2lf Centimeter\nXXXX\n", dMiles, dCentiMeter); break; case 6: nLoop = 0; break; default: printf("Invalid Entry, please try again."); break; } } printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * conversion function */ void MilesToAll (double x) { dKilometer = dMiles * 1.609344; dMeter = dKilometer*1000; dCentiMeter = dMeter*100; dInche = dCentiMeter/2.54; dFeet = dInche/12; } /* * OUTPUT * [sgupta@rhel54x64 distance-conversion]$ gcc distance-converter.c -o distance-converter [sgupta@rhel54x64 distance-conversion]$ ./distance-converter XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX Distance Converter XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 1 Enter the number of Miles : 15 XXXX 15.00 Miles = 24.14 kilometers XXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 2 Enter the number of Miles : 10 XXXX 10.00 Miles = 16093.44 Meter XXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 3 Enter the number of Miles : 20 XXXX 20.00 Miles = 105600.00 Feet XXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 4 Enter the number of Miles : 30 XXXX 30.00 Miles = 1900800.00 Inches XXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 5 Enter the number of Miles : 50 XXXX 50.00 Miles = 8046720.00 Centimeter XXXX Please choose a conversion: 1: Miles to Kilometer 2: Miles to Meter 3: Miles to Feet 4: Miles to Inches 5: Miles to Centimeter 6: Exit Program 6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 distance-conversion]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment