C is used to write the bulk of game libraries. The extension of a C file is “.cc” or “.cpp.” It’s a high-level as well as a low-level language. C was created with a focus on systems programming, embedded, resource-constrained software, and big systems in mind, with performance, efficiency, and usage flexibility as design goals. C is also effective in various situations, having particular strengths in software infrastructure and resource-constrained applications such as desktop apps, video games, servers (such as online e-commerce search).

When we square a number, we simply multiply it by itself. We have to utilize a header file if we want to get a square of a number. Header files allow us to declare a function with a type placeholder that the compiler will fill in at compile-time based on how the function is used.

In C , when we need a square of any given number, numerous methods are available. Let’s talk about a few of them:

Find square of a number using Power function

Using the Power function, we may square any value. For it, we will have to include library. We must pass the Base value to be squared and the Power value into the function. In C , the power() function works as a square operator in this instance.

#include

#include

using namespace std;


 

int main(){


    int b = 34;


    int p = 2;


    float result = pow(b,p)


    cout<< “Square = “<<result<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-01.png" data-lazy- height="162" src="data:image/svg xml,” width=”456″>

The “cmath” library has a predefined function called pow. Therefore, we must integrate this library at the beginning of the code. We declare two variables in the body of the main function. The first variable is defined to store the value of the base. The value here is “34”. The second variable is declared to store a power value that is 2 in this program. These two variables have an integer data type.

Furthermore, we apply the pow() function. We pass two arguments (base value and power value) for this function. It returns the result. The output is stored in a new variable termed ‘result’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-02.png" data-lazy- height="118" src="data:image/svg xml,” width=”496″>

Find square of a number using for loop

If we need to get a square of a value without applying multiplication or division, we must use another logic to get the given value’s square. In the succeeding program, we utilize for loop.

#include

using namespace std;

float Sqr(float number){


     


    float a=0.0;


    for(int j=0;j<number;j ){


     a= a number;  


    }


     


    return a;

}


 

int main(){


    cout<<“Square = “<<Sqr(20.0)<<endl;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-03.png" data-lazy- height="250" src="data:image/svg xml,” width=”454″>

We declare the function sqr(), and its data type is ‘float’. We pass a floating-point number as an argument to this function. Moreover, we utilize a for loop in this instance to add a number. First, we allocate ‘0’ to the variable ‘j’ in the initialization segment. The test condition checks the value of that variable. ‘j<number’ tests the loop every time to see if ‘j’ is less than the given value. ‘j ’ increases the variable ‘j’ every time the loop is implemented.

Generally, any indication can be utilized to increment the loop variable. Once the loop ends, the variable is still defined and holds the value allocated by the latest increment. In the code, we add 20 20….Up to 20 times. Therefore, after the addition, 20 square (400) is created. Compiling and running the above program produces this type of output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-04.png" data-lazy- height="112" src="data:image/svg xml,” width=”536″>

Find square of a number using while loop

If we use a while loop to find the square of any number, we will need to include an odd number so that the square is created at the end of the program.

#include

using namespace std;

float Square(float value)

{


    float OddNum = 1.0;


    float SquareNum = 0.0;


 


    value = abs(value);


 


    while (value)


    {


        SquareNum = SquareNum OddNum;


        OddNum = OddNum 2;


    }


 


  return SquareNum;

}

int main()

{


    cout<<“Square of Number = “<<Square(5.0)<<endl;


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-05.png" data-lazy- height="372" src="data:image/svg xml,” width=”458″>

In this instance, after integrating the library ‘#include , we define the ‘square’ function. The floating-point value is passed as an argument to this function. Further, we declare variables ‘OddNum’ and ‘SquareNum’ and assign them values. Afterward, we apply the absolute function ‘abs()’ that converts the negative value to the positive when we enter any negative value. We use a while loop.

The compiler first evaluates the test condition when a while statement is implemented. Once the body of the loop is implemented, the condition is assessed again, and if it becomes true, the body of the loop is implemented once again. This procedure continues till the test condition becomes false. Once it is false, the control is passed on to the first statement after the end of the body of a loop. In every evaluation, ‘2’ is added to the value ‘OddNum’ to make it odd.

When the above code is executed, it will give the output shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Square-Number-cpp-06.png" data-lazy- height="106" src="data:image/svg xml,” width=”608″>

Conclusion

In this article, we have deliberated three techniques for finding the square of the number in C . First, we see how we get the square of a number by using the pow() function. Likewise, we utilize the ‘for’ loop and ‘while’ loop for finding the square. By using for loop, we perform the addition of any number. Similarly, we add an odd number by using the while loop to get the square.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/omar-150×150.png6220578683961.jpg" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.