Thread: How to find digit place in number

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    How to find digit place in number

    How to figure out digit place in given number

    Example
    Input
    Input number: 6294Output
    digit place 1: 6
    digit place 2: 2
    digit place 3: 9
    digit place 2: 4
    What is logic to find digit place in number given number

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would use the logic commonly used to reverse the order or digits.
    In example, the code to change "6294" to "4926".
    But, I have never seen this exact problem.
    If I wished to solve it without using the normal beginner way.
    I would just use snprintf function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by stahta01 View Post
    I would use the logic commonly used to reverse the order or digits.
    In example, the code to change "6294" to "4926".
    Good point. I had not this idea how to change "6294" to "4926" like first see the number in last digit is 4 so store then store it in another variable then see second last digit is 9 then store value in variable

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The way to change "6294" to "4926" in simple beginner code;

    Uses divide by 10 and modulus by 10. The % percent sign is the C modulus operator.

    Tim S.
    Last edited by stahta01; 01-27-2018 at 01:22 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by stahta01 View Post
    The way to change "6294" to "4926" in simple beginner code;

    Uses divide by 10 and modulus by 10. The % percent sign is the C modulus operator.

    Tim S.
    I can do it but I am looking another way
    Code:
    #include<stdio.h>
    
    int main (void)
    {
    	unsigned int number = 6294;
        int	reverse=0; 
    	
    	while (number != 0)
    	{
    		reverse = reverse * 10;
    
    
    		reverse = reverse + number % 10;
    
    
    		number   = number / 10;
    	}
    
    
    	printf("Reverse number is = %d \n", reverse);
    		
    	return 0;
    }
    Reverse number is = 4926

    Do you know any another way like I was saying ?

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    How about this, just uses inter math.
    If the division return zero then you know the number does not take up that place holder

    6294/1000=6

    6294-6*1000= 294 subtract last result to get next number

    294/100=2

    294-200=94

    94/10=9

    94-90=4

    4/1=4

    4-4*1=0 your done !

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vead View Post
    I can do it but I am looking another way
    Code:
    #include<stdio.h>
    
    int main (void)
    {
    	unsigned int number = 6294;
        int	reverse=0; 
    	
    	while (number != 0)
    	{
    		reverse = reverse * 10;
    
    
    		reverse = reverse + number % 10;
    
    
    		number   = number / 10;
    	}
    
    
    	printf("Reverse number is = %d \n", reverse);
    		
    	return 0;
    }
    Reverse number is = 4926

    Do you know any another way like I was saying ?
    Yes, I already said I would use snprintf; but, that is NOT the normal beginners way.
    Edit: And, just use the string reverse function.
    You code is the the normal beginners way to reverse a number.

    Tim S.
    Last edited by stahta01; 01-28-2018 at 02:40 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion to find sum of digits of n digit number ?
    By justine in forum C Programming
    Replies: 7
    Last Post: 11-26-2012, 05:35 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM

Tags for this Thread