Louis Richard Louis Richard - 24 days ago 4
C Question

Extract last value from an integer and convert into a character in C Language

I'm working on a program that takes a seed number and produces a series of random numbers whose last digits form a lottery number. In one example, the last digits of the first four numbers are used (3 4 3 1) but in another the last digits of only the first three are used (5 7 2), so I'm converting the integers to characters to utilize the space in the ASCII table(so that can be the fourth digit).

My first major dead end is figuring out how to extract those last integer values and turn them into characters. So I'd really appreciate help with that. Keep in mind: I can't use if/else statements or relational operators. This program makes use of selection through calculation.


To get the last (smallest) decimal digit of an integer n and convert it into a char do e.g.:

char c = (n % 10) + '0';

n % 10 does a division by 10 and returns the rest which must be a single digit by definition and the addition of the ASCII value of '0' makes a usable character out of that numerical digit.