Opposite Number, C, CodeWars

Problem Statement

Very simple, given a number (integer / decimal / both depending on the language), find its opposite (additive inverse).

Examples:

1: -1
14: -14
-34: 34

Chosen Solution

Using the additive inverse property, we know the following:

In the integers, every element x has a unique element -x such that:
x + (-x) = 0

Now that we know that, we can return the inverse of a number by multiplying it by -x.

int opposite(int number)
{
  return -number; // use additive inverse property
}

Link To Solution on CodeWars

Bibliography

@misc{codewars-opposite-number}
author Codewars
title Opposite Number
note C kata.
Last updated