hi! iam a newbie how can i get the 10th, 100th, 1000th place for example: the interger is 1235, the ones place is 5, the tenth place is 3, the hundreds place is 2, and the thousands place is 1, please help me and how do you program it at least give me an idea;

6
Contributors
14
Replies
16
Views
7 Years
Discussion Span
Last Post by sharathg.satya

hi! iam a newbie how can i get the 10th, 100th, 1000th place for example: the interger is 1235, the ones place is 5, the tenth place is 3, the hundreds place is 2, and the thousands place is 1, please help me and how do you program it at least give me an idea;

or least give me a free code

We are not here to give you FREE CODE ..

Edited by Shankye: n/a

or at least give me an idea on how, because i dont know how to and im very confused, please help me, please understand im a newbie, dont worry i dont hacked but i just want to improve my skill in C,

Try working with the modulus operator...Like below

``````#include <stdio.h>

int main(int argc, char**argv)
{
fprintf(stdout, "%d\n", 12345 % 10);
fprintf(stdout, "%d\n", 12345 % 100);
fprintf(stdout, "%d\n", 12345 % 1000);
return 0;
}``````

This should give you a good start...

Edited by gerard4143: n/a

Above code will print

``````5
45
345``````

I think using Shift operator will be a easier way for you ..

Not easy 4 newbie

Consider the number 123:

``````while number is greater than 0
one = 123 % 10
number = number/10  //remove the digit in the one's place from the variable number.
loop``````

That's all there is to it. It peels off the digits, one by one, from the right hand side. Count the number of times it loops, if needed.

here's my code, and still i cant do it exactly

#include <stdio.h>

int main (void)
{

int digit;
int ones,tens,hundreds;

clrscr ();

printf("enter 3 digits:");
scanf("%d", digit);

ones = digit / 10;
tens = digit / 100;
hundreds = digit / 1000;

printf("the ones place is: %d", ones % 10);
printf("the tens place is: %d", tens % 100);
printf("the hundreds place is: %d", hundreds % 1000);

getch ();
}

Just refer to Adak's posting, he pretty much solves it for you.

Edited by gerard4143: n/a

thanks, i got the ryt code but the output must hav only one number, example:

numbers 123 and the ones place is: 3, the tens place is: 2 "not 23", the hundreds place is: 1 "not 123".

Divide number by 10 every time before going to next place ..
Try doing how Adak said ...

``````printf("%d",number%10) ;
number = number/10 ;
printf("%d",number%10) ;``````

try to do this in loop ..

And I say again...Adak pretty much solves it for you. Please look at his posting.

Pls study the changes.

``````#include<stdio.h>
int main (void)
{

int digit;
int position[3],i;

printf("enter 3 digits:");
scanf("%d",&digit);

for(i = 0; digit > 0; i++)
{
position[i] = digit % 10;
digit=digit/10;
}
printf("The ones place is: %d\n", position[0]);
printf("The tens place is: %d\n", position[1]);
printf("The hundreds place is: %d\n", position[2]);

}``````