Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,500 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,840 people online right now. Registration is fast and FREE... Join Now!




Reversing an Integer

 
Reply to this topicStart new topic

Reversing an Integer

LogicCrusher
post 3 Oct, 2006 - 06:52 AM
Post #1


New D.I.C Head

*
Joined: 3 Oct, 2006
Posts: 24


My Contributions


I am trying to write a code that will read an integer and print it out in reverse order.
So far the code prints strange characters (smiley faces…).
Why would it do that?
User is offlineProfile CardPM

Go to the top of the page


Jayman
post 3 Oct, 2006 - 06:54 AM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,819



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


To answer you question you are going to need to post the code.
User is offlineProfile CardPM

Go to the top of the page

LogicCrusher
post 3 Oct, 2006 - 07:48 AM
Post #3


New D.I.C Head

*
Joined: 3 Oct, 2006
Posts: 24


My Contributions


QUOTE(jayman9 @ 3 Oct, 2006 - 07:54 AM) *

To answer you question you are going to need to post the code.


I had to redo the code because I didn’t save it first time.
Here is the code, if you type more than nine characters it will print crazy stuff.
I am using dev c++ compiler.

CODE
#include <iostream>
#include <string>

using namespace std;

int main()
{

char ch[20],ch1[20];
int i,j;
cout<<"enter the string ";
cin>>ch;
for( i=0;ch[i]!='\0';i++)
{
}
for( j=0,i-=1;i>=0;j++,i--)
{
ch1[j]=ch[i];
}
cout<<"\n"<<"the reverse string is "<<ch1;

    system("pause");
    return 0;
}


This post has been edited by Dark_Nexus: 3 Oct, 2006 - 09:02 AM
User is offlineProfile CardPM

Go to the top of the page

frog
post 3 Oct, 2006 - 10:14 AM
Post #4


unleashed

Group Icon
Joined: 26 Mar, 2006
Posts: 682



Dream Kudos: 500
My Contributions


first of all in the second for loop you are updating the value of i in the intialization statement . that doesnt make any sense .also you should use a loop to take in the values in the character array rather than cin. next the garbage values you are getting are because there in no null character at the end of the array ch1 . just a little bit of modifications in the program and it'll run fine smile.gif
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 4 Oct, 2006 - 06:14 AM
Post #5


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Actually, it's not meant to be done in this way.
Use the % operator to get the remainder after dividing by 10 to keep extracting the rightmost digit of the number.
Print the Extracted Digit.
Then divide the number by 10 and repeat the process.

Also remember that if number%10 results in a zero, keep dividing it by 10 till it's not.

Try writing the code, and post it here.
If you can't figure it out, we'll help you.
User is offlineProfile CardPM

Go to the top of the page

frog
post 4 Oct, 2006 - 06:31 AM
Post #6


unleashed

Group Icon
Joined: 26 Mar, 2006
Posts: 682



Dream Kudos: 500
My Contributions


you were reversing an integer??? ohk i just read ur code and that asked the user to enter a string . anyways the method suggested by born2c0de is correct . work on that and i u face a problem feel free to post again
User is offlineProfile CardPM

Go to the top of the page

santoshbb
post 12 Dec, 2006 - 11:08 PM
Post #7


New D.I.C Head

*
Joined: 12 Dec, 2006
Posts: 2


My Contributions


QUOTE(frog @ 4 Oct, 2006 - 07:31 AM) *

you were reversing an integer??? ohk i just read ur code and that asked the user to enter a string . anyways the method suggested by born2c0de is correct . work on that and i u face a problem feel free to post again


The below program does the reverse int

CODE

#include<stdio.h>
int main()
{
        int num = 1234,resnum;
        resnum = reverse_num(num);
        printf("The reverse num is %d",resnum);
}

int reverse_num(int num)
{
        int tmp,tmp1,final_num=0;
        tmp = num;
        while(tmp)
        {
                tmp1 = tmp%10;
                tmp = tmp/10;
                final_num = (final_num *10)+tmp1;
        }
                return(final_num);
}
User is offlineProfile CardPM

Go to the top of the page

eXceed69
post 12 Dec, 2006 - 11:34 PM
Post #8


"Super Sentai Knight Of DawN"

Group Icon
Joined: 12 Nov, 2006
Posts: 682



Thanked 1 times

Dream Kudos: 675
My Contributions


Kindly used the [code] tag here...seems you used a function but this type program is not complex enough to had a function..and the main function had no return 0 when it is declare as int main....biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

shakti8ie
post 14 Dec, 2006 - 01:19 AM
Post #9


New D.I.C Head

*
Joined: 11 Dec, 2006
Posts: 21



Thanked 1 times
My Contributions


CODE

/*Reversing a number using string function*/

#include <iostream>
#include <string>

using namespace std;


int main()
{
    cout<<"Enter a number: ";
    int num;
    cin>>num;

    char buffer[10];//limitation
    itoa(num,buffer,10);
    strrev(buffer);
    num=atoi(buffer);
    
    cout<<endl<<"On reversing: "<<num<<endl;
}


I think this is one way to reversing a number...it has a limitation to work for upto 10 digit numbers...to accommodate bigger nums u have to change the size of array buffer[...]

This post has been edited by shakti8ie: 14 Dec, 2006 - 01:22 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 12:25AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month