Code Snippets

Unit Conversion

                                    
        #include <stdio.h>
        
        float inMiles(float num)
        {
            return (num * 0.621371);
        }
        
        float inFoots(float num)
        {
            return (num / 12);
        }
        
        float inInches(float num)
        {
            return (num / 2.54);
        }
        
        float inKgs(float num)
        {
            return (num / 2.20462);
        }
        
        float inMeters(float num)
        {
            return (num / 39.3701);
        }
        
        int main()
        {
            int a;
            float b, result;
        
            while (a != 0)
            {
                printf("\n******Main Menu******\n");
                printf("1 - kms to miles\n");
                printf("2 - inches to foots\n");
                printf("3 - cms to inches\n");
                printf("4 - pounds to kgs\n");
                printf("5 - inches to meters\n");
                printf("0 - exit\n");
                printf("*********************\n");
        
                printf("\nChoose the option: ");
                scanf("%d", &a);
        
                if (a == 1)
                {
                    printf("Enter the value: ");
                    scanf("%f", &b);
                    result = inMiles(b);
                    printf("\n%.3f kms in miles is %.3f\n", b, result);
                }
                else if (a == 2)
                {
                    printf("Enter the value: ");
                    scanf("%f", &b);
                    result = inFoots(b);
                    printf("\n%.3f inches in foots is %.3f\n", b, result);
                }
                else if (a == 3)
                {
                    printf("Enter the value: ");
                    scanf("%f", &b);
                    result = inInches(b);
                    printf("\n%.3f cms in inches is %.3f\n", b, result);
                }
                else if (a == 4)
                {
                    printf("Enter the value: ");
                    scanf("%f", &b);
                    result = inKgs(b);
                    printf("\n%.3f pounds in kgs is %.3f\n", b, result);
                }
                else if (a == 5)
                {
                    printf("Enter the value: ");
                    scanf("%f", &b);
                    result = inMeters(b);
                    printf("\n%.3f inches in meters is %.3f\n", b, result);
                }
                else if (a == 0)
                {
                    break;
                }
                else
                {
                    printf("Enter a valid option from the main menu!!!");
                }
            }
        
            printf("\nSee you soon, have a nice day :)");
        
            return 0;
        }