get all the right digits

This is a discussion on get all the right digits within the C Programming forums, part of the General Programming Boards category; How can i get the all right digit.... eg. input digit is 332, output is 32. Please see the below ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    get all the right digits

    How can i get the all right digit.... eg. input digit is 332, output is 32. Please see the below code and help me thanks

    Code:
    //Get all the right digits
    int getRightDigit(int id)// eg. 332
    {	int rtn;
    	if (id < 10)
    		return id%10;
    	rtn = getRightDigit(id/10);
    	printf("%d",rtn);
    	return rtn;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    the above code don't give u wanted, check out this

    Code:
    void getRightDigit(int id)
    {
     	 if(id==0)
     	      return;
               else
         	 printf("%d  ",id%10);
         	 getRightDigit(id/10);
    }
    ssharish2005

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Don't know witch one you want.
    Code:
    #include <stdio.h>
    
    //Get all the right digits
    //returns id if id < 10
    int getRightDigit(int id) {
        int div = 10;
        while (( id / div )  > 10 )
            div *= 10;
        return id % div;
    }
    
    //Get all the right digits
    //returns 0 if id < 10
    int getRightDigit1(int id) {
        if ( id < 10 ) return 0;
        int div = 10;
        while (( id / div )  > 10 )
            div *= 10;
        return id % div;
    }
    
    int main(void) {
      printf( "%d \n" , getRightDigit(332) );  
      printf( "%d \n" , getRightDigit1(2) );  
      return 0;
    }
    Kurt

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Thanks for your reply...but the code can't output 32 when i input 332...thanks

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    there u go
    Code:
    void getRightDigit(int id)
    {
     	 if 
    	  (id==0)
     	 		return;
         else
         	 printf("%d  ",id%100);
         	 getRightDigit(id/100);
    }
    /*my output
    32  3
    */
    ssharish2005

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What's wrong with my solution ?
    Kurt

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Sorry Kurt...It's something error in my main program, so that it's wrong return...sorry ...thanks million for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  5. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 09:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21