How do I reverse the numbers in my program to produce the orginal numbers and the numbers in reverse?

int magic_box (int N)
{
int digit1, digit2, digit3;
digit1 = N / 100;
digit2 = N % 100 /10;
digit3 = N % 10;
return digit1 + digit2 * 10 + digit3 * 100;
}

Example

Enter a 3-digit integer and my magic box
will reverse the digits in your number for you: 739
My magic box has change your number 739 into 937

Recommended Answers

All 25 Replies

Standart solution is stack. Continue?

Standart solution is stack. Continue?

ummm yes?:| lol please explain this to me in lamest terms im a newb at this stuff brah

ummm yes?:| lol please explain this to me in lamest terms im a newb at this stuff brah

// ------------------------------------------------------------
// File       : stack.h
// Created    : December 11, 2005
// Author     : Eugene Lykhovyd
// Description: stack class
// ------------------------------------------------------------
#ifndef __STACK_H
#define __STACK_H

#if !defined __cplusplus
#error This stack works only with C++(put .CPP extention to source file)
#endif

#if !defined __IOSTREAM_H
#include <iostream.h>
#endif

//class stack
template <class TYPE>
class stack {
private:
	enum { EMPTY = -1};
	enum boolean { false, true };
	TYPE* s;
	int max_len;
	int top;
public:
	stack() :max_len(1000)
		{ s = new TYPE[1000]; top = EMPTY; }
	stack(int size) :max_len(size)
		{ s = new TYPE[size]; top = EMPTY; }
	~stack() { delete [] s; }
	void reset() { top = EMPTY; }
	void push(TYPE c) { s[++top] = c; }
	TYPE pop() { return (s[top--]); }
	TYPE top_of() { return (s[top]); }
	boolean empty() { return boolean(top == EMPTY); }
	boolean full() { return boolean(top == max_len - 1); }
	void print();
};

template <class TYPE>
void stack<TYPE>::print() {
	for (int i = 0; i < max_len; ++i)
		cout << s[i];
};

#endif

Then you just do smth like this:

stack<int> s = new stack(4);

s.reset();

s.push(number_1);
s.push(number_2);
s.push(number_3);

number1 = s.pop();
number2 = s.pop();
number3 = s.pop();

delete [] s;

Hope this is helpful information.

wow.....is this C++ tho? all of that looks to complicated for me to understand

This is really OOP with C++. You can make this trick with numbers, strings, doubles, another classes and what you want. Really it is a standart problem. Is it works properly?

