Extracting digits from an integer

This is a discussion on Extracting digits from an integer within the C Programming forums, part of the General Programming Boards category; I have to create a program that asks the user for an integer, my program must determine whether or not ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Unhappy Extracting digits from an integer

    I have to create a program that asks the user for an integer, my program must determine whether or not that integer is divisible by 9, also it must display every digit within the integer starting with the rightmost digit. can someone please help me

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    The rule is to post your attempt first and then the play goes on....

    however i am going to post you some code that is going to test a three-digit number..Get some inspiration from it and give it a try
    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int n=152;
        int a,b,c,d,f,g,z;
         a=n/100;
         b=n/10;
         c=n/1;
        printf("a= %d  b = %d  c=%d\n",a,b,c);
        d=n%100;
        f=n%10;
        g=n%1;
        printf("with mod->d= %d  f = %d  g=%d\n",d,f,g);
        z=b%10;
        printf("z=%d\n",z);
        return 0;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Sure, you look up the / and % operators.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    Thanks for the help, i get how to "peel off numbers" but what if my number doesnt have a set amount of digits and the user enters a random number like from 0 - 999999 (just for argument sake)

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    you should think about a loop maybe..you see i can not say more,because i have not see some code of yours :/ :/

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    Ok so heres what I have so far, the example gives a number thats 6 digits long so i just assume the user puts in 6 ( after i get this perfected i would like to know how to have it adjust to the amount of numbers in a random (any digit) number the user wants to input). Also how i make the program check if the original int is divisible by 9?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    /* Determines whether or not the users integer is divisible by 9 and displays all numbers withing integer*/
    
    int
    main(void)
    {
    
        int N;    /* Users inputed integer */
        int d6; /* 6th digit of the number */
        int d5; /* 5th digit of the number */
        int d4; /* 4th digit of the number */
        int d3; /* 3rd digit of the number */
        int d2; /* 2nd digit of the number */
        int d1; /* 1st digit of the number */
        
        
        
        
        printf("To determine wheteher or not your number is divisible by 9,\n");
        printf("please enter your number\n");
         scanf("%d", &N);
        
        
                 d6 = N % 10;
                 d5 = (N / 10) % 10;
                 d4 = (N / 100) % 10;
                 d3 = (N / 1000) % 10;
                 d2 = (N / 10000) % 10;
                 d1 = (N / 100000) % 10;
    
                    printf("%d\n", d6);
                    printf("%d\n", d5);
                    printf("%d\n", d4);
                    printf("%d\n", d3);
                    printf("%d\n", d2);
                    printf("%d\n", d1);
    
              system("pause");
        return(0);
    }
    Last edited by AlexTank853; 09-29-2012 at 02:29 PM.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    ok so i finished the program, The example said to have the digits displayed in reverse order and display to the user whether or not their number was divisble by 9, let me know if I did this correctly please
    Code:
    #include <stdio.h>
    
    /* Determines whether or not the users integer is divisible by 9 and displays all numbers withing integer*/
    
    int
    main(void)
    {
    
    	int N;	/* Users inputed integer */
    	int d6; /* 6th digit of the number */
    	int d5; /* 5th digit of the number */
    	int d4; /* 4th digit of the number */
    	int d3; /* 3rd digit of the number */
    	int d2; /* 2nd digit of the number */
    	int d1; /* 1st digit of the number */
    	
    	printf("please enter your number, must have at 6 digits\n");
    	scanf("%d", &N);
    	
    	if ( N % 9 == 0)
    	   {
    
                   /* Peels off the integer's digits and assigns them to a variable*/
                 d6 = N % 10;
                 d5 = (N / 10) % 10;
                 d4 = (N / 100) % 10;
                 d3 = (N / 1000) % 10;
                 d2 = (N / 10000) % 10;
                 d1 = (N / 100000) % 10;
    	         
                 /*prints out the intger's digits starting with the rightmost digit */
                 printf("rightmost digit: %d\n", d6);
                 printf("%d\n", d5);
                 printf("%d\n", d4);
                 printf("%\n", d3);
                 printf("%d\n", d2);
    	         printf("leftmost digit: %d\n", d1);
    	         
    	         /* Tells the user the number is divisible by 9 */
    	         printf("your number is divisible by 9\n");
          }
       else 
          {
                    /* Peels off the integer's digits and assigns them to a variable*/
                 d6 = N % 10;
                 d5 = (N / 10) % 10;
                 d4 = (N / 100) % 10;
                 d3 = (N / 1000) % 10;
                 d2 = (N / 10000) % 10;
                 d1 = (N / 100000) % 10;
    	         
                 /*prints out the intger's digits starting with the rightmost digit */
                 printf("rightmost digit: %d\n", d6);
                 printf("%d\n", d5);
                 printf("%d\n", d4);
                 printf("%d\n", d3);
                 printf("%d\n", d2);
    	         printf("leftmost digit: %d\n", d1);
    	         
    	         /* Tells the user the number isnt divisible by 9 */
    	         printf("your number is not divisible by 9\n");
          }
          
          	system("pause");
    	return(0);
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're the best one to test and find out whether your program is accurate or not.

    Of interest would be a while loop like (not exactly) this:

    Code:
    char digits[20];
    int i = 0;
    while(number > 0) {
       digits[i]= number % 10   // to get the right most digit
       number /= 10              //reduce the number by one digit
       ++i;
    }
    digits[i]='\0'    //terminate the string
    print %s , digits  //print the string
    The advantage your program has, is it gets the larger digits, sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of digits of an integer: Odd or Even?
    By Devolution in forum C Programming
    Replies: 14
    Last Post: 03-06-2009, 05:24 PM
  2. Integer digits
    By Necrofear in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2007, 08:38 AM
  3. Rightmost digits in integer
    By Nutshell in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2005, 07:21 PM
  4. Separating an Integer into digits
    By Mr Curtains in forum C Programming
    Replies: 2
    Last Post: 03-22-2003, 02:58 AM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM

Tags for this Thread


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