Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number squared in programming

Tags:

math

I know this is probably a very simple question but how would I do something like n2 in a programming language?

Is it n * n? Or is there another way?

like image 716
Ozzy Avatar asked Apr 06 '10 16:04

Ozzy


1 Answers

n * n is the easiest way.

For languages that support the exponentiation operator (** in this example), you can also do n ** 2

Otherwise you could use a Math library to call a function such as pow(n, 2) but that is probably overkill for simply squaring a number.

like image 129
Justin Ethier Avatar answered Nov 05 '22 07:11

Justin Ethier