Iterate Through Each Digit in a Number

Iterate through each digit in a number

You can use a modulo 10 operation to get the rightmost number and then divide the number by 10 to get the next number.

long addSquaresOfDigits(int number) {
long result = 0;
int tmp = 0;
while(number > 0) {
tmp = number % 10;
result += tmp * tmp;
number /= 10;
}
return result;
}

You could also put it in a string and turn that into a char array and iterate through it doing something like Math.pow(charArray[i] - '0', 2.0);

Iterate over digits of number

You could map the string to int:

for c in map(int, string):
# do stuff with c

Demo:

>>> for c in map(int, '100124'):
... c
...
1
0
0
1
2
4
>>>

How to iterate through an integer's digits in C?

You have several problems in your code.

The first is doing

x = scanf("%d", &x);

scanf() sets its argument x to the input, and then returns the number of variables that it assigned, which is 1. Then the x = assignment replaces the input with 1. So no matter what you enter, you're setting x to 1. Just use scanf("%d", &x) without assigning to the variable as well.

The next problem is your code to loop through the digits. The % operator returns the remainder in a division. So x % 10 returns the remainder when dividing x by 10, which is the last digit of the number (e.g. 123 / 10 returns 12 with a remainder of 3). When you do

x = x % 10;

you get into an infinite loop, because once you get to a single digit, you just keep setting x to itself (unless the last digit is 0, which fails the while (x > 0) condition). That's why your program doesn't seem to do anything -- it never gets out of this loop.

You should be adding the last digit to counter, and then removing the last digit by dividing by 10. So the loop should be:

    while (x > 0)
{
counter = counter + x % 10;
x = x / 10;
}

The corrected code is:

#include <stdio.h>
int main (void)
{
printf("Enter a value.\n");
int x = 0;
scanf("%d", &x);
while (x < 0)
{
printf("Enter another integer please.\n");
scanf("%d", &x);
}
int counter = 0;
while (x > 0)
{
counter = counter + x % 10;
x = x / 10;
}
printf("The total sum of the integer's digits are %d\n", counter);
}

Here's how modulus and division iterates through the digits.

Let's say you have a number x = 5649.

When you calculate x % 10, that returns the last digit, which is 9.
Then you set x = x / 10;, which sets it to 564 -- the number without the last digit.

The next time through the loop, you calculate x % 10, which returns the last digit of this, which is 4.
Then you set x = x / 10;, which sets it to 56 -- again, the number without the last digit.

The next time you calculate x % 10, which returns 6. Then x = x / 10 sets it to 5.

The next time you calculate x % 10, which returns 5. Then x = x / 10 sets it to 0.

Now the loop stops because the condition was x > 0, which is no longer true.

So as you can see, the loop steps through the digits from right to left, because each time it removes the last digit by dividing by 10. Meanwhile, you're adding each digit to counter when you do counter = counter + x % 10;

python - iterating over each digit in number

Assuming by "return" you mean "print". You don't need a loop. Just print the element directly.

>>> seq = ['0', '2757', '2757']
>>> print seq[1]
2757

Javascript: How can I loop through a multi-digit number and find the consecutive digit ^ n of each?

This converts the number to a string splits it into digits then raises each to the power of its index plus one and then reduces via addition to result in the answer:

('' + 345).split('').map(function(v, i) {
return Math.pow(parseInt(v), i+1)
}).reduce(function(a, v) {
return a + v
}, 0)

results in 144



Related Topics



Leave a reply



Submit