Answer to Question #272025 in C++ for P11

Question #272025

Implement a calculator using bitwise operators and for loops only. Your program should run correctly for positive integers only. You calculator should be able to perform following operations. Basic arithmetic functions (Addition, subtraction, division and multiplication); Square – to compute the square of the given value and Power – to compute the power of an integer. It should take two int arguments number and its power. Any operation that uses operators other than bitwise operators will be awarded zero marks


1
Expert's answer
2021-11-27T02:33:59-0500


#include <iostream>
#include <math.h>
using namespace std;




void add(){
    int n1,n2;
    cout<<"\nEnter the first number: ";
    cin>>n1;
    cout<<"\nEnter the second number: ";
    cin>>n2;


    cout<<"\n"<<n1<<" + "<<n2<<" : "<<(n1+n2);
}
void sub(){
    int n1,n2;
    cout<<"\nEnter the first number: ";
    cin>>n1;
    cout<<"\nEnter the second number: ";
    cin>>n2;


    cout<<"\n"<<n1<<" - "<<n2<<" : "<<(n1-n2);
}
void div1(){
    int n1,n2;
    cout<<"\nEnter the first number: ";
    cin>>n1;
    cout<<"\nEnter the second number: ";
    cin>>n2;


    cout<<"\n"<<n1<<" / "<<n2<<" : "<<(n1/n2);
}
void mul(){
    int n1,n2;
    cout<<"\nEnter the first number: ";
    cin>>n1;
    cout<<"\nEnter the second number: ";
    cin>>n2;


    cout<<"\n"<<n1<<" * "<<n2<<" : "<<(n1*n2);
}
void square(){
    int n;
    cout<<"\nEnter a number: ";
    cin>>n;
    cout<<"\nSquare is: "<<(n*n);
}
void power(){
    int b,p;
    cout<<"\nEnter base: ";
    cin>>b;
    cout<<"\nEnter power: ";
    cin>>p;


    cout<<"\n"<<b<<" raised to power "<<p<<" = "<<pow(b,p);
}
int main()
{
    cout<<"\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n5. Square\n6. Power\n";
    cout<<"Choose an operation to perform: ";
    int c;
    cin>>c;


    if (c==1){
        add();
    }
    else if (c==2){
        sub();
    }
    else if (c==3){
        div1();
    }
    else if (c==4){
        mul();
    }
    else if (c==5){
        square();
    }
    else if (c==6){
        power();
    }




    return 0;
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS