Algorithm 4 - Return Negative

Beast from the east·2021년 10월 6일
0

Algorithm

목록 보기
4/27

Q.

Description:
In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?

Example:

makeNegative(1); // return -1
makeNegative(-5); // return -5
makeNegative(0); // return 0
Notes:

The number can be negative already, in which case no change is required.
Zero (0) is not checked for any specific sign. Negative zeros make no mathematical sense.

A)

int makeNegative(int num)
{
  return (num < 0 ? num : -num);
}

another solution
int makeNegative(int num)
{
  return -abs(num);
}
-> <stdlib.h> // abs() Function / https://blockdmask.tistory.com/335
profile
Hello, Rabbit

0개의 댓글