print the number 7011 as 011 (take off a 7)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 2
Reputation: mmcshmi11 is an unknown quantity at this point 
Solved Threads: 0
mmcshmi11 mmcshmi11 is offline Offline
Newbie Poster

print the number 7011 as 011 (take off a 7)

 
0
  #1
Feb 5th, 2008
I need to print out a number that begins with 7, without the seven. For example, print 71011 as 1011 or 701 as 01. I've tried many techniques and just can't get it to work. Is there a printf format that will let me print out everything left of the decimal minus that very left number? I've also tried to make a string that will contain the 0's and 1's, but not the 7... it is not working however.

Any help would be greatly appreciated. Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: print the number 7011 as 011 (take off a 7)

 
0
  #2
Feb 5th, 2008
Use sprintf to convert the number to a string and then print all but the first character:
  1. sprintf ( buffer, "%d", value );
  2. puts ( &buffer[1] );
Last edited by Narue; Feb 5th, 2008 at 5:24 pm.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 431
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 52
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: print the number 7011 as 011 (take off a 7)

 
0
  #3
Feb 5th, 2008
try the modulus operator %

ie

int x = (71011 % 70000);

and x = 1011
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: mmcshmi11 is an unknown quantity at this point 
Solved Threads: 0
mmcshmi11 mmcshmi11 is offline Offline
Newbie Poster

Re: print the number 7011 as 011 (take off a 7)

 
0
  #4
Feb 5th, 2008
Thanks for the help guys, I finally got it working with the sprintf tip. The modulus wont work if my number starts with 0 however, learned that the hard way already, but thanks for the help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: print the number 7011 as 011 (take off a 7)

 
0
  #5
Feb 6th, 2008
>The modulus wont work if my number starts with 0
Your number won't start with 0 if you store it in an integer type. Leading zeros are ignored on formatted integer input and during integer processing. The only way to get the leading zero is to convert the number to a formatted string that specifies a field width and zero fill:
  1. sprintf ( buffer, "%0*d", digits + 1, value );
The real problem with the remainder operator is that you're extracting the most significant digit, which involves finding the width of the number to create an appropriate mask if the value can have a variable number of digits:
  1. #include <stdio.h>
  2.  
  3. int get_mask ( int value )
  4. {
  5. int mask = 1;
  6.  
  7. do {
  8. mask *= 10;
  9. value /= 10;
  10. } while ( value > 9 );
  11.  
  12. return mask;
  13. }
  14.  
  15. int main ( void )
  16. {
  17. int value = 123;
  18.  
  19. value %= get_mask ( value );
  20. printf ( "%d\n", value );
  21.  
  22. return 0;
  23. }
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: print the number 7011 as 011 (take off a 7)

 
0
  #6
Feb 6th, 2008
Alternative way from Narue:

  1. // input your num here.
  2. for (temp = num; temp > 9; temp /= 10)
  3. printf("%c", '0' + (temp % 10));
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: print the number 7011 as 011 (take off a 7)

 
0
  #7
Feb 6th, 2008
>Alternative way from Narue:
Maybe I missed something, but I don't think the original question included reversing the digits as well as trimming the most significant digit.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: print the number 7011 as 011 (take off a 7)

 
0
  #8
Feb 6th, 2008
Originally Posted by Narue View Post
>Alternative way from Narue:
Maybe I missed something, but I don't think the original question included reversing the digits as well as trimming the most significant digit.
Sorry. Without much thinking, I jumped to answer this question.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 969 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC