473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop to print reverse of any size number

40 New Member
The problem is input a number then the output should be in reverse order..
I already made a code but only good for 9 - digit only...What I want to do is to hold infinite digit, in other words don't have a limit on how many digit that I want to input..

for (x = y;x>0;x=x/10){
z = x % 10;
cout<<z;
}

* code good for 9 - digit only, if I input 10 digit its output is not correct anymore..I used int as data type
Pls. Help
Sep 27 '07 #1
6 6880
ruskalym
65 New Member
Expand|Select|Wrap|Line Numbers
  1. Code has been removed.  Please see our Posting Guidelines.
  2.  
  3. MODERATOR
In recursion we trust!

Just call revhat() with your number.
Sep 27 '07 #2
bitong
40 New Member
What's revhat()? could you please elaborate more?

Expand|Select|Wrap|Line Numbers
  1. Code has been removed.  Please see our Posting Guidelines.
  2.  
  3. MODERATOR
In recursion we trust!

Just call revhat() with your number.
Sep 28 '07 #3
Ganon11
3,652 Recognized Expert Specialist
That post had some full code, which is not allowed (see our Posting Guidelines).

I'm having trouble understanding why this code doesn't work universally...
Sep 28 '07 #4
Ganon11
3,652 Recognized Expert Specialist
This simple code compiled:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     cout << "Please enter a number: ";
  6.     int y;
  7.     cin >> y;
  8.     cout << endl;
  9.  
  10.     int x, z;
  11.  
  12.     for (x = y; x > 0; x /= 10){
  13.         z = x % 10;
  14.         cout << z;
  15.     }
  16.     cout << endl;
  17.     return 0;
  18. }
  19.  
and produced the following result:

Expand|Select|Wrap|Line Numbers
  1. $ digit.exe
  2. Please enter a number: 1231231231
  3.  
  4. 1321321321
Anything more than 10 digits was not working - possibly because you are exceeding the maximum amount for an int?
Sep 28 '07 #5
Ganon11
3,652 Recognized Expert Specialist
EDIT: After a few more test runs, I'm sure of it. The maximum value for an int is something like 2,148,000,000, which is 10 digits. If you input anything greater than that, your program won't work (because an int isn't big enough to hold that). This is an expected error, so I wouldn't worry about it. If you need to use numbers bigger than 10 digits, try a long int.
Sep 28 '07 #6
ruskalym
65 New Member
What's revhat()? could you please elaborate more?
I am sorry but code was deleted by moderator.

I provided code implementing following recursive algorithm :

revrec() function takes an integer argument :
if it is not 0 then last digit is printed and revrec() is called again with argument divided by 10.

The revhat() function is intended to check if initial argument is 0, in which case it is printed. Else, revrec() is called with argument.
Sep 28 '07 #7

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

Similar topics

2
1558
by: - | last post by:
Hi, If I have a simple for loop like this: for a in b: print a Is there a way I can get the next element in the loop? Something like this: for a in b:
6
6322
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
0
3668
by: Vedran Vukotic | last post by:
Hello I made the following infinite loop that check's if the bot died and call it again: ------loadloop.pl----- #!/usr/bin/perl use strict; while (1) {
10
5349
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...
6
18090
by: raghuveer | last post by:
i want to print my linked list in the reverse order ie..last item first and first item last..this is what i have so far struct linked_list { int number; struct linked_list *next; }; typedef...
2
2084
by: nabman | last post by:
I am reading a txt file, I have a txt file(customers.txt) that is read, with account numbers and passwords. Once the user enters a account number and password it should read the file and if there in...
7
1427
by: Dubs | last post by:
Hi i need to find the GC content in a number of files i have a code which works but would like to shorten it, at the moment i have the same piece of script repeated 8 times but would like to add a...
15
64493
by: Alex Snast | last post by:
Hello I'm new to python and i can't figure out how to write a reverse for loop in python e.g. the python equivalent to the c++ loop for (i = 10; i >= 0; --i)
11
4182
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
7136
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
7344
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,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.