1.11M Members

How do I reverse numbers in my program

 
0
 

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

 
0
 

Standart solution is stack. Continue?

 
0
 

Standart solution is stack. Continue?

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

 
0
 

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.

 
0
 

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

 
0
 

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?

 
0
 

Na i cant get it to work :(

 
0
 

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? )

 
0
 

na no stacks , are needed

 
0
 

help

 
0
 

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
 
0
 

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!!!!!!

 
0
 

anyone home????

 
0
 

bump

 
0
 

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.

 
0
 

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;

 
0
 

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

 
0
 

nvm i got it myself you ppl move slow

 
0
 

now how do i delete this thread?

 
0
 

>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
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article