Computer Science Of Engineering

Simple and practical computer science tutorials, programming guides, and engineering concepts for students and beginners.

Breaking

Friday, January 2, 2026

Conversion of Fahrenheit to Celsius and vice versa in C

 #include <stdio.h>


int main() {

    int choice;

    float fahrenheit, celsius;


    printf("Temperature Conversion Menu:\n");

    printf("1. Fahrenheit to Celsius\n");

    printf("2. Celsius to Fahrenheit\n");

    printf("Enter your choice (1 or 2): ");

    scanf("%d", &choice);


    if (choice == 1) {

        // Fahrenheit to Celsius

        printf("Enter temperature in Fahrenheit: ");

        scanf("%f", &fahrenheit);

        celsius = (fahrenheit - 32) * 5 / 9;

        printf("%.2f Fahrenheit = %.2f Celsius\n", fahrenheit, celsius);

    } else if (choice == 2) {

        // Celsius to Fahrenheit

        printf("Enter temperature in Celsius: ");

        scanf("%f", &celsius);

        fahrenheit = (celsius * 9 / 5) + 32;

        printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);

    } else {

        printf("Invalid choice!\n");

    }


    return 0;

}






No comments:

Post a Comment