Example 01: Manual Way
Firstly, make a C++ file within it. Use the touch keyword with the new file name having a C++ extension. The file will be created within your home folder of the Linux system. You can open this file by double-tapping it directly in the text editor to make code in it. There comes another built-in editor as well in the Linux system, i.e., Nano and vim. We used the GNU Nano to directly open the new file within the shell console. Thus, both the commands for the creation and opening of the C++ file are shown in the terminal within the image below.
We have been starting our very first example by using a simple manual way to raise a number to power with an exponent in C++. So, we have included the input-output stream library header at the first line of code after opening the file. The standard namespace has been utilized in the code after the header file. These two are necessary for our C++ code to run and execute. The execution of C++ code mainly starts from the main() method. Thus, we have been using the main() function here while initializing some integer variables to 1, i.e., b stands for a base, e stands for exponent, res stands for a result.
The standard cout clause has been used for all users to enter a base value, i.e., number. The cin clause is used to get the input from a user in variable “b” as a base value that will be raised to a power. Another cout statement tells a user to input the exponent value, i.e., number. This number would be used as a power to a base number. The cin clause has been used to take exponent number as input from the user at run-time. The for loop has been used to calculate the value of a base number using the exponent as its power. The loop will be executed from 0 to the exponent value entered by a user as input, i.e., 2,3,4,5,6, etc. Until then, the variable “res” value will be multiplied by the base value number entered by a user. The variable “res” uses 1 as the initial value, while its value would be changed at the next consecutive iteration. The resultant value would be displayed by the use of the variable “res” in the standard cout statement within the code.
This is how we use a manual way to calculate a number having an exponent to raise a number to the power. Save your code file using the Ctrl+S first. Now, quit the file to back towards the terminal via the “Ctrl+X” shortcut.
Now, it’s time to compile the newly created code file of C++. Thus we have already installed the C++ compiler in our Ubuntu 20.04 system named g++. The compilation of this code returns nothing. This illustrates that our C++ code is correct by every means. The execution of any C++ code can be done by the “./a.out” command within the shell. So, we have used the same one. The user has been asked to input the base number. Thus, we have added 6 as a base value. Then, a user has been asked to enter the exponent value that would be used as a power to the base number. The user entered 5 this time. The program “for” loop has taken both values and calculated the resultant value 7776 for a base number 6 having exponent raised to the power 5. This has been calculated as a simple mathematical logic of 6*6*6*6*6.
Let’s have another example of using the power function pow() in the C++ code to use a base number with raise to some power. For this purpose, we have opened the same C++ file and added the input-output library with standard c++ bits header file using the #include a keyword. The namespace is used after the header files. The main function has been started with initializing an integer variable “x”. The power function pow() uses two values within its parameter. The first value is base, i.e., 7, and the other is exponent value to raise a number to the power 3. The 0.5 has been included with the power function to avoid any inconvenience caused by the compiler, i.e., may take the result value in double. The calculated value would be saved to the variable “x” while converted to integer-type as we don’t want any other type of value here. The standard cout statement is utilized so far to display the result using the variable “x” in it. The main method closes here. You have to save this updated code with Ctrl+S.
Compiled the code with G++ compiler and got no errors, i.e., compilation got successful as the code contains no logical or syntax errors. The execution has been performed by the same “./a.out” commands in the shell. In return, we have got the value 343 as calculate a result for the base 7 raised to the power exponent 3.
Let’s take a deep look at the power function to know how it reacts to the exponent 0. So., we have opened the same file once again and left the overall code unchanged. The only change we have done is within the parenthesis of the pow() function. We have used the exponent 0 here to see if the calculated value would be 1 or not. Leave the file after saving it.
So, after the compilation of this code, we have executed it. The result is 1 as expected.
Let see how the pow() function works on the negative exponent power to raise a number in power. So, we have opened the same and changed the power function only. The rest of the code has remained unchanged. We have added -2 as the exponent value. Let’s execute this file.
After the file compilation and execution, we have got 0 as a result of a negative exponent.
Within the below illustration, we have used the negative base value and positive exponent.
As a result, we have got a positive result due to the use of even positive exponent.
This article explains using the exponent to raise a number to certain power within C++ code. We have utilized the manual “for” loop and the power() function to achieve this goal. All the examples used in this article are easy and simple to understand for every basic and expert user. We believe this article would help every type of C++ user.