Skip to main content

unit conversion example without recursion

#include <stdio.h>
int main()
{
    int selection;
    float inrusdpoundskgmetercm;
    printf("please press 1 to convert USD to INR\n");
    printf("please press 2 to convert POUNDS to KG\n");
    printf("please press 3 to convert CM to METER\n");
    scanf("%d", &selection);

   //im using if/else condition..i can also use switch statement but it will better for me xD

    if (selection == 1)
    {
        printf("please enter USD amount to convert into INR\n");
        scanf("%f", &inr);
        usd = inr * 75.36;
        printf("INR = %f"usd);
        printf("Thanks for using conversion program\n"); //this is my credit
    }

    else if (selection == 2)
    {
        printf("please enter kg to convert into pounds\n");
        scanf("%f", &kg);
        pounds = kg * 2.205;
        printf("pounds = %f"pounds);
        printf("Thanks for using conversion program\n"); //this is my credit
    }

    else if (selection == 3)
    {
        printf("please enter meter to convert into cm\n");
        scanf("%f", &cm);
        meter = cm * 100;
        printf("cm = %f"meter);
        printf("\nThanks for using conversion program\n"); //this is my credit
    }
    return 0;
}

// another best way
#include <stdio.h> int main() { char input; float kmsToMiles = 0.621371; float inchesToFoot = 0.0833333; float cmsToInches = 0.393701; float poundToKgs = 0.453592; float inchesToMeters = 0.0254; float first, second; while (1) { printf("Enter the input character. q to quit\n"); printf("1.kms to miles\n"); printf("2.inches to foot\n"); printf("3.cms to inches\n"); printf("4.pound to kgs\n"); printf("5.inches to meters\n"); scanf(" %c", &input); // printf("The character is '%c'", input); switch (input) { case 'q': printf("Quitting the program..."); goto end; break; case '1': printf("Enter quantity in terms of first unit\n"); scanf("%f", &first); second = first * kmsToMiles; printf("%.2f Kms is equal to %.2f Miles\n\n\n", first, second); // here 2 represent that we will only get 2 number after decimal break; case '2': printf("Enter quantity in terms of first unit\n"); scanf("%f", &first); second = first * inchesToFoot; printf("%.3f Inches is equal to %.3f Foot\n", first, second); // here 3 represent that we will only get 3 number after decimal break; case '3': printf("Enter quantity in terms of first unit\n"); scanf("%f", &first); second = first * cmsToInches; printf("%.1f Cms is equal to %.1f Inches\n", first, second); // here 1 represent that we will only get 1 number after decimal break; case '4': printf("Enter quantity in terms of first unit\n"); scanf("%f", &first); second = first * poundToKgs; printf("%.4f Pounds is equal to Kgs %.4f \n", first, second); // here 4 represent that we will only get 4 number after decimal break; case '5': printf("Enter quantity in terms of first unit\n"); scanf("%f", &first); second = first * inchesToMeters; printf("%.3f inches is equal to %.3f meters \n", first, second); // here 3 represent that we will only get 3 number after decimal break; default: printf("In defult now"); break; } } end: return 0; }



Comments