Reputation: 448
Say I have a number which is counting from 0 to 10000,
0,1,2,3 ... 9998, 9999, 10000
How am I able to only display the last digit on this number.
eg: 0,1,2,3,4,5,6,7,8,9,0,1,2...
I have this code but i'm not sure how to implement what I want.
if (count_a > 9) {
//something here
count_a = 0; //resets it back to 0
} else { //count_a is less than 9
count_a =((newtime-original_time)/100); //to get ms //this counts up in the way which I have stated above.
How can I loop the ((newtime-original_time)/100)
to go back to 0 once it is >9?
Upvotes: 2
Views: 206
Reputation: 592
To get the last digit. do count_a = num % 10
To loop back the ((newtime-original_time)/100) expression when count_a > 9 (can be re-written as when count_a == 0 since after 9, you get 0 in this case so you code should be :
for(num = 0; num <MAX; num++)
{
count_a = num % 10;
if (count_a == 0)
{
original_time = newtime; // this will make the expression = 0
}
count_a = ((newtime-original_time)/100);
}
Hope this helps :)
Upvotes: 0
Reputation: 19864
I hope this is what you want
num % 10
for digit num
the result of the above operation will always range from 0 to 9.
Upvotes: 2
Reputation: 992955
To extract the last (decimal) digit from a number x
, use:
x % 10
The %
represents the modulo operator, which equals the remainder after dividing by 10.
Upvotes: 4