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.
#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;
}
Comments
Leave a comment