C language calculator program सीखना हर beginner programmer के लिए एक महत्वपूर्ण milestone है, क्योंकि यह एक ऐसा project है जिसमें input/output, arithmetic operations, और decision-making — तीनों concepts एक साथ practice होते हैं। अगर आप C programming सीख रहे हैं और एक practical example ढूंढ रहे हैं जो आपको switch case, user input, और error handling सिखाए, तो यह post आपके लिए बिल्कुल सही है।
इस post में हम step-by-step सीखेंगे कि C language में switch case का उपयोग करके एक fully functional calculator कैसे बनाया जाता है। यह calculator addition, subtraction, multiplication, और division — चारों operations perform कर सकता है। साथ ही यह division by zero जैसी common errors को भी handle करता है, जो इसे एक real-world जैसा program बनाता है।
इस tutorial को पढ़ने के बाद आप न केवल एक working calculator program लिखना जान जाएंगे, बल्कि आप switch case statement की internal working, float data type का सही उपयोग, और formatted output के बारे में भी confident हो जाएंगे। तो चलिए शुरू करते हैं।
Table of Contents
C Language Calculator Program क्या होता है?
C language calculator program एक ऐसा console-based application है जो user से दो numbers और एक arithmetic operator input लेता है, और उस operator के आधार पर calculation perform करके result display करता है। यह program C programming के कुछ सबसे fundamental concepts — जैसे variables, scanf, printf, switch-case, और conditional logic — को एक ही जगह practically demonstrate करता है।
Beginners के लिए यह program इसलिए भी बेहतरीन है क्योंकि यह छोटा और readable होने के बावजूद काफी complete है। इसमें error handling भी है, जो इसे एक professional approach देती है।
Program Statement (Problem Definition)
इस program में हमें निम्नलिखित काम करने हैं:
- User से दो floating point numbers input लेना
- User से एक arithmetic operator (+, -, *, /) choose करवाना
- चुने गए operator के आधार पर calculation perform करना
- Result को formatted output (2 decimal places) में display करना
- Division by zero की स्थिति में error message दिखाना
- Invalid operator enter करने पर “Invalid Choice” print करना
Program Code (Switch Case का उपयोग करके)
#include <stdio.h>
int main()
{
float num1, num2;
char ch;
// Take two numbers as input
printf("Enter two numbers (space separated): ");
scanf("%f %f", &num1, &num2);
// Take the arithmetic operator as input
printf("\nEnter your choice (+, -, *, /): ");
scanf(" %c", &ch);
// Perform the selected operation
switch (ch)
{
case '+':
printf("\n%.2f + %.2f = %.2f\n",
num1, num2, num1 + num2);
break;
case '-':
printf("\n%.2f - %.2f = %.2f\n",
num1, num2, num1 - num2);
break;
case '*':
printf("\n%.2f * %.2f = %.2f\n",
num1, num2, num1 * num2);
break;
case '/':
// Check for division by zero
if (num2 != 0)
{
printf("\n%.2f / %.2f = %.2f\n",
num1, num2, num1 / num2);
}
else
{
printf("\nError! Division by zero is not allowed.\n");
}
break;
default:
printf("\nInvalid choice!\n");
}
return 0;
}
Program Output
Code Explanation — Line by Line
1. Header File
#include<stdio.h>
यह C की standard input-output library है। इसके बिना printf() और scanf() functions काम नहीं करते।
2. Variables Declaration
float num1, num2;
char ch;
float data type इसलिए use किया गया है ताकि user decimal numbers भी enter कर सके। अगर int use करते तो 15 / 4 का result 3 आता, 3.75 नहीं। char ch variable operator symbol store करता है।
3. User Input लेना
scanf("%f%f", &num1, &num2);
scanf(" %c", &ch);scanf(” %c”, &ch) में %c से पहले space देना जरूरी है। यह पिछले scanf के बाद buffer में रह गए newline character (\n) को skip करता है। अगर यह space न हो तो ch में newline character store हो जाएगी और program गलत output देगा — यह एक बहुत common beginner mistake है।
4. Switch Case Logic
switch(ch)
{
case '+': ...
case '-': ...
case '*': ...
case '/': ...
default: ...
}Switch statement ch की value को check करता है और matching case execute करता है। यह की तुलना में ज्यादा clean और readable approach है जब multiple fixed values check करनी हों।
5. Break Statement का महत्व
हर case के बाद break; लगाना अनिवार्य है। अगर break न हो तो fall-through होता है — यानी एक case execute होने के बाद control अगले case में चला जाता है बिना condition check किए। यह C का एक unique behavior है जिसे beginners अक्सर misunderstand करते हैं।
6. Division by Zero Check
case '/':
if (num2 != 0)
printf("\n%.2f / %.2f = %.2f", num1, num2, num1/num2);
else
printf("\nError! Division by zero not allowed.");
break;Mathematics में किसी भी number को zero से divide नहीं किया जा सकता। C में अगर integer को zero से divide करें तो program crash हो जाता है। Float division में infinity आ सकती है। इसलिए पहले check करना और error message देना एक अच्छी programming practice है।
7. Formatted Output — %.2f का मतलब
%.2f format specifier का अर्थ है कि float value को 2 decimal places के साथ print करो। उदाहरण के लिए 3.75000 की जगह 3.75 दिखेगा। यह output को user-friendly बनाता है।
8. Default Case
default:
printf("\nInvalid Choice!");अगर user +, -, *, / के अलावा कोई और character enter करे (जैसे %, ^, या कोई letter), तो default case execute होता है। यह program को robust बनाता है।
C Language Calculator Program के Variations
एक बार basic calculator समझ आने के बाद आप इसे और advanced बना सकते हैं:
Loop-based Calculator — जब तक user ‘q’ न press करे, program बार-बार calculate करता रहे:
while (ch != 'q')
{
// calculator logic
printf("\nContinue? (q to quit): ");
scanf(" %c", &ch);
}Function-based Calculator — हर operation के लिए अलग function बनाएं, जैसे float add(float a, float b), float divide(float a, float b) आदि। इससे code modular और reusable बनता है।
Multiple Operations — modulus (%) और power (^) जैसे additional operations भी add किए जा सकते हैं।
Common Mistakes जो Beginners करते हैं
इस C language calculator program को लिखते समय ये गलतियां सबसे ज्यादा होती हैं:
- scanf(” %c”, &ch) में space भूल जाना — इससे program operator ही नहीं पढ़ता
- break statement न लगाना — fall-through के कारण गलत output
- int की जगह float use न करना — decimal results गलत आते हैं
- Division by zero check न करना — program crash या undefined behavior
- default case न लिखना — invalid input पर program कुछ नहीं करता
Conclusion ( निष्कर्ष )
C language calculator program वास्तव में C programming सीखने का एक बेहतरीन starting point है। इस एक program में आपने switch case, float variables, formatted output, error handling, और user input — सभी जरूरी concepts एक साथ practically देखे। यह program छोटा जरूर है, लेकिन इसके concepts बड़े-बड़े projects में भी काम आते हैं। Switch case का clean structure, break statement की importance, और division by zero जैसी edge cases handle करना — ये सब professional programming habits हैं जो आपको शुरुआत से ही adopt कर लेनी चाहिए।
अब आपकी बारी है — इस program को अपने compiler में type करें, run करें, और फिर इसे modify करने की कोशिश करें। Loop add करें ताकि program बार-बार calculate करे, या नए operations जैसे modulus या power add करें। जितना ज्यादा आप experiment करेंगे, उतना ज्यादा सीखेंगे। अगर कोई doubt है तो नीचे comment में जरूर पूछें, और इस post को उन दोस्तों के साथ share करें जो C programming सीख रहे हैं।
Frequently Asked Questions
Q 1: C language calculator program में float की जगह double क्यों नहीं use करते?
Q 2: scanf(” %c”, &ch) में space क्यों जरूरी है?
Q 3: क्या इस calculator program को बिना switch case के बनाया जा सकता है?
Q 4: Division by zero check num2 == 0 से करें या num2 < 0.00001 से?
Q 5: इस program को VS Code में compile और run कैसे करें?
gcc calculator.c -o calculator
Compile होने के बाद program को run करें:
Windows: calculator.exe Linux / macOS: ./calculator
अगर आपके system में GCC install नहीं है, तो पहले GCC compiler install करें। बिना installation के practice करने के लिए OnlineGDB, Replit या CodeChef IDE जैसे online compilers का इस्तेमाल भी कर सकते हैं।
