473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

to reverse the digits of a number

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 3051
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

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

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
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
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
"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
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
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
>"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Brad Tilley | last post by:
A friend of mine wrote an algorithm that generates strings. He says that it's impossible to figure out exactly how the algorithm works. That may be true, but I think if I had enough sample strings...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
47
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
18
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the...
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
20
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
14
by: mosi | last post by:
Problem: how to get binary from integer and vice versa? The simplest way I know is: a = 0100 a 64 but: a = 100 (I want binary number) does not work that way.
15
by: rajash | last post by:
Thanks for the additional comments. Here is a solution to an exercise I had problems with. I still don't think it's really what's wanted as it uses a "state variable" n - but I can't see how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.