HELP WITH FINAL TOUCH OF PROGRAM!!

I already have all but it has to accept negative numbers but I don't know how:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main()
{
int number, right_digit;

printf("Enter your number .\n");
scanf("%i", &number);

do {
right_digit = number % 10;
printf("%i", right_digit);
number = number / 10;
} while (number != 0 );

printf("\n");

getchar(); //keeping the output window open

return 0;
}
check if it is negative, if it is invert it and print a - at the end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

int main()
{
int number, right_digit;

printf("Enter your number .\n");
scanf("%i", &number);

int negative = number < 0;
if(negative)
    number = -number;

do {
right_digit = number % 10;
printf("%i", right_digit);
number = number / 10;
} while (number != 0 );

if(negative)
    printf("-");
printf("\n");

getchar(); //keeping the output window open

return 0;
} 
Last edited on
Topic archived. No new replies allowed.