435,061 Members | 1,563 Online
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

# to reverse the digits of a number

 P: n/a Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting display. i hve. tried this using long int also. ¦ void main() ¦ { ¦ clrscr(); ¦ int a,i,d,sum=10000,sum1=0; ¦ printf("Enter A Five Digit No.of Your Choice:"); ¦ scanf("%d",&a); ¦ ¦ for(i=0;i<=4;i++) ¦ { ¦ d=a%10; ¦ a=a/10; ¦ d=d*sum; ¦ sum1=sum1+d; ¦ sum=sum/10; ¦ printf("%d\n",sum1); ¦ } ¦ printf("%d"\n",sum1); ¦ getch(); ¦ } Apr 19 '06 #1
11 Replies

 P: n/a dipti wrote: Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting I just tried this specific input and it gave me the correct ouput (using VS 2005), are you by any chance using an old 16-bit compiler? display. i hve. tried this using long int also. It won't make a difference, they are the both 32-bit size on a 32-bit compiler... Abdo Haji-Ali Programmer In|Framez Apr 19 '06 #3

 P: n/a dipti wrote: Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting I just tried this specifc input and it gave me the correct results (using VS 2005), are you by any chance using a 16-bit compiler? display. i hve. tried this using long int also. It's the same both are 32-bit size on a 32-bit compiler... [code] Abdo Haji-Ali Programmer In|Framez Apr 19 '06 #4

 P: n/a dipti wrote: Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting display. i hve. tried this using long int also. The next time you post code, please format it so that others can easily copy/paste it into an editor. Remove the broken bars before each line and get rid of the double spacing. ¦ void main() It's int main (void) ¦ { ¦ clrscr(); Not a standard function. ¦ int a,i,d,sum=10000,sum1=0; ¦ printf("Enter A Five Digit No.of Your Choice:"); You need to #include before calling printf. You should either fflush(stdout) or print a newline character to ensure the user actually sees this. ¦ scanf("%d",&a); You need to #include before calling scanf. You should check the return value of scanf, if it fails there will be garbage in your uninitialized a at this point. ¦ ¦ for(i=0;i<=4;i++) This is mostly a style issue but in C we usually prefer for (i = 0; i < 5; i++) ¦ { ¦ d=a%10; ¦ a=a/10; ¦ d=d*sum; ¦ sum1=sum1+d; ¦ sum=sum/10; ¦ printf("%d\n",sum1); ¦ } ¦ printf("%d"\n",sum1); ¦ getch(); Not a standard function. ¦ } missing return statement. Although you use a very round-about algorithm, I don't see anything particularly wrong with it. When I run your program (with modifications to fix the above-mentioned issues) with the specified input I get the expected output. From your description it sounds like you may be experiencing integer overflow or perhaps the call to scanf is failing and since you don't initialize a or check the return value of scanf a would contain garbage. What EXACTLY is the troublesome input and output? What is the max value of int on your system? What happens to the output if you use unsigned int instead? Robert Gamble Apr 19 '06 #5

 P: n/a dipti wrote: Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting display. Have you considered the possibility that your platform has 16-bit ints, where "43211" won't fit into an "int"? Is "75536" treated as "10000"? i hve. tried this using long int also. Did you change the "%d"s to "%ld"s where necessary? [... snip code ...] Also, you don't validate the input as being a number. -- +-------------------------+--------------------+-----------------------------+ | Kenneth J. Brody | www.hvcomputer.com | | | kenbrody/at\spamcop.net | www.fptech.com | #include | +-------------------------+--------------------+-----------------------------+ Don't e-mail me at: Apr 19 '06 #6

 P: n/a "Abdo Haji-Ali" writes: dipti wrote: Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value like 43211 it's not working,some value with '-' sign preceding it is getting I just tried this specific input and it gave me the correct ouput (using VS 2005), are you by any chance using an old 16-bit compiler? display. i hve. tried this using long int also. It won't make a difference, they are the both 32-bit size on a 32-bit compiler... Assuming the OP using a "32-bit compiler" (it's not even clear what that means). The standard merely requires int to be at least 16 bits. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Apr 19 '06 #7

 P: n/a On 19 Apr 2006 09:36:00 -0700, "dipti" wrote: Hi, Can u guys please tell me what's wrong with the following codeas on givinga simple input value like: 10001, 10032, 10432, everything seems to beworking fine but on giving a input value like43211 it's not working,some value with '-' sign preceding it is getting Since the code doesn't compile, how can you claim it works? display.i hve. tried this using long int also.¦ void main() int main(void) if you please. What is with all the wasted vertical space? ¦ {¦ clrscr(); A non-standard, and in this case totally useless function, which does nothing but present problems for someone who may be inclined to help. ¦ int a,i,d,sum=10000,sum1=0;¦ printf("Enter A Five Digit No.of Your Choice:"); Since your text does not end with a \n, you need a fflush(stdout) here. ¦ scanf("%d",&a); This leaves the \n (from the ENTER key) in the buffer. ¦ for(i=0;i<=4;i++)¦ {¦ d=a%10;¦ a=a/10;¦ d=d*sum;¦ sum1=sum1+d;¦ sum=sum/10;¦ printf("%d\n",sum1);¦ }¦ printf("%d"\n",sum1); Look at this line carefully. See if if if you have something extraneous in it. ¦ getch(); Another non standard function. Would not getchar do just as well? In any case, this will eat the \n left in the buffer without waiting for you type anything in. It therefore doesn't accomplish its intended function. ¦ } After correcting the above problems, it ran fine on my 32 bit system with 43211 as the input. By any chance does your system use 16 bit int? If so, 43211 is out of range for an int and you have invoked undefined behavior. Remove del for email Apr 20 '06 #8

 P: n/a Hello, Just write the same code in vc++ 6.0 & remove clrscr from ur code.. may be it will work. I hope it will work for you... & in present working environment try for following sample values... 32765,32766,32767,32768,32769,32770,32771,32772. And reply me whats the result Niranjan Podduturi. po***************@yahoo.com +919849238297 Apr 20 '06 #9

 P: n/a >"dipti" wrote in messagenews:11*********************@e56g2000cwe.googlegr oups.com...Hi, Can u guys please tell me what's wrong with the following codeas on givinga simple input value like: 10001, 10032, 10432, everything seems to >beworking fine but on giving a input value like43211 it's not working,some value with '-' sign preceding it is getting display.i hve. tried this using long int also. [snip code] Your code had the problems other people pointed out. If you just want to print the reversed digits and not store the corresponding value you can try this function: void reverse_digits(unsigned int a) { while (a) { printf("%d",a % 10); fflush(stdout); a/=10; } } including all the necessary header files. Apr 20 '06 #10

 P: n/a Hello, Your code is working for any kind of values. i tested ur code on vc++6.0 the problem is ur compiler is not supporting that big values. so take care about data types Niranjan Apr 24 '06 #11

 P: n/a po***************@gmail.com writes: Hello, Your code is working for any kind of values. i tested ur code on vc++6.0 the problem is ur compiler is not supporting that big values. so take care about data types Niranjan We have no idea what you're talking about because you didn't provide any context. Most of us can't see the article you're replying to. Read . -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Apr 24 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.