C language calculator program in Hindi – Switch Case

C language calculator program in Hindi सीखना हर beginner programmer के लिए बहुत जरूरी है, क्योंकि इससे basic arithmetic operations और decision making concepts दोनों clear हो जाते हैं। इस पोस्ट में आप जानेंगे कि कैसे C language में एक simple calculator बनाया जाता है जो addition, subtraction, multiplication और division perform करता है। साथ ही आप switch case, user input और error handling जैसे concepts को practically समझ पाएंगे। इस program को सीखने के बाद आप अपने खुद के interactive programs बनाना शुरू कर सकते हैं।

Program statement

दो floating point numbers user से input लीजिए। फिर user से एक operator (+, -, *, /) चुनने को कहिए। चुने गए operator के आधार पर संबंधित arithmetic operation perform कीजिए और result display कीजिए। यदि user division में zero से divide करने की कोशिश करे तो error message दिखाइए। अगर invalid operator चुना जाए तो “Invalid Choice” दिखाएं।

Program Code

#include<stdio.h>
int main()
{
float num1,num2;
char ch;
printf("Enter two Numbers (Space Seprated): ");
scanf("%f%f",&num1,&num2);
printf("\nEnter Your choice (+,-,*,/) : ");
scanf(" %c",&ch);
switch(ch)
{
case '+':
printf("\n%.2f + %.2f = %.2f",num1,num2,num1+num2);
break;
case '-':
printf("\n%.2f - %.2f = %.2f",num1,num2,num1-num2);
break;
case '*':
printf("\n%.2f * %.2f = %.2f",num1,num2,num1*num2);
break;
case '/':
if(num2 != 0)
printf("\n%.2f / %.2f = %.2f",num1,num2,num1/num2);
else
printf("\nError! Division by zero not allowed.");
break;
default :
printf("\nInvalid Choice!");
}
return 0;
}

Program output :

Enter two Numbers (Space Seprated): 10 5

Enter Your choice (+,-,*,/) : *

10.00 * 5.00 = 50.00

Code Explanation

सबसे पहले #include<stdio.h> header file include की गई है, जिससे input और output functions जैसे printf() और scanf() use किए जा सकें।

float num1, num2; दो variables declare किए गए हैं ताकि decimal numbers भी accept किए जा सकें। char ch; variable operator store करने के लिए use किया गया है।

scanf(“%f%f”,&num1,&num2); के जरिए user से दो numbers input लिए जाते हैं। इसके बाद scanf(” %c”,&ch); user से operator लेता है। ध्यान दें कि %c से पहले space दिया गया है ताकि previous newline character skip हो जाए।

अब main logic switch(ch) के अंदर है। switch statement operator की value check करता है।

  • अगर + है तो addition होता है।
  • अगर – है तो subtraction.
  • अगर * है तो multiplication.
  • अगर / है तो पहले check किया जाता है कि num2 zero तो नहीं है। अगर zero नहीं है तो division किया जाता है, वरना error message print होता है।

default case तब execute होता है जब user कोई invalid operator enter करता है।

हर case के बाद break जरूरी है, वरना control अगले case में चला जाएगा।

यह program beginners को switch case, conditional checking और formatted output (%.2f) समझने में मदद करता है। %.2f का मतलब है कि result दो decimal places तक show होगा।

इस तरह यह simple calculator program basic C concepts को practically समझने के लिए एक बेहतरीन example है।

Leave a Comment

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

Scroll to Top