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;

Recommended Answers

All 14 Replies

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

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

Above code will print

5
45
345

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

commented: Not easy 4 newbie +0

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!

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

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

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

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]);


}

i think adak solved the problem and its appreciable if you please follow it

i think mfaisalm well solved the problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.