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;
printf("Please enter a positive integer:");
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");
printf("please enter a positive integer:");
}
Thanks in advance for your help