| | |
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:
Solved Threads: 0
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
Any help would be greatly appreciated. Thanks
Use sprintf to convert the number to a string and then print all but the first character:
c Syntax (Toggle Plain Text)
sprintf ( buffer, "%d", value ); puts ( &buffer[1] );
Last edited by Narue; Feb 5th, 2008 at 5:24 pm.
New members chased away this month: 5
>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:
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:
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:
c Syntax (Toggle Plain Text)
sprintf ( buffer, "%0*d", digits + 1, value );
c Syntax (Toggle Plain Text)
#include <stdio.h> int get_mask ( int value ) { int mask = 1; do { mask *= 10; value /= 10; } while ( value > 9 ); return mask; } int main ( void ) { int value = 123; value %= get_mask ( value ); printf ( "%d\n", value ); return 0; }
New members chased away this month: 5
Alternative way from Narue:
C Syntax (Toggle Plain Text)
// input your num here. for (temp = num; temp > 9; temp /= 10) 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
Behind every smile is a tear.
Visal .In
![]() |
Other Threads in the C Forum
- Previous Thread: sorting the "news"
- Next Thread: Division /subtraction of two structures.
Views: 969 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






