How to print digit by digit of a number? [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Identify the digits in a given number.
I need to print each digit of number without convert it to string. It's possible?
For example:
int n = 1234;
int x;
while( ?? ) {
x = ??
printf("%d\n", x);
}
it prints:
1
2
3
4
I have no idea how to do this. Thanks in advance.

Unless you tell me this isn't homework, I won't give the full answer.
Note that x / 10 gives you x stripped of the last digit. So 123 / 10 = 12, 45 / 10 = 4, etc.
And note that x % 10 gives you the last digit of x. So 123 % 10 = 3, 45 % 10 = 5, etc.

This actually touches on number theory / mathematical methods. You can write a number 1234, for example, as 1x10^3 + 2x10^2 + 3x10^1 + 4x10^0. Now think about how you can use % (mod) and / (integer division) to extract each digit.

If you know the maximum number of digits, you can do it using / and %
so for instance, if you want to find the thousands place, the answer is
num =...
int thousands = (num / 1000) % 10
you can actually do this in a loop

Related

What is the purpose of the % in the following code? [duplicate]

This question already has answers here:
How to make sense of modulo in c
(2 answers)
Closed 1 year ago.
I am making a snake game for my university assignment. In C. Now, I know this isn't the ideal langiage for the task but it's my assignment. I found an example code anda am currently rewriting it / understanding it so I can write my own working code. So far it's an easy ride and it's all coming together but for the life of me I can't figure out why they put a the % in the following lines:
for (i = 1; i <= 1 + rand() % 10; i++)
food.x = 12 + rand() % (68 - 12);
for (i = 1; i <= 1 + rand() % 10; i++)
food.y = 12 + rand() % (28 - 12);
For context this is in the function for creating food in the playing field. I understand the rand thing and adding these numbers (it's my playing field size) but why put a modulo division? What does it achieve?
% is the modulo operator, mostly used like in this scenario to limit a value see modulo operator wiki
f.e.
1 + rand() % 10 means the value will be 1 + [0..9] - so max is 10. [0..9] is the random number modulo 10, and will be a number between 0 and 9.
% is the "modulo" operator , i.e.: it's the remainder of the integer division of its two operands.
That's one way to guarantee the output of rand() is within a certain range.
e.g.: rand()%100 will be between 0 and 99
In other words: 12 + rand() % (68 - 12) will be between 12 and 67
Because: rand()%(68-12) is between 0 and 68-12-1
And so: 12+rand()%(68-12) is between 12 and 67

How do I manipulate the digits of a number?

In some kinds of problems I often face this situation where I have a variable where I need to rearrange the digits, e.g., int e = 2385;. Let's suppose I don't know which number is stored there, but still I need to shift 2nd and 4th position. When I know variable's value I can simply do e = 2583, but when I don't know I simply can't solve the problem.
Another situation is when I have two values and want to use them to form another number i.e int a = 2, b = 1;, and I need to order them so that I will get a 21 or a 212. I mean, that's easy to do when I'm outputting data, I can simply do:
printf("%d%d\n",a,b);
printf("%d%d%d",a,b,a);
Problem is when I have to store this number in another variable. I don't know how to do that.
This can be broken into two tasks: (1) breaking up a number into pieces, and (2) combining pieces into a whole number.
To break up a number into pieces, use the division and modulus operators.
int num = 2385;
int a = num / 1000; // 2
int b = num / 100 % 10; // 3
int c = num / 10 % 10; // 8
int d = num / 1 % 10; // 5
The trick is to use division to remove the digits to the right, then modulus to keep only the rightmost digit. For example, to calculate the hundreds' place (b) we compute 2385 / 100, which is 23. 23 % 10 is the remainder when you divide 23 by 10. The remainder is 3.
To combine the pieces back into a number, do the opposite with multiplication and addition.
num = a * 1000 // 2000
+ d * 100 // + 500
+ c * 10 // + 80
+ b * 1; // + 3
// ----
// 2583
Notice how I switched d and b to swap those digits.

Generate random numbers with rand() except number zero [duplicate]

This question already has answers here:
How to generate a random integer number from within a range
(11 answers)
Closed 8 years ago.
I would like to generate random numbers between 1 and 25 with the method rand().
But I only know how to generate random numbers this way, which includes the number zero by default:
int r = rand() % 26 /* random int between 0 and 25 */
Anyone? Thank you.
Very simple
int r = 1 + rand() % 25 /* random int between 1 and 25 */
but you should use this
int r = (int)(1.0 + 25.0 * rand() / RAND_MAX)
as mentioned in the comments, the second is the more robust way to generate random numbers see this link

What is the meaning of the percent character in this code?

In this case, what does the percentage refers to?
int myInt = 27 % 10;
myInt = 7;
What does the % mean in this code?
% means remainder, when 27 is divided by 10 leaves a remainder 7
EDIT:
My 2 cents about all the discussion about difference between modulo & remainder
Take a % b
1. When both +ve, Modulo & Remainder are one and the same
2. When a is -ve, they are not the same
For example;
a = -10, b = 3
Remainder of -10 % 3 = -1
for Modulo, add a greater multiple of 3 to your 'a' and calculate the remainder.
-10 + 12 = 2
2 % 3 = 2 is your answer
the % is modulus operator, not percentage. For percentage, you just do regular math. 50% is to multiply by .5... etc.
For future reference, the objective c mathematical operations are documented many places, including here.
Note the % is called "Modulo" operator.
% is a operator to find the remainder of a division.
The "%" in this code is called the modulus operator. This causes the processor to perform a division operation, and it returns the remainder of the division.
For example:
8 % 10 = 8
5 % 4 = 1

How to split up a two-digit number in C

Let's say I have a number like 21 and I want to split it up so that I get the numbers 2 and 1.
To get 1, I could do 1 mod 10. So basically, the last digit can be found out by using mod 10.
To get 2, I could do (21 - (1 mod 10))/10.
The above techniques will work with any 2-digit number.
However, let me add a further constraint, that mod can only be used with powers of 2. Then the above method can't be used.
What can be done then?
2 == 23 / 10
3 == 23 - (23 / 10) * 10
To get 2 you can just do
int x = 23 / 10;
remember that integer division drops the fractional portion (as it can't be represented in an integer).
Modulus division (and regular division) can be used for any power, not just powers of two. Also a power of two is not the same thing as a two digit number.
To split up a three digit number
int first = 234/100;
int second = (234/10)-first*10;
int third = (234/1)-first*100-second*10;
with a little work, it could also look like
int processed = 0;
int first = 234/100-processed;
processed = processed + first;
processed = processed * 10;
int second = 234/10-processed;
processed = processed + second;
processed = processed * 10;
... and so on ...
If you put a little more into it, you can write it up as a loop quite easily.
what about
x%10 for the second digit and
x/10 for the first?

Resources