473,218 Members | 1,696 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,218 software developers and data experts.

Reverse a number

Que- If a five digit number is input through the keyboard, write a program to reverse the number.

Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.    int num=12345, res=0, rem;
  4.    clrscr();
  5.    while(num > 0)
  6.    {
  7.       rem=num% 10;
  8.       res=res* 10 + rem;
  9.       num=num/10;
  10.    }
  11.    printf("Reverse of a number=%d",res);
  12.    getch();
  13. }
According to me logic is rite, but the result coming out to be is not correct... Check it our,,, & reply
Mar 19 '08 #1
10 4207
Sick0Fant
121 100+
Que- If a five digit number is input through the keyboard, write a program to reverse the number.

void main()
{
int num=12345, res=0, rem;
clrscr();
while(num > 0)
{
rem=num% 10;
res=res* 10 + rem;
num=num/10;
}
printf("Reverse of a number=%d",res);
getch();
}

According to me logic is rite, but the result coming out to be is not correct... Check it our,,, & reply
It should (and does) work.
Mar 19 '08 #2
If we enter the number upto four digit,answer is rite, Suppose I enter 1234 Answer is 4321.
But If we enter five digit numb,suppose 12345, Answer is not coming 54321.
If we declare num as long, even then the o/p is not coming out to be correct for 5 digit number.
Mar 20 '08 #3
Sick0Fant
121 100+
Far be it from me to doubt you, but I copied and pasted your code into an editor and compiled it, and it produced the desired output.
Mar 20 '08 #4
Ok thx, but i face still d same probs.
Mar 20 '08 #5
Ganon11
3,652 Expert 2GB
Exactly what output are you getting?

e.g. You enter 12345.

What is output?
Mar 20 '08 #6
When I enter 12345, answer is -11215.
When I enter the numb upto 4 digit, answer is coming correct.
Mar 21 '08 #7
JosAH
11,448 Expert 8TB
When I enter 12345, answer is -11215.
When I enter the numb upto 4 digit, answer is coming correct.
The ints on your system are two bytes wide so they can only store numbers in
the range [-2^15,2^15-1], e.g. the number 54321 - 2^15 == -11215, so your
algorithm is correct, the ints are just to narrow (try four byte longs).

kind regards,

Jos
Mar 21 '08 #8
then wat to do to get the correct answer of Five digit number,,,,,,,Even if I declare the numb as long datatype.........even then the answer is not coming out to be correct
Mar 21 '08 #9
JosAH
11,448 Expert 8TB
then wat to do to get the correct answer of Five digit number,,,,,,,Even if I declare the numb as long datatype.........even then the answer is not coming out to be correct
Make all your variable long ints and print them as such: %ld instead of %d.

kind regards,

Jos
Mar 21 '08 #10
Dear Sonia.;
ur code is absolutely correct. firstly check ur compiler size n gv 3/4 digit num.
as it is working in my sys. i.e visual studio..
ck it out n info.

Regards
Vicky...
Mar 25 '08 #11

Sign in to post your reply or Sign up for a free account.

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...
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
8
by: schaf | last post by:
Hi NG! I have a problem in my remote application. After calling a remote function the calculation will be done by the service. The calculation result will be sent to the caller (client) via...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.