Challenge 19 - Difference Between Squares

In this challenge, you are going to implement the DiffSquares function so that it returns the difference between the first number squared minus the second number squared.

5^2 - 4^2 = 9

If you require a hint as to how this is done, please click below:

Hint

You can calculate powers of numbers in Go using the math.Pow function. You can read more about this here: Math Pow

View Solution
package main

import (
	"fmt"
	"math"
)

func DiffSquares(n, m int) int {
	x := math.Pow(float64(n), 2)
	y := math.Pow(float64(m), 2)
	return int(x) - int(y)
}

func main() {
	fmt.Println("Calculate The Difference of Squares")
	result := DiffSquares(5, 4)
	fmt.Println(result)
}

Further Reading:

If you like this challenge then you may also appreciate some of the following articles on the site:


Other Challenges