In addition to other solutions:
- Don't use
float
unless you have very good reason for. Use double
instead. - Your code should handle incorrect user choice.
- The
cm
to inch
formula in your code is wrong.
Try
#include <stdio.h>
int main()
{
double arg, result;
int choice;
printf("Type 0 to convert km to miles\nType 1 to convert kg to pounds\nType 2 to convert cm to inches\n");
scanf("%d", &choice);
if (choice == 0)
{
printf("Enter km to convert in miles:\n");
scanf("%lf", &arg); result = arg / 1.609;
printf("%f km is equals to %f\n", arg, result);
}
else if (choice == 1)
{
printf("Enter kgs to convert in pounds:\n");
scanf("%lf", &arg);
result = arg * 2.205;
printf("%f kg is equals to %f Pounds\n", arg, result);
}
else if (choice == 2)
{
printf("Enter cm to convert in inches:\n");
scanf("%lf", &arg);
result = arg / 2.54;
printf("%f cm is equals to %f inches\n", arg, result);
}
else
{
printf("Invalid choice\n");
}
return 0;
}