C program to convert temperature from celsius to fahrenheit and vice versa
// Online C compiler to run C program onlin #include<stdio.h> #include<stdlib.h> float Cel(float fahrenheit); float Fah(float celsius); int main() { float celsius, fahrenheit; int choice; printf("Enter the choice:"); scanf("%d", &choice); switch(choice) { case 1: printf("Convert the Temperature from Celsius to Farenheit:"); printf("Enter the Temperature in Celsius:") ; scanf("%f", &celsius);
fahrenheit=Cel(celsius); printf("\n Fahrenheit=%.2f", fahrenheit); break; case 2: printf("Convert the Temperature from Farenheit to Celsius:"); printf("Enter the Temperature in Fahrenheit:"); scanf("%f", &fahrenheit ); celsius=Fah(fahrenheit); printf("\n Celsius=%.2f",celsius); break; default: exit(0); } return 0; } float Cel( float celsius) { float fahrenheit; fahrenheit=(celsius*1.8)+32; return fahrenheit; } float Fah(float fahrenheit) { float celsius; celsius=(fahrenheit-32)/1.8; return celsius; }
Output:
Enter the choice:1
Convert the Temperature from Celsius to Farenheit:
Enter the Temperature in Celsius:23
Fahrenheit=73.40
Explanation:
We use two user-defined functions in the program.
function 'Cel' and function 'Fah'. Function 'Cel' is used to calculate the conversion from Celsius to Fahrenheit. Function 'Fah' is used to calculate the conversion from Fahrenheit to celsius.
Post a Comment
Post a Comment