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

ConversionFormula
Celsius to KelvinK = C + 273.15
Celsius to FahrenheitF = (C × 9/5) + 32
Celsius to ReaumurR = C × 4/5
Fahrenheit to CelsiusC = (F − 32) × 5/9
Kelvin to CelsiusC = K − 273.15
Reaumur to CelsiusC = 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 Program
Enter scale: C/K/F/R
Enter your choice: C
Enter Temperature in Celsius: 100
Temperature in Different Scales:
Celsius = 100.00
Kelvin = 373.15
Fahrenheit = 212.00
Reaumur = 80.00

Example 2: Invalid Scale

Enter your choice: X
Error: Wrong scale choice

Example 3: Non-numeric Temperature

Enter your choice: C
Enter Temperature in Celsius: abc
Error: Please enter a numeric temperature

Example 4: Temperature Below Absolute Zero

Enter your choice: K
Enter Temperature in Kelvin: -10
Error: Kelvin temperature cannot be negative

Explanation

  • scanf() return value is checked to detect invalid input.

  • switch(choice) selects the appropriate conversion formula.

  • default handles 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

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025