Skip to content

Instantly share code, notes, and snippets.

@dave-martinez
Created January 26, 2017 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave-martinez/2280d415a776a16d0a02f5072dc567e7 to your computer and use it in GitHub Desktop.
Save dave-martinez/2280d415a776a16d0a02f5072dc567e7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
main()
{
printf("Units converter:\n\n");
printf("[k] Kilogram (kg)\n[p] Pounds (lbs)\n[o] Ounces(o)\n");
char firstUnit = ' ';
char secondUnit = ' ';
float initialValue, convertedValue;
printf("Select unit initial: ");
scanf("%c", &firstUnit);
printf("Select unit final: ");
scanf(" %c", &secondUnit);
printf("Enter value: ");
scanf("%f", &initialValue);
switch(firstUnit)
{
case 'k':
switch(secondUnit)
{
case 'k': convertedValue = initialValue; break;
case 'p': convertedValue = initialValue * 2.20462; break;
case 'o': convertedValue = initialValue * 35.274; break;
default: printf("Invalid option selected. Re-run the program.");break;
}
break;
case 'p':
switch(secondUnit)
{
case 'k': convertedValue = initialValue * 0.453592; break;
case 'p': convertedValue = initialValue; break;
case 'o': convertedValue = initialValue * 16; break;
default: printf("Invalid option selected. Re-run the program.");break;
}
break;
case 'o':
switch(secondUnit)
{
case 'k': convertedValue = initialValue * 0.0283495; break;
case 'p': convertedValue = initialValue * 0.0625; break;
case 'o': convertedValue = initialValue; break;
default: printf("Invalid option selected. Re-run the program.");break;
}
break;
default: printf("Invalid option selected. Re-run the program.");break;
}
printf("Result: %f",convertedValue);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment