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
}
Links from this note
Opposites Attract, C, CodeWars
Leap, C, Exercism
Clock, C++, CodeWars
Roman Numerals Decoder, C++, Codewars
CodeWars
1 of 2
2 of 2
Links to this note
Bibliography
@misc{codewars-opposite-number}
author Codewars
title Opposite Number
note C kata.