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;

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

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 5 Years Ago by gerard4143: n/a

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 ();
}


please check my code, and correct my mistakes, tnx in advance!

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

please help me on this one, cause i want to learn more on C.

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

I just merged Adak's and Your code.
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]);


}
This article has been dead for over six months. Start a new discussion instead.