# 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. ## 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. 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. Sure, you look up the / and % operators.

4. 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. you should think about a loop maybe..you see i can not say more,because i have not see some code of yours :/ :/

6. 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);
}```

7. 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. 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