Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- float n1 = 0.0F;
- float n2 = 0.0F;
- int n11 = 0; // For integer division and modulus
- int n22 = 0; // For integer division and modulus
- float ans = 0.0F;
- char ch = '\0'; //NULL
- cout<<"Calculator App"<<endl;
- cout<<"===================="<<endl;
- cout<<"Please enter first number: ";
- cin>>n1;
- cout<<"Please enter second number: ";
- cin>>n2;
- cout<<"choose operation +,-,*,/,%: ";
- cin>>ch;
- switch (ch) {
- case '+':
- cout << "You chose addition." << endl;
- ans = n1 + n2;
- break;
- case '-':
- cout << "You chose subtraction." << endl;
- ans = n1 - n2;
- break;
- case '*':
- cout << "You chose multiplication." << endl;
- ans = n1 * n2;
- break;
- case '/':
- cout << "You chose division." << endl;
- if (n1 == 0.0F && n2 == 0.0F) {
- cout << "Error: Both numbers are zero!" << endl;
- return 1; // Exit with error code
- }
- else if (n1 == 0.0F) {
- cout << "Error: First number is zero!" << endl;
- return 1; // Exit with error code
- }
- else if(n2 == 0.0F) {
- cout << "Error: Division by zero!" << endl;
- return 1; // Exit with error code
- }
- else {
- ans = n1 / n2; // Perform division
- }
- break;
- case '%':
- cout << "You chose modulus." << endl;
- n11 = static_cast<int>(n1); // Convert to integer for modulus
- n22 = static_cast<int>(n2); // Convert to integer for modulus
- if (n22 == 0) {
- cout << "Error: Division by zero!" << endl;
- return 1; // Exit with error code
- }
- ans = n11 % n22; // Perform modulus
- break;
- default:
- cout << "Invalid operation!" << endl;
- return 1; // Exit with error code
- }
- cout<<"answer = "<<ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement