Advertisement
DrAungWinHtut

calculator1.cpp

Jun 26th, 2025
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     float n1 = 0.0F;
  6.     float n2 = 0.0F;
  7.     float ans = 0.0F;
  8.     char ch = '\0'; //NULL
  9.     cout<<"Calculator App"<<endl;
  10.     cout<<"===================="<<endl;
  11.     cout<<"Please enter first number: ";
  12.     cin>>n1;
  13.     cout<<"Please enter second number: ";
  14.     cin>>n2;
  15.     cout<<"choose operation +,-,*,/,%: ";
  16.     cin>>ch;
  17.  
  18.     if (ch == '+'){
  19.         ans = n1 + n2;
  20.     }
  21.  
  22.     else if (ch == '-'){
  23.         ans = n1 - n2;
  24.     }
  25.  
  26.     else if (ch == '*'){
  27.         ans = n1 * n2;
  28.     }
  29.  
  30.     else if (ch == '/'){
  31.         ans = n1 / n2;
  32.     }
  33.  
  34.     else if (ch == '%'){
  35.         ans = (int)n1 % (int)n2;
  36.     }
  37.     else {
  38.         cout << "Invalid operation!" << endl;
  39.         return 1; // Exit with error code
  40.     }
  41.  
  42.     cout<<"answer = "<<ans<<endl;
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement