C Program for Basic Calculator Using switch स्विच का उपयोग करके Basic Calculator बनाना
A basic calculator performs arithmetic operations such as addition, subtraction, multiplication, and division. In C language, we can use the switch statement to perform these operations based on the user's choice.
एक Basic Calculator Addition, Subtraction, Multiplication और Division जैसी arithmetic operations करता है। C भाषा में switch statement का उपयोग करके user की choice के आधार पर इन operations को perform किया जा सकता है।
C Program
#include <stdio.h>int main(void){float a, b, ans;int choice;while (1){printf("\nBasic Calculator\n");printf("1. Addition\n");printf("2. Subtraction\n");printf("3. Multiplication\n");printf("4. Division\n");printf("5. Exit\n");printf("Enter your choice: ");scanf("%d", &choice);if (choice == 5)break;if (choice < 1 || choice > 5){printf("Wrong input\n");continue;}printf("Enter two numbers: ");scanf("%f %f", &a, &b);switch (choice){case 1:ans = a + b;printf("Answer = %f\n", ans);break;case 2:ans = a - b;printf("Answer = %f\n", ans);break;case 3:ans = a * b;printf("Answer = %f\n", ans);break;case 4:if (b != 0){ans = a / b;printf("Answer = %f\n", ans);}else{printf("Division by zero is not allowed\n");}break;}}return 0;}
Output
Basic Calculator1. Addition2. Subtraction3. Multiplication4. Division5. ExitEnter your choice: 1Enter two numbers: 10 5Answer = 15.000000
Explanation
while(1)repeats the calculator menu until the user chooses Exit.switch(choice)selects the arithmetic operation.case 1performs Addition.case 2performs Subtraction.case 3performs Multiplication.case 4performs Division, after checking that the second number is not zero.case 5exits the program usingbreak.default-like validation checks invalid choices.
switch(choice) user की choice के अनुसार operation select करता है। Division करने से पहले यह जाँच की जाती है कि दूसरा number zero न हो। User द्वारा 5 चुनने पर calculator बंद हो जाता है।
Modern C Compatibility Note
This program uses standard C and works with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), getch(), or exit().
This program demonstrates how to create a basic calculator using the switch statement and a loop in C.
यह program C भाषा में switch statement और loop का उपयोग करके Basic Calculator बनाना सिखाता है।
Comments
Post a Comment