Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.