Are you searching for a way to square something in c++? Like you want to square any number in C++, In this article, I will show you how you can square a number in C++ in three different and unique ways.

## Square using Pow Function from Math Library

You can use the pow function from the math library^{[1]}. This function accepts two integer inputs, one is the number as a base and the other the power you want. Since you want to square the number hence all you need to do is to change the base number and keep the power input to a constant value of 2.

Let us see in the example code below to understand how the pow function works in c++ and how you can use it as well in your application. You can **use the system pause** if you want to keep your program running to verify the output and unless any button is pressed.

#include <iostream> #include <math.h> int main () { //creating the base and power constant int base = 5; int powerConstant = 2; //Initially keeping the Number as zero int squaredNumber = 0; squaredNumber = pow(base, powerConstant); std::cout<<squaredNumber; return 0; }

**Output:**

`The Square of 5 is 25`

## Sqaure using Multiplication to input Number

A Simple and naive approach can be just multiplying the base number two times and storing it in different variables and outputting that variable. Let us see in the below example code for C++.

#include <iostream> int main () { //creating the base int base = 20; //Initially keeping the Number as zero int squaredNumber = 0; //multiplying base two times squaredNumber = base*base; std::cout<<"The Square of "<<base<<" is "<<squaredNumber; return 0; }

**Output:**

`The Square of 20 is 400`

## Adding the Base Number to Number of Times of Base Number

You can get the square of any number by adding the same number to the number of times of number. This sentence became a lot confusing hence let me clear it out with an example.

Support you have a number 5 hence adding number 5 up to 5 times will give you 25 that is the square of the number. Hence we can create a function to do that math and return the square of the number. Let us see in the below example code.

#include <iostream> int squareByAddingNumber(int baseNumber){ int squaredNumber = 0; for(int i=0;i<baseNumber;i++){ squaredNumber += baseNumber; } return squaredNumber; } int main () { //creating the base int base = 15; //Initially keeping the Number as zero int squaredNumber = 0; //multiplying base two times squaredNumber = squareByAddingNumber(base); std::cout<<"The Square of "<<base<<" is "<<squaredNumber; return 0; }

**Output:**

`The Square of 15 is 225`

That is all for this post on how to square something in C++. Then please follow us on **Facebook **and **Twitter**. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

**Further Read:**