# Thread: Displaying the rightmost digit of a positive number

1. ## Displaying the rightmost digit of a positive number

Hi all,

I recently received the following programming assignment and am a little stumped. The question is as follows:

"Write a program to take a non-negative integer and display its rightmost (least significant) digit, and the number with its rightmost digit zeroed.

Example input/output:

Please enter a non-negative integer >> 128
Right-most digit = 8
Number with right-most digit zeroed = 120

Hint: Consider using the following operators: / and % (divide and modulus)"

As for the first part, displaying the rightmost digit, I have no idea how to approach it so if anyone could simply start me off that'd be great.

To display the number with its rightmost digit zeroed I know how to do it if the user enters a number already zeroed eg 130 but I do not know how to change say 135 to 130. The only piece of code I have so far is as follows(and is only a rough draft):

Code:
```#include <stdio.h>

int main (void)
{
int pos_num, num_zeroed = 0;

scanf("%d", &pos_num);
if (pos_num >= 0)
{
if (pos_num % 10 == 0)
{
num_zeroed = pos_num;
printf("The number zeroed is %d\n", num_zeroed);
}

}
else
printf("Number entered must be a positive integer\n");
}```

2. Hint: Consider using %.

3. Then perhaps you should take some time out to brush up on basic math skills before diving into programming.

4. I second the suggestion that you brush up on some basic arithmetic. I would also second the suggestion you look into modulus. Here's a very suggestive hint:

122 mod 10 = 2

5. Yep I get it thanks. Also how to zero the number:
Code:
```if (x % 10 == 0)//if number is evenly divisible by ten the right most digit is already 0
{
num_zeroed = x;
printf("The number zeroed is %d\n", num_zeroed);
}

else
{
num_zeroed = x - x%10;
printf("The number zeroed is %d\n", num_zeroed);
}```

6. More basic math. What happens when you divide by ten and then multiply by ten?

Quzah.

7. It'll give me the same result as the code above.
The code above works fine as far as I can tell.