-
get all the right digits
How can i get the all right digit.... eg. input digit is 332, output is 32. Please see the below code and help me thanks
Code:
//Get all the right digits
int getRightDigit(int id)// eg. 332
{ int rtn;
if (id < 10)
return id%10;
rtn = getRightDigit(id/10);
printf("%d",rtn);
return rtn;
}
-
the above code don't give u wanted, check out this
Code:
void getRightDigit(int id)
{
if(id==0)
return;
else
printf("%d ",id%10);
getRightDigit(id/10);
}
ssharish2005
-
Don't know witch one you want.
Code:
#include <stdio.h>
//Get all the right digits
//returns id if id < 10
int getRightDigit(int id) {
int div = 10;
while (( id / div ) > 10 )
div *= 10;
return id % div;
}
//Get all the right digits
//returns 0 if id < 10
int getRightDigit1(int id) {
if ( id < 10 ) return 0;
int div = 10;
while (( id / div ) > 10 )
div *= 10;
return id % div;
}
int main(void) {
printf( "%d \n" , getRightDigit(332) );
printf( "%d \n" , getRightDigit1(2) );
return 0;
}
Kurt
-
Thanks for your reply...but the code can't output 32 when i input 332...thanks
-
there u go
Code:
void getRightDigit(int id)
{
if
(id==0)
return;
else
printf("%d ",id%100);
getRightDigit(id/100);
}
/*my output
32 3
*/
ssharish2005
-
What's wrong with my solution ?
Kurt
-
Sorry Kurt...It's something error in my main program, so that it's wrong return...sorry ...thanks million for your help