# Thread: Finding each digit of a number

1. ## Finding each digit of a number

Hello, I'm a newbie in C but I know how to code in C++ and I feel like I'm struggling with more complicated problems because I can't get my head around some basic principles. For instance, I know that the algorithm of determining each digit of a number in C++ is this one:
Code:
```#include <iostream>
using namespace std;
int n, lastDigit;
int main()
{
cin>>n;
while(n!=0)
{
lastDigit=n%10;
n=n/10;
cout<<lastDigit<<' ';
}
return 0;
}```
If my input for this was 548 I would get 8 4 5. Now, what I tried to do was 'translate' this basic algorithm from C++ to C, to better understand how I could obtain the digits of a number in C:
Code:
```#include <stdio.h>
int digits(int n){
int uc;
while(n!=0){
uc=n%10;
n=n/10;
}
return uc;
}
int main(){
int x;
x=548;
printf("The digits of the number %d are %d", x, digits(x));
return 0;
}```
However, for the same number 548, I get the message "The digits of the number 548 is 5". And I just don't understand why my while is being treated as an if in this instance. Please explain as detailed as possible, as I really want to understand. Thank you!!

2. Notice that in your C++ code you're printing each digit in the loop, whereas in your C code you're only returning the last (and hence the first, lexically speaking) digit to be printed.

3. Thank you for replying, I edited the code like this:
Code:
```#include <stdio.h>
int digits(int n){
int uc;
while(n!=0)
{
uc=n%10;
n=n/10;
printf("%d ", uc);
}
}
int main(){
int x;
x=548;
printf("The digits of the number %d are %d ", x, digits(x));
return 0;
}```
However my output is now "8 4 5 The digits of the number 548 are 2". So I did manage to get the digits but how can I print them nicely in main, also I still don't really understand why it didn't work then but it does now, because even before, when I returned uc in the loop, I would get as output "The digits of the number 548 are 8". Could you please explain a bit more?

4. Originally Posted by rmmstn
also I still don't really understand why it didn't work then but it does now, because even before, when I returned uc in the loop, I would get as output "The digits of the number 548 are 8". Could you please explain a bit more?
Look at this line:
Code:
`uc=n%10;`
So, uc is the current "last digit" or current "right-most digit" or current "least significant digit" of the number. If you return uc in the body of the loop, you therefore return the very first such digit, e.g., if your input is 548, you will return 8, because 8 is the very first "right-most digit", and you never loop more than once. If you return uc after the loop, which is what you did in post #1, you therefore return the very last such digit, e.g., if your input is 548, you will return 5, because 5 is the very last "right-most digit" (i.e., it is the "left-most digit").

Originally Posted by rmmstn
However my output is now "8 4 5 The digits of the number 548 are 2". So I did manage to get the digits but how can I print them nicely in main
Just print x in main

Okay, a more serious answer: the crux of the problem is that you have no way of returning all the digits to the caller (i.e., main) separately. There are ways of doing this, e.g., you can pass an array of digits to be used as an output parameter and return the count of digits. This way, the caller can access the digits separately through the array and will know how many digits in the array to use by saving the return value of the function call.

5. Why did you complicate your c++ code with an extra function ?
A one to one translation looks like this.
Code:
```#include <stdio.h>

int n, lastDigit;

int main()
{
scanf("%d", &n);
while (n != 0)
{
lastDigit = n % 10;
n = n / 10;
printf("%d ", lastDigit);
}
return 0;
}```

6. ## I think this will work fine.

Here is the simple approach.

Code:
```#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n<0)
{
int digit=n%10;
printf("%d",digit);
n=n/10;
}
return 0;
}```