By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
11 Replies


P: n/a
dipti wrote:
Hi, Can u guys please tell me what's wrong with the following code void main() ^^^^
dead already. main has a return type of int {
clrscr(); ^^^^^^
dead again. Not only is clrscr() not a standard function, there
is no declaration or definition in your code, not even the inclusion of
a non-standard header. int a,i,d,sum=10000,sum1=0;
printf("Enter A Five Digit No.of Your Choice:"); ^^^^^^
dead again. No declaration, not even the inclusion of <stdio.h> scanf("%d",&a); ^^^^^
dead again. No declaration, not even the inclusion of <stdio.h>
In addition, the prompt is followed by neither a '\n' nor call to
'fflush(stdout);' so there is no reason to suppose the prompt appears.
for(i=0;i<=4;i++)
{
d=a%10;
a=a/10; a /= 10; would do as well, without the two references to a d=d*sum;
sum1=sum1+d;
sum=sum/10; This looks incoherent.
It would seem that you actually want code like
[instead of the whole for(i... loop]
if (a < 0) {
putchar('-');
a = -a;
}
sum = 0;
while(a) {
sum *= 10;
sum += a%10;
a /= 10;
printf("%d\n",sum);
}
printf("%d\n",sum1);
}
printf("%d"\n",sum1); And this is unneeded. getch(); dead again. Not only is getch() not a standard function, there
is no declaration or definition in your code, not even the inclusion of
a non-standard header.

If you are not using a C99 compiler, you need (and should have, even
using a C99 compiler)
return 0;
}

Apr 19 '06 #2

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 <stdio.h> 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 <stdio.h> 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 <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Apr 19 '06 #6

P: n/a
"Abdo Haji-Ali" <ah***@inframez.com> 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 <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
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" <de*********@yahoo.com> 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
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" <de*********@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegr oups.com...
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.


[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 <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
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.