Thread: get digit from left to right

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

    get digit from left to right

    I want to write a program that can get the left most digit each time , the below program can get the the left most digit at first, but how can i get the others eg. input digit 33332.....Please help!!Thanks

    Code:
    int getLeftDigit(int n)
    {     if (n<10)
             return n;
           rtn = getLeftDigit(n/10);
           return n;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Your code has an undeclared variable.

    Code:
    #include <stdio.h>
    	
    int main() {
         int digits = 834541;
         
         printf("%d \n", digits / 100000);
         printf("%d \n", (digits % 100000) / 10000);
         printf("%d \n", (digits % 10000) / 1000);
         printf("%d \n", (digits % 1000) / 100);
         printf("%d \n", (digits % 100) / 10);
         printf("%d", digits % 10);
         
         return 0;
    }
    Output:
    Code:
    8
    3
    4
    5
    4
    1
    Last edited by SlyMaelstrom; 11-24-2005 at 01:38 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 05:16 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM