to reverse the digits of a number

D

dipti

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();


¦ }
 
M

Martin Ambuhl

dipti said:
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 said:
¦ 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;
 
A

Abdo Haji-Ali

dipti said:
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
 
A

Abdo Haji-Ali

dipti said:
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
 
R

Robert Gamble

dipti said:
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
 
K

Kenneth Brody

dipti said:
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:[email protected]>
 
K

Keith Thompson

Abdo Haji-Ali said:
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?

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.
 
B

Barry Schwarz

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
 
P

podduturiniranjan

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.
(e-mail address removed)
+919849238297
 
S

stathis gotsis

dipti said:
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.
 
P

podduturiniranjan

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
 
K

Keith Thompson

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/>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top