Na i cant get it to work :(

Na i cant get it to work :(

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
const int max_len = 1000;

enum boolean {false, true};
enum {EMPTY = -1, FULL = max_len - 1};

struct stack {
	int s[max_len];
	int   top;
};

void reset(stack* stk)
{
	stk -> top = EMPTY;
}

void push(int c, stack* stk)
{
	stk -> s[++stk -> top] = c;
}

int pop(stack* stk)
{
	return (stk -> s[stk -> top--]);
}

int top(stack* stk)
{
	return (stk -> s[stk -> top]);
}

boolean empty(const stack* stk)
{
	return (boolean) (stk -> top == EMPTY);
}

boolean full(const stack* stk)
{
	return (boolean) (stk -> top == FULL);
}

main() {
stack s;
reset(&s);
push(number_1, &s);
push(number_2, &s);
push(number_3, &s);

number_1 = pop(&s);
number_2 = pop(&s);
number_3 = pop(&s);
return 0 ;
}

Try this one.
It is easier.:icon_idea:

Is there any stacks in your code you have sent me? )

na no stacks , are needed

help

Think it out a bit more, you almost got it:

Let say 123 is the number you enter
after (123%10)*100+(123%100)/10+(123)/100
you'll get 321

this is what i got so far

using namespace std;

      //Named constants

      const int THOUSAND = 1000;
      const int HUNDRED = 100;
      const int TEN = 10;
      const int ONE = 1;


int main()
{
    //Declare vaiables 

    int threedigitnum;
    int digit1,digit2,digit3;
    int rnum1, rnum2, rnum3;

    // Statements Steps
    cout << "Enter a postive Four-digit number please: ";                      
    cin >> threedigitnum;                                        


    digit1 = threedigitnum / HUNDRED;                          
    threedigitnum = threedigitnum % HUNDRED;


    digit2 = threedigitnum / TEN ;                            
    threedigitnum = threedigitnum % TEN;



    digit3 = threedigitnum; 





    rnum1 = threedigitnum % TEN;
    threedigitnum = threedigitnum % TEN;

    rnum2 = threedigitnum % HUNDRED;
    threedigitnum = threedigitnum / TEN;


    rnum3 = threedigitnum / HUNDRED;

but its not working HELP!!!!!!

anyone home????

bump

Jboy05, it's rude to post continuously just to bump your thread. The people here have lives outside the forum and 9 of the 14 posts here are yours. Patience!!!

Next, ignore everything zhelih said. Stacks are not needed at all.

Do you know how to use a while loop? Do you know the % operator? I assume you know division (/). These are all you need.

If you get the last digit of the original number, that is the first digit of the reversed number (use %). Store it in the reverse integer.
Remove that digit from the original number.
Then get the 'new' last digit. Multiply the reverse value by 10 and add the digit. From there, think through the rest of the instructions and see what you come up with. Your while loop should be less than 6 lines.

I got the first reversed integer but the second and third don't work and i need help

const int THOUSAND = 1000;
const int HUNDRED = 100;
const int TEN = 10;
const int ONE = 1;


int main()
{
//Declare vaiables

int threedigitnum;
int digit1,digit2,digit3;
int rnum1, rnum2, rnum3;

// Statements Steps
cout << "Enter a postive Four-digit number please: ";
cin >> threedigitnum;


digit1 = threedigitnum / HUNDRED;
threedigitnum = threedigitnum % HUNDRED;


digit2 = threedigitnum / TEN ;
threedigitnum = threedigitnum % TEN;

digit3 = threedigitnum;

rnum1 = threedigitnum % TEN;
threedigitnum = threedigitnum % TEN;

rnum2 = threedigitnum % HUNDRED;
threedigitnum = threedigitnum / TEN;


rnum3 = threedigitnum / HUNDRED;

cout << "My magic box has change your number " << digit1 << digit2 << digit3 << " into " << rnum1 << rnum2 << rnum3 << endl;

is there something im missing because i did the math on the reverse numbers and it works on paper

nvm i got it myself you ppl move slow

commented: Slow indeed. -1
commented: 'sorry' we couldn't help you cheat +3

now how do i delete this thread?

>now how do i delete this thread?
We don't delete threads just because the original poster's question is answered. However, if you want, a moderator can mark this thread as "solved".

>now how do i delete this thread?
We don't delete threads just because the original poster's question is answered. However, if you want, a moderator can mark this thread as "solved".

You don't need a moderator to mark a thread solved. If you are the original poster and got the answer you need, you mark the thread solved.

You don't need a moderator to mark a thread solved. If you are the original poster and got the answer you need, you mark the thread solved.

Oh yeah, I forgot that thread starters could do that. :icon_cheesygrin: My bad.

/*easiest program in c++ to reverse a number . courtesy:- dinesh.mudgil*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
/*program to reverse the digits of a number as well
as to calculate the number of digits present in a number*/
void main()
{double innum;
clrscr();
cout<<"enter a number"<<endl;
cin>>innum;
for(int i=1;i<100;i++)
{double rem=fmod(innum,pow(10,i));
if(rem==innum)
break;
}
cout<<"the number is "<<i<<" digit number"<<endl;
int m=1;
for(m=1;m<=i;m++)
{
double rem1;
double num=innum;
for(int j=i-m;j>0;j--)
{ rem1=fmod(num,pow(10,j));
}
if(m!=i)
{
cout<<rem1;
innum=(innum-rem1)/10;
}
if(m==i)
cout<<innum;
}

getch();
}
/* sample output:-
enter a number
123456789
the number is a 9 digit number
987654321
*/

ummm did you see the date? and it was already solved...

have u seen the complexity of the previously qouted CODE.....?????

have u seen the complexity of the previously qouted CODE.....?????

Yea! Usually one needs a couple of lines, for examples:

long int reversi(long int z)
{ long int r = 0;
  while (z != 0) { r = r * 10 + z % 10; z /= 10; }
  return r;
}

...
cout  << reversi (12345);  // result: 54321

krs,
tesu

oops sorry, already solved. does it make no odds?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.