C Program for Temperature Conversion in Different Scales विभिन्न Temperature Scales में Conversion करने का C Program
Temperature can be measured using different scales such as Celsius, Kelvin, Fahrenheit, and Reaumur. In C language, we can use the switch statement to convert temperature from one scale to the other three scales.
Temperature को Celsius, Kelvin, Fahrenheit और Reaumur जैसी अलग-अलग scales में मापा जा सकता है। C भाषा में switch statement का उपयोग करके एक scale की temperature को अन्य तीन scales में convert किया जा सकता है।
Temperature Conversion Formula
| Conversion | Formula |
|---|---|
| Celsius to Kelvin | K = C + 273.15 |
| Celsius to Fahrenheit | F = (C × 9/5) + 32 |
| Celsius to Reaumur | R = C × 4/5 |
| Fahrenheit to Celsius | C = (F − 32) × 5/9 |
| Kelvin to Celsius | C = K − 273.15 |
| Reaumur to Celsius | C = R × 5/4 |
C Program
#include <stdio.h>int main(void){float ct, kt, ft, rt, temp;char choice;printf("Temperature Conversion Program\n");printf("Enter scale: C/K/F/R\n");printf("Enter your choice: ");if (scanf(" %c", &choice) != 1){printf("Error: Invalid scale input\n");return 1;}switch (choice){case 'c': case 'C':printf("Enter Temperature in Celsius: ");if (scanf("%f", &temp) != 1){printf("Error: Please enter a numeric temperature\n");return 1;}if (temp < -273.15){printf("Error: Temperature cannot be below absolute zero\n");return 1;}ct = temp;kt = ct + 273.15;ft = (ct * 9.0 / 5.0) + 32;rt = ct * 4.0 / 5.0;break;case 'k': case 'K':printf("Enter Temperature in Kelvin: ");if (scanf("%f", &temp) != 1){printf("Error: Please enter a numeric temperature\n");return 1;}if (temp < 0){printf("Error: Kelvin temperature cannot be negative\n");return 1;}kt = temp;ct = kt - 273.15;ft = (ct * 9.0 / 5.0) + 32;rt = ct * 4.0 / 5.0;break;case 'f': case 'F':printf("Enter Temperature in Fahrenheit: ");if (scanf("%f", &temp) != 1){printf("Error: Please enter a numeric temperature\n");return 1;}if (temp < -459.67){printf("Error: Temperature cannot be below absolute zero\n");return 1;}ft = temp;ct = (ft - 32) * 5.0 / 9.0;kt = ct + 273.15;rt = ct * 4.0 / 5.0;break;case 'r': case 'R':printf("Enter Temperature in Reaumur: ");if (scanf("%f", &temp) != 1){printf("Error: Please enter a numeric temperature\n");return 1;}if (temp < -218.52){printf("Error: Temperature cannot be below absolute zero\n");return 1;}rt = temp;ct = rt * 5.0 / 4.0;kt = ct + 273.15;ft = (ct * 9.0 / 5.0) + 32;break;default:printf("Error: Wrong scale choice\n");return 1;}printf("\nTemperature in Different Scales:\n");printf("Celsius = %.2f\n", ct);printf("Kelvin = %.2f\n", kt);printf("Fahrenheit = %.2f\n", ft);printf("Reaumur = %.2f\n", rt);return 0;}
Output
Example 1: Valid Input
Temperature Conversion ProgramEnter scale: C/K/F/REnter your choice: CEnter Temperature in Celsius: 100Temperature in Different Scales:Celsius = 100.00Kelvin = 373.15Fahrenheit = 212.00Reaumur = 80.00
Example 2: Invalid Scale
Enter your choice: XError: Wrong scale choice
Example 3: Non-numeric Temperature
Enter your choice: CEnter Temperature in Celsius: abcError: Please enter a numeric temperature
Example 4: Temperature Below Absolute Zero
Enter your choice: KEnter Temperature in Kelvin: -10Error: Kelvin temperature cannot be negative
Explanation
scanf()return value is checked to detect invalid input.switch(choice)selects the appropriate conversion formula.defaulthandles an invalid scale choice.The program checks whether the entered temperature is below absolute zero.
If an error occurs, the program displays an error message and terminates.
scanf() के return value को check करके invalid input का पता लगाया जाता है। switch(choice) सही conversion formula चुनता है। यदि scale गलत है, temperature numeric नहीं है या absolute zero से कम है, तो program error message display करता है।
Modern C Compatibility Note
This program uses standard C and is compatible with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().
This program demonstrates temperature conversion using the switch statement, along with input validation and error handling in C.
यह program C भाषा में switch statement का उपयोग करके temperature conversion के साथ input validation और error handling करना सिखाता है।
Comments
Post a Comment