Temperature Conversion Program in C – Complete Guide with Code and Explanation

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 आपके काम आएगी।

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 बनाना है जो:

  1. User को दो विकल्प दिखाए — Celsius to Fahrenheit या Fahrenheit to Celsius
  2. User की choice के अनुसार temperature value input ले
  3. सही formula से calculation करे
  4. Result को दो decimal places तक screen पर दिखाए
  5. अगर 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:

▶️ Output Result
1. Celsius to Fahrenheit 2. Fahrenheit to Celsius Enter your choice: 1 Enter temperature in Celsius: 25 25.00 Celsius = 77.00 Fahrenheit

Case 2 — Fahrenheit to Celsius:

▶️ Output Result
1. Celsius to Fahrenheit 2. Fahrenheit to Celsius Enter your choice: 2 Enter temperature in Fahrenheit: 98.60 98.60 Fahrenheit = 37.00 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 logicswitch statement से multiple conditions handle करना
  • Data typesfloat 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 करें — हम जरूर मदद करेंगे।

Frequently Asked Questions

Q 1: C में temperature conversion के लिए float क्यों use करते हैं, int क्यों नहीं?
Ans: Temperature values अक्सर decimal में होती हैं, जैसे 36.67°C या 98.6°F। अगर int use करें तो decimal part cut हो जाता है और result गलत आता है। इसलिए float या double data type use करना जरूरी है।
Q 2: switch statement की जगह if-else भी use हो सकता है?
Ans: हाँ, बिल्कुल। if-else से भी यही program लिखा जा सकता है। लेकिन switch statement code को ज्यादा readable और organized बनाता है, खासकर जब multiple fixed choices हों। दोनों approaches technically सही हैं।
Q 3: Program में break statement क्यों जरूरी है?
Ans: switch statement में अगर break नहीं लिखा जाए, तो एक case match होने के बाद program उसके नीचे के सभी cases भी execute करता रहता है — इसे fall-through behavior कहते हैं। इससे बचने के लिए हर case के अंत में break लिखा जाता है।
Q 4: क्या इस program को Kelvin conversion के लिए भी extend कर सकते हैं?
Ans: हाँ। switch में एक case 3 और case 4 add करके Kelvin से Celsius या Celsius से Kelvin conversion भी जोड़ा जा सकता है। Kelvin formula है: K = C + 273.15। यह एक अच्छी practice exercise है।
Q 5: %.2f का क्या मतलब है?
Ans: %.2f एक format specifier है जो printf() में use होता है। इसका मतलब है कि floating point number को दो decimal places तक दिखाओ। जैसे 36.6666… को 36.67 के रूप में print करो। यह output को clean और readable बनाता है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top