Thread: Finding each digit of a number

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    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!!
    Last edited by rmmstn; 10-17-2020 at 05:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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").

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    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. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    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;
    }
    Last edited by Salem; 11-04-2020 at 11:02 PM. Reason: snipped URLs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-07-2020, 01:46 AM
  2. Finding the first digit in a number....help
    By patso in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 07:10 PM
  3. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  5. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM

Tags for this Thread