Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I print the last digit of INT_MIN integer using putchar only?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading