C program for temperature conversion using switch case


 /* C program to convert Fahrenheit into Celsius and reverse using switch case */

#include"stdio.h"

float ctof(float c);        // Function declaration / prototype
float ftoc(float f);

void main()
{

    int num;
    float cel, fah, result;
start : printf("\n\n\t\t\t Degree Conversion \n");
    printf("\t\t\t ````````````````` \n");
    printf("1. From Celsius to Fahrenheit. \n");
    printf("2. From Fahrenheit to Celsius. \n");
    printf("3. Exit \n");
    printf("Enter your choice: ");
    scanf("%d", &num);
    switch(num)
    {
        case 1:
            printf("Enter the temperature in Celsius: ");
            scanf("%f", &cel);
            result = ctof(cel);       //function call
            printf("Temperature in Fahrenheit is : %.2f \n", result);
            break;
        case 2:
            printf("Enter the temperature in Fahrenheit: ");
            scanf("%f", &fah);
            result = ftoc(fah);
            printf("Temperature in Celsius is : %.2f \n", result);
            break;
        case 3:
            exit(0);
        default:
            printf("\n\nPlease choose anyone option.\n");
            goto start;
    }


}
//   function definition
float ctof(float c)                    
{

    float f;
    f = (1.8*c) + 32;
    return f;

}

float ftoc(float f)
{

    float c;
    c = 0.56*(f-32);
    return c;

}

Comments

Post a Comment

Popular Posts