### Free Coding Ebook 👉 Get Now

# Creating Advanced Calculator Using C++

## Introduction

Hello, coders. Welcome to the codewithrandom blog. Today we will learn how to create an Advanced Calculator which will consist of four operations –

- Arithmetic
- Trigonometric
- Exponential
- Logarithmic

Out of the given choices of operations, the user can choose and operate by entering values as per their choice.

For each operation there will be choices given and the user can then select the option they would want for the operation they want to perform. After selecting the choice of operation the user will enter the values. The result will be displayed after entering the values. Then a choice to either continue performing operations or not will be given. The user can select (y) for yes – if they wish to continue performing more operations and (n) for no – if they don’t wish to perform another operation and would like to exit the program.

ADVERTISEMENT

## Overview of the UI

**As we can see from the User Interface of the app in the main picture at the start of our article, the available options are:**

- Arithmetic
- Trigonometric
- Exponential
- Logarithmic

Creating an Address Book Project in C++ (Source Code)

Further operations for **Arithmetic: **

- Addition
- Subtraction
- Product
- Division

Further operations for **Trigonometric:**

- Sin
- Cosine

Further for **Exponential:**

- Enter base:
- Enter exponent:

Further for **Logarithmic:**

- Enter the value to calculate the log(e):

Now let us take a look at the code of this project:

CODE:

#include<iostream> #include<cmath> using namespace std; void arithmetic(){ int op = 0; float A = 0; float B = 0; cout<<"Select opeartion\n"; cout<<"[1] Addition\n"; cout<<"[2] Substraction\n"; cout<<"[3] Product\n"; cout<<"[4] Division\n"; cin>>op; cout<<"Enter the number:"; cin>>A; cout<<"Enter second number:"; cin>>B; cout<<"Result: "; switch(op){ case 1: cout<<(A+B); break; case 2: cout<<(A-B); break; case 3: cout<<(A*B); break; case 4: cout<<(A/B); break; default: cout<<"Invalid operation"; break; } cout<< endl; } void trignometric(){ int op = 0; float val = 0.0; cout<<"Select\n"; cout<<"[1] Sine\n"; cout<<"[2] Cosine\n"; cout<<"Op: "; cin>>op; cout<<"Enter value: "; cin>>val; if(op == 1){ cout<<sin(val); } else if(op == 2){ cout<<cos(val); } else{ cout<<"Invalid operation"; } cout<<endl; } void exponential(){ float base = 0.0; float eee = 0.0; cout<<"Enter base:"; cin>>base; cout<<"Enter expnent: "; cin>>eee; cout<<pow(base, eee)<<endl; } void logarithmic(){ float value = 0.0; cout<<"Enter the value to calculate the log(e): "; cin>>value; cout<<log(value)<<endl; } int main(){ int sel = 0; char choice = 'y'; cout<<"Advanced Calculator\n"; cout<<"[1] Arithmetic\n"; cout<<"[2] Trignometric\n"; cout<<"[3] Exponential\n"; cout<<"[4] Logarithmic\n"; cout<<"Your choice:"; while(choice == 'y'){ cout<<"Enter the type of operation you want to calculate\n"; cin>>sel; switch(sel){ case 1: arithmetic(); break; case 2: trignometric(); break; case 3: exponential(); break; case 4: logarithmic(); break; default: cout<<"Invalid Operation"; } cout<<"Do you want to continue? y/n"<<endl; cin>>choice; if(choice == 'n'){ break; } } return 0; }

**Code Explanation:**

After writing the header of the code with the required libraries, we have declared functions with their definitions, the functions are – arithmetic(), trignometric(), exponential(), logarithmic() and then the main() function.

How to Create an Encryption-Decryption project using C++

**The arithmetic() function:**

It consists of a switch-case which will allow the user to select the operation they want to perform under arithmetic, entering of the two values required for the operation and displaying the result.

Here a snippet for you to understand the working of arithmetic() function properly:

**The** **trignometric() function:**

It consists of an if-else if-else control statement for the selection fo operation between sine and cosine. We will enter the value we want to operate for the sine or cosine trigonometric function. After entering the value, the result will be displayed.

Here a snippet for you to understand the working of trignometric() function properly:

ADVERTISEMENT

**The exponential() function:**

ADVERTISEMENT

It consists of entering the base and the exponent value. The pow function is used to calculate the result which will be displayed after entering the values.

ADVERTISEMENT

Here a snippet for you to understand the working of exponential() function properly:

ADVERTISEMENT

ADVERTISEMENT

**The logarithmic() function:**

It consists of entering the value to calculate the log(e). After entering the value, the result will be displayed.

Here a snippet for you to understand the working of logarithmic() function properly:

**The main() function:**

- First of we want to display the operation this advanced calculator has so the operations will be printed on the console using cout.
- The user will choose an operation from the options displayed for which a switch case has been written. The cases contain the functions which we have declared for the operations to be performed.
- So upon selecting the option from the cases, the corresponding function will be called and the operation will be performed.
- After the function has been called and the operation has been completed, the user will get a choice of continuing to perform more functions or to exit for the program.

Electricity Bill Generator Project In C++(Source Code)

## Conclusion

We have reached the end of this article but we have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects. Learning C++ by creating fun projects makes learning easy and interesting.

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