I am not allowed to use abs or any other libraries.
Here is what I’ve tried so far.
#include <stdio.h>
#include <limits.h>
int main()
{
int n = INT_MIN;
if (n == INT_MIN)
{
int a = -n;
a = n % 10;
putchar(a + '0');
}
return (0);
}
The output is always "(" and not the correct answer.
>Solution :
Apply modulus to the negative number first to get a single negative digit. Negate that.
#include <stdio.h>
#include <limits.h>
int main()
{
int n = INT_MIN;
int a = -( n % 10);
putchar(a + '0');
return (0);
}