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

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




reversing digits

 
Reply to this topicStart new topic

reversing digits

BLACKOUTFALLOUT
23 Sep, 2008 - 03:26 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 3

[RDG]
cpp
// reverse.cpp
// Reverse the digits of a number.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

/* Write prototype for reverseDigits */
int reverse (int number);



int main()
{
int number; // input number
int ans;

cout << "Enter a number between 1 and 9999: ";
cin >> number;

cout << "The number with its digits reversed is: ";

// find number with digits reversed
ans = reverse(number);


cout << ans << endl;

return 0; // indicate successful termination
} // end main



// reverseDigits returns number obtained by reversing digits of n
/* Write function header for reverseDigits */
// STUDENT WRITES CODE BELOW ***************
int reverse (int divisor, int multiplier)
{

{
int reverse = 0; // reversed number
int divisor = 1000; // current divisor
int multiplier = 1; // current multiplier

// loop until single-digit number
while ( n > 9 )
{
// if n >= current divisor, determine digit
if ( n >= divisor )
{
// update reversed number with current digit
reverse += n / divisor * multiplier;
n %= divisor; // update n
/* Write a line of code that reduces divisor by a factor of 10 */
// STUDENT WRITES CODE HERE ***************


/* Write a line of code that increases multiplier by a factor of 10 */
// STUDENT WRITES CODE HERE ***************


} // end if
else // else, no digit
divisor /= 10; // update divisor
} // end while

reverse += n * multiplier;
return reverse; // return reversed number
} // end function reverseDigits



I am having trouble where I am suppose to write the code. I am not to sure how to increase multiplier by factor of 10 and reduce divisor by 10. Any help would be greatly appreciated.

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Reversing Digits
23 Sep, 2008 - 10:42 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
I am too surprised to read that. You don't know how to increase and decrease any value by factor of 10 [or any other number]! do you know something called multiplication and division. crazy.gif

This post has been edited by AmitTheInfinity: 23 Sep, 2008 - 10:43 PM
User is offlineProfile CardPM
+Quote Post

tis4pgms
RE: Reversing Digits
28 Sep, 2008 - 01:59 AM
Post #3

New D.I.C Head
*

Joined: 14 Sep, 2008
Posts: 9


My Contributions
QUOTE(BLACKOUTFALLOUT @ 23 Sep, 2008 - 04:26 PM) *

[RDG]
cpp
// reverse.cpp
// Reverse the digits of a number.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

/* Write prototype for reverseDigits */
int reverse (int number);



int main()
{
int number; // input number
int ans;

cout << "Enter a number between 1 and 9999: ";
cin >> number;

cout << "The number with its digits reversed is: ";

// find number with digits reversed
ans = reverse(number);


cout << ans << endl;

return 0; // indicate successful termination
} // end main



// reverseDigits returns number obtained by reversing digits of n
/* Write function header for reverseDigits */
// STUDENT WRITES CODE BELOW ***************
int reverse (int divisor, int multiplier)
{

{
int reverse = 0; // reversed number
int divisor = 1000; // current divisor
int multiplier = 1; // current multiplier

// loop until single-digit number
while ( n > 9 )
{
// if n >= current divisor, determine digit
if ( n >= divisor )
{
// update reversed number with current digit
reverse += n / divisor * multiplier;
n %= divisor; // update n
/* Write a line of code that reduces divisor by a factor of 10 */
// STUDENT WRITES CODE HERE ***************


/* Write a line of code that increases multiplier by a factor of 10 */
// STUDENT WRITES CODE HERE ***************


} // end if
else // else, no digit
divisor /= 10; // update divisor
} // end while

reverse += n * multiplier;
return reverse; // return reversed number
} // end function reverseDigits



I am having trouble where I am suppose to write the code. I am not to sure how to increase multiplier by factor of 10 and reduce divisor by 10. Any help would be greatly appreciated.

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif


User is offlineProfile CardPM
+Quote Post

Soura
RE: Reversing Digits
28 Sep, 2008 - 05:28 AM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 27


My Contributions
QUOTE(BLACKOUTFALLOUT @ 23 Sep, 2008 - 04:26 PM) *



I am having trouble where I am suppose to write the code. I am not to sure how to increase multiplier by factor of 10 and reduce divisor by 10. Any help would be greatly appreciated.

The program is -

CODE
/*Reverse No.*/
#include<stdio.h>
# include<conio.h>
# include<iostream.h>

main()
{
    int a,n,s=0,t;
    printf("Enter the number=");
    scanf("%d", &a);
    for(n=a;n!=0;n=n/10)
    {
        t=n%10;
        s=(s*10+t);
    }
    printf("The Reverse No. is %d", s);
    getch();
}


Please try this.

This post has been edited by Soura: 28 Sep, 2008 - 05:28 AM
User is offlineProfile CardPM
+Quote Post

robhilly
RE: Reversing Digits
28 Sep, 2008 - 06:33 AM
Post #5

New D.I.C Head
*

Joined: 26 May, 2008
Posts: 10



Thanked: 1 times
My Contributions
Did you get this piece of code as a template from your instructor? You should know that when they ask to increase multiplier by a factor of 10, the formula is something like this:

inc_mult = current_mult * 10

This can be implemented in C++ using the longhand notation:

CODE
multiplier = multiplier * 10


or:

CODE
multiplier *= 10


The second method is a shorthand notation for multiplying and assigning at the same time. If you haven't realized it, the * symbol is used for multiplication operations.

I won't post how to do the division operation, but you should be able to figure it out by looking at how it is done with multiplication. (Hint: use the / operator for division.

I also would recommend looking through the early parts of your programming textbook so you understand basic syntax rules of the language.
User is offlineProfile CardPM
+Quote Post

Soura
RE: Reversing Digits
28 Sep, 2008 - 06:43 AM
Post #6

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 27


My Contributions
QUOTE(robhilly @ 28 Sep, 2008 - 07:33 AM) *

Did you get this piece of code as a template from your instructor? You should know that when they ask to increase multiplier by a factor of 10, the formula is something like this:

inc_mult = current_mult * 10

This can be implemented in C++ using the longhand notation:

CODE
multiplier = multiplier * 10


or:

CODE
multiplier *= 10


The second method is a shorthand notation for multiplying and assigning at the same time. If you haven't realized it, the * symbol is used for multiplication operations.

I won't post how to do the division operation, but you should be able to figure it out by looking at how it is done with multiplication. (Hint: use the / operator for division.

I also would recommend looking through the early parts of your programming textbook so you understand basic syntax rules of the language.

Is anything wrong with my program?
Have you told that to me?
But, my prog runs without any error and it is composed by me only.

User is offlineProfile CardPM
+Quote Post

Satan.inc
RE: Reversing Digits
4 Oct, 2008 - 03:24 AM
Post #7

New D.I.C Head
*

Joined: 26 Sep, 2008
Posts: 6

[quote name='Satan.inc' post='429262' date='4 Oct, 2008 - 04:21 AM']
CODE

#include<stdio.h>
main()
{

    int x=132,n,rev;  
    
    /*save the number*/
                n=x;

    /* reverse it*/
    for(rev=n%10;n/=10;rev=(rev*10+n%10));

    /* print it*/
    printf("%d",rev);
}

Dude Try this!
Simple 3 step ONE! biggrin.gif I love to KISS (Keep It Simple and Short) wub.gif


This post has been edited by Satan.inc: 4 Oct, 2008 - 03:26 AM
User is offlineProfile CardPM
+Quote Post

Sadaiy
RE: Reversing Digits
4 Oct, 2008 - 05:07 AM
Post #8

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 38



Thanked: 1 times
My Contributions
here is a recursive solution :

CODE

#include <iostream>

using namespace std;

void reverse(int);


int main()
{
    int num;

    cout << "Please enter a number: " << endl;
    cin >> num;

    cout << "You entered number: " << num << endl;
    
    reverse(num);

    return 0;
}

void reverse(int n)
{
    if((n/10) == 0)
        cout << n << endl;
    else
    {
        cout << n%10;
        reverse(n/10);
    }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 05:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month