Temperature Conversion Program in C एक ऐसा बेसिक लेकिन बहुत ही उपयोगी program है जो user को Celsius और Fahrenheit के बीच temperature बदलना सिखाता है। इस पोस्ट में आप सीखेंगे कि कैसे switch statement का उपयोग करके user choice के आधार पर अलग-अलग calculation की जाती है। साथ ही आप यह भी समझेंगे कि formula कैसे काम करता है और input और output को format कैसे किया जाता है। यह program beginners के लिए बहुत फायदेमंद है क्योंकि इससे condition handling, floating data type और mathematical expressions की practice होती है।
Program statement
एक ऐसा C प्रोग्राम लिखिए जो user से विकल्प (choice) ले।
- Celsius to Fahrenheit
- Fahrenheit to Celsius
user द्वारा चुने गए विकल्प के अनुसार temperature को एक यूनिट से दूसरी यूनिट में बदलकर result स्क्रीन पर दिखाइए।
Program Code
#include <stdio.h>
int main()
{
/* Temperature Conversion */
float celsius, fahrenheit;
int choice;
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
break;
case 2:
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("%.2f Fahrenheit = %.2f Celsius\n", fahrenheit, celsius);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Program output :
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Enter your choice: 1
Enter temperature in Celsius: 25
25.00 Celsius = 77.00 Fahrenheit
या
Enter your choice: 2
Enter temperature in Fahrenheit: 98
98.00 Fahrenheit = 36.67 Celsius
Code Explanation
इस program में सबसे पहले #include <stdio.h> header file शामिल की गई है, जो input और output functions के लिए जरूरी है।
float celsius, fahrenheit; दो floating variables बनाए गए हैं ताकि दशमलव (decimal) वाली values को store किया जा सके।
int choice; यूज़र का choice लेने के लिए integer variable है।
printf() के माध्यम से यूज़र को दो विकल्प दिखाए जाते हैं। scanf(“%d”, &choice); से यूज़र की choice ली जाती है।
इसके बाद switch (choice) statement का उपयोग किया गया है।
- यदि user 1 चुनता है, तो Celsius से Fahrenheit में conversion होता है। Formula है:
F = (C × 9/5) + 32 - यदि user 2 चुनता है, तो Fahrenheit से Celsius में conversion होता है। Formula है:
C = (F − 32) × 5/9
printf(“%.2f”, value) का उपयोग output को दो decimal places तक दिखाने के लिए किया गया है।
अगर user 1 या 2 के अलावा कोई और संख्या डालता है, तो default case चलकर “Invalid choice!” दिखाता है।
यह program beginners को switch statement, mathematical calculation और formatted output की समझ मजबूत करने में मदद करता है।
