Square and Square root in Arduino

Arduino has support for several popular math functions, square and square root being among them. Let’s look at the square root first.



where is a number of any data type. It returns a double.

For square, you ideally shouldn’t need a separate function. You can just multiply the number by itself.

x_squared = x*x;

However, Arduino does have a separate function for calculating squares. The syntax is −

sq(x) where x is a number of any data type. This again returns a double.


The following example illustrates the use of these functions −

void setup() {
   // put your setup code here, to run once:
   float x = 4.0;

void loop() {
   // put your main code here, to run repeatedly:


The Serial Monitor output is shown below −

Updated on: 29-May-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started