get digit from left to right

This is a discussion on get digit from left to right within the C Programming forums, part of the General Programming Boards category; I want to write a program that can get the left most digit each time , the below program can ...

  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,069
    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 12: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, 04: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, 03:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM

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