Saturday, January 26, 2013

Separating Digits in an Integer

/* This program separates a five-digit number into its individual digits */

int main (void) {
    int number;           /* number entered by user  */
    int placeHolder;    /* stores the remainder       */
    int firstDigit;    
    int secondDigit;
    int thirdDigit;
    int fourthDigit;
    int fithDigit;
 
    /* Get number from user */

    printf("Please enter a five-digit number:\t");
    scanf("%d",&number);
 
   /* Separate the digits */
 
    firstDigit  = number / 10000;                /* getting the first digit         */
    placeHolder = number % 10000;
 
    secondDigit = placeHolder / 1000;      /* getting the second digit    */
    placeHolder = placeHolder % 1000;
 
    thirdDigit  = placeHolder / 100;          /* getting the thrid digit         */
    placeHolder = placeHolder % 100;
 
    fourthDigit = placeHolder / 10;          /* getting the fourth digit        */
    placeHolder = placeHolder % 10;
 
    fithDigit = placeHolder;                     /* getting the fith digit            */
 
    /* Print the digits individually */
 
    system ("cls");
    
    printf(" %d \n %d \n %d \n %d \n %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit,          fithDigit);
    
    system("pause");
    return 0;
 
} /* end main function */



No comments:

Post a Comment