How to find the opposite number in C#?

How to find the opposite number in C#? #csharp #dotnet #math

Abdallah Yashir
Jan 10, 2023

We want to return the opposite number.

Examples:

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

public static int Opposite(int number) { return -number; }

Solution

Another solution is:

return n * -1;

A SO reference:
https://stackoverflow.com/questions/1059480/math-opposite-sign-function

Kata from codewars:
https://www.codewars.com/kata/56dec885c54a926dcd001095/train/csharp

Read more about Math Functions:

These functions can be important if you’re going to work with numbers.

--

--

Abdallah Yashir

Senior Software Developer, Writer, Amateur Photographer, Reader