C++ Operators | Set 1 (Arithmetic) | Practice | GeeksforGeeks

Given two numbers A and B. Perform addition,multiplication, subtraction and division on these two numbers.
Example 1:
Input: A = 1,  B = 2
Output: 3 2 1 2
Explanation:
A + B = 3
A * B = 2
Since B>A, therefore the result of
subtraction and division are
B - A = 1 and B / A = 2 respectively.
Example 2:
Input: A = 5 B = 7
Output: 12 35 2 1
Explanation:
A + B = 12
A * B=35
Since B>A, therefore results of subtraction
and division are B - A = 2 and B / A = 1.

Your Task:
You don't need to read input or print anything.Your task is to complete the function cppOperator() which takes two integers A and B as input parameters and returns a vector containing the results of addition,multiplication,subtraction and division of the two numbers.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
 
Constraints:
1 <= A,B <= 10000
0
cf05567729951 week ago
class Solution { public:   vector<int> cppOperators(int A, int B) {       vector<int> v;       int a,b;       if(A>B)       {           a=A;           b=B;       }       else       {           a=B;           b=A;       }       v.push_back(a+b);       v.push_back(a*b);       v.push_back(a-b);       v.push_back(a/b);          }};
0
mayank180919993 weeks ago
 vector<int> cppOperators(int A, int B) {
        // code here
        vector<int>v;
          v.push_back(A+B);
          v.push_back(A*B);
         if(B>A){
           v.push_back(B-A);
           v.push_back(B/A);
         }else{
           v.push_back(A-B);
           v.push_back(A/B);  
         }
       return v; 
    }
0
rsbly7300954 weeks ago
class Solution {
 public:
   vector<int> cppOperators(int A, int B) {
       // code here
       if(A>B){
           return{A+B,A*B,A-B,A/B};
       }
       else{
           return{B+A,B*A,B-A,B/A};
       }
   }
};
0
rsbly730095
This comment was deleted.
0
anushavennapoosa20022 months ago
vector<int> cppOperators(int A,int B)
{
      if(A>B)       {           return {A+B,A*B,A-B,A/B};       }      return {A+B,A*B,B-A,B/A};        }
};
0
0niharika22 months ago
vector<int> v;       v.push_back(A+B);       v.push_back(A*B);       v.push_back(abs(A-B));       if(B>A) v.push_back(B/A);       else    v.push_back(A/B);       return v;
0
niharikamalik502 months ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
// } Driver Code Endsclass Solution { public:   vector<int> cppOperators(int A, int B) {       // code here              if (A>B){           return{A+B,A*B,A-B,A/B};       }       else{           return{B+A,B*A,B-A,B/A};       }   }};
// { Driver Code Starts.int main() {   int t;   cin >> t;   while (t--) {       int A, B;       cin >> A >> B;       Solution ob;       vector<int> ans = ob.cppOperators(A, B);       for (int u : ans) cout << u << "\n";   }}  // } Driver Code Ends
+1
tushargarg98683 months ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;
// } Driver Code Endsclass Solution { public:   vector<int> cppOperators(int A, int B) {       // code here       if(A>B){           return{A+B,A*B,A-B,A/B};       }       else{           return{B+A,B*A,B-A,B/A};       }   }};
// { Driver Code Starts.int main() {   int t;   cin >> t;   while (t--) {       int A, B;       cin >> A >> B;       Solution ob;       vector<int> ans = ob.cppOperators(A, B);       for (int u : ans) cout << u << "\n";   }}  // } Driver Code Ends
+1
shivamguptakrm3 months ago
class Solution {
  public:
    vector<int> cppOperators(int A, int B) {
        // code here
        if (A>B){
            return{A+B,A*B,A-B,A/B};
        }else{
            return{B+A,B*A,B-A,B/A};
        }
    }
};
0
dsvr240220014 months ago
if(A>B){
        return {A+B,A*B,A-B,A/B};
    
    }
      
     return {B+A,B*A,B-A,B/A};
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.