C Program Simple Calculator Using Switch Statement






C Program Simple Calculator using Switch Statement

\n\n\n

\n

C Program Simple Calculator using Switch Statement

\n

Use this tool to simulate and understand a C program that performs basic arithmetic operations (addition, subtraction, multiplication, division) using a switch statement.

\n \n

\n \n \n

\n

\n\n

\n \n \n

\n

\n\n

\n \n \n

\n\n

\n \n \n

\n\n

Result will appear here

\n\n

\n

How This Works (C Code Logic)

\n

This calculator demonstrates a basic C program structure. The user inputs two numbers and selects an operation. The program uses a switch statement to check the selected operation and perform the corresponding arithmetic.

\n

\n#include <stdio.h>\nint main() {\n    char operator;\n    double num1, num2, result;\n    printf(\"Enter an operator (+, -, *, /): \");\n    scanf(\"%c\", &operator);\n    printf(\"Enter two numbers: \");\n    scanf(\"%lf %lf\", &num1, &num2);\n    switch(operator) {\n        case '+': result = num1 + num2; break;\n        case '-': result = num1 - num2; break;\n        case '*': result = num1 * num2; break;\n        case '/': \n            if (num2 != 0) result = num1 / num2; \n            else { printf(\"Error! Division by zero.\\n\"); return 1; }\n            break;\n        default: printf(\"Invalid operator.\\n\"); return 1;\n    }\n    printf(\"Result: %.2lf\\n\", result);\n    return 0;\n}\n        

\n

\n

\n\n\n\n\n\n”
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]

Leave a Reply

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