Temperature Conversion Program in C सीखना हर C programming beginner के लिए एक बेहद जरूरी कदम है, क्योंकि यह program बेसिक concepts जैसे switch statement, floating point variables, और formatted output को एक साथ practical तरीके से समझने का मौका देता है। अगर आप C language में अभी शुरुआत कर रहे हैं, तो इस तरह के real-world programs आपकी coding thinking को मजबूत बनाते हैं।
इस पोस्ट में हम step-by-step देखेंगे कि Celsius और Fahrenheit के बीच temperature convert करने वाला program कैसे लिखा जाता है। आप समझेंगे कि user से input कैसे लिया जाता है, conversion formula कैसे काम करता है, और output को सही format में कैसे दिखाया जाता है।
पोस्ट के अंत तक आपके पास न सिर्फ working code होगा, बल्कि यह भी पूरी तरह clear होगा कि program की हर line क्यों और कैसे काम करती है। चाहे आप exam की तैयारी कर रहे हों या कोई mini project बना रहे हों, यह guide आपके काम आएगी।
Table of Contents
Temperature Conversion क्या होता है और C में इसे क्यों सीखें?
Temperature Conversion का मतलब है एक temperature unit को दूसरी unit में बदलना। सबसे common conversion है Celsius से Fahrenheit और Fahrenheit से Celsius। रोजमर्रा की जिंदगी में weather apps, medical thermometers, और cooking recipes में इस conversion की जरूरत होती है।
C programming में यह program इसलिए important है क्योंकि इसमें कई fundamental concepts एक साथ practice होते हैं:
- switch statement का उपयोग multiple choices handle करने के लिए
- float data type जो decimal values store करता है
- scanf() और printf() से input-output handle करना
- Mathematical expressions के जरिए formula implement करना
जब आप यह program खुद लिखकर run करते हैं, तो इन सभी चीजों की समझ automatically गहरी हो जाती है।
Program Statement
हमें एक ऐसा C program बनाना है जो:
- User को दो विकल्प दिखाए — Celsius to Fahrenheit या Fahrenheit to Celsius
- User की choice के अनुसार temperature value input ले
- सही formula से calculation करे
- Result को दो decimal places तक screen पर दिखाए
- अगर user गलत choice दे, तो उसे “Invalid choice!” का message मिले
Conversion Formulas
Temperature conversion के लिए दो standard formulas होते हैं:
Celsius से Fahrenheit
F = (C × 9/5) + 32
उदाहरण: अगर Celsius = 25, तो
F = (25 × 9/5) + 32 = 45 + 32 = 77°F
Fahrenheit से Celsius
C = (F − 32) × 5/9
उदाहरण: अगर Fahrenheit = 98, तो
C = (98 − 32) × 5/9 = 66 × 5/9 = 36.67°C
ये formulas internationally accepted हैं और physics तथा meteorology दोनों में use होते हैं।
Program Code
नीचे Temperature Conversion Program in C का पूरा code दिया गया है:
#include <stdio.h>
int main() {
float celsius, fahrenheit; // Variables to store temperatures
int choice; // Variable to store the user's choice
// Display the conversion menu
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice: ");
scanf("%d", &choice);
// Perform the selected temperature conversion
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
Case 1 — Celsius to Fahrenheit:
Case 2 — Fahrenheit to Celsius:
Code Explanation — Line by Line
अब हम program की हर important line को विस्तार से समझते हैं ताकि कोई भी confusion न रहे।
1. Header File
#include <stdio.h>
यह line stdio.h header file को include करती है। इसमें printf() और scanf() जैसे functions defined होते हैं। इसके बिना input-output संभव नहीं होता।
2. Variables Declare करना
float celsius, fahrenheit;
int choice;
- float type के variables इसलिए बनाए गए हैं क्योंकि temperature में decimal values आती हैं, जैसे 36.67।
- int choice user की menu selection store करने के लिए है।
3. Menu Display और Input
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter your choice: ");
scanf("%d", &choice);printf() से menu दिखाया जाता है और scanf(“%d”, &choice) से user की integer choice ली जाती है।
4. Switch Statement
switch(choice) के अंदर तीन cases हैं:
- case 1: Celsius की value लेता है, formula apply करता है, और Fahrenheit में result दिखाता है।
- case 2: Fahrenheit की value लेता है, formula apply करता है, और Celsius में result दिखाता है।
- default: अगर user 1 या 2 के अलावा कुछ और डाले, तो “Invalid choice!” print होता है।
5. Formatted Output
printf(“%.2f Celsius = %.2f Fahrenheit\n”, celsius, fahrenheit);
%.2f का मतलब है कि floating point number को 2 decimal places तक दिखाओ। यह output को professional और readable बनाता है।
6. break Statement
हर case के अंत में break; लिखा है। यह जरूरी है ताकि एक case execute होने के बाद program अगले case में न जाए। इसे fall-through से बचना कहते हैं।
Common Mistakes जो Beginners करते हैं
Temperature Conversion Program in C लिखते समय beginners अक्सर कुछ गलतियाँ करते हैं, जिनसे बचना चाहिए:
- int की जगह float न लेना — अगर celsius और fahrenheit को int declare करें तो decimal result गलत आएगा।
- break भूल जाना — switch में break न होने पर सभी cases एक-के-बाद-एक execute हो जाते हैं।
- Formula में bracket गलत लगाना — जैसे (fahrenheit – 32) * 5 / 9 में bracket जरूरी है, वरना calculation गलत होगी।
- scanf में & भूलना — scanf(“%f”, celsius) गलत है; सही है scanf(“%f”, &celsius)।
इस Program से क्या सीखते हैं?
यह एक simple program है, लेकिन इससे मिलने वाले concepts बहुत powerful हैं:
- Conditional logic — switch statement से multiple conditions handle करना
- Data types — float vs int का सही चुनाव
- User interaction — input लेना और formatted output देना
- Mathematical operations — C में arithmetic expressions लिखना
- Error handling — default case से invalid input manage करना
ये सभी concepts आगे चलकर बड़े और complex programs बनाने में काम आते हैं।
Conclusion ( निष्कर्ष )
Temperature Conversion Program in C एक बेहतरीन starting point है जो C language के core concepts को practically समझने में मदद करता है। इस program को खुद type करके run करने से switch statement, float variables, और formatted output की समझ बहुत ज्यादा मजबूत हो जाती है। साथ ही, real mathematical formulas को code में implement करने का confidence भी बढ़ता है।
अगर आप सच में programming सीखना चाहते हैं, तो सिर्फ पढ़ने से काम नहीं चलेगा। इस code को अपने compiler में लिखें, run करें, और फिर इसे Kelvin conversion जोड़कर extend करने की कोशिश करें। जितना ज्यादा practice करेंगे, उतना ज्यादा सीखेंगे। अगर कोई doubt हो तो नीचे comment करें — हम जरूर मदद करेंगे।
