By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,800 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

how to add two numbers using stack in java

100+
P: 107
hi guys, great to be back in this site after a long time. well, i am learning java and want to add two numbers using stack.
int a= 123;
int b=4567;
i want each of the values to read one by one and push to the stack. so i will have one stack to store "a" value and another to store "b" value. then i want to add the top of stack values and store it in third stack and then once the operation is over, i need to print out the result from the third stack.

please enlighten me!
Apr 13 '13 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
What have you tried?
Apr 13 '13 #2

100+
P: 107
Ok, the following code is missing only the remainder. I want to know how to add a remainder of previous operation to new operation. suppose below 7+6=13, i've pushed the 3 to stack and now i want the 1 to be added to (6+2)+1=9
Expand|Select|Wrap|Line Numbers
  1. package practical;
  2. import java.util.*;
  3.  
  4.  
  5. public class StackAddAB {
  6.     public static void main(String[] args){
  7.         int a = 1226;
  8.         int b = 4567;
  9.         int result = 0;
  10.  
  11.         a = reverseNum(a);
  12.         b = reverseNum(b);
  13.  
  14.         Stack<Integer> stacka = new Stack<Integer>();
  15.         Stack<Integer> stackb = new Stack<Integer>();
  16.         Stack<Integer> stackc = new Stack<Integer>();
  17.  
  18.         push(stacka, a);
  19.         push(stackb, b);
  20.  
  21.         while (!stacka.empty() && !stackb.empty()) {
  22.         result = addValues(stacka, stackb);
  23.  
  24.         push(stackc, result);
  25.         }
  26.  
  27.         System.out.println("Stack A: " + stacka);
  28.         System.out.println("Stack B: " + stackb);
  29.         System.out.println("Stack C: " + stackc);
  30.  
  31.     }
  32.     static void push(Stack<Integer> st, int v){
  33.  
  34.         while (v > 0){
  35.             st.push(v);            
  36.             v = v / 10;
  37.             }
  38.         }
  39.     static void pop(Stack<Integer> st) {
  40.         st.pop();
  41.         }
  42.     static Integer addValues(Stack<Integer> sta, Stack<Integer> stb){
  43.         int res=0, val = 0, rem=0, x, y;
  44.  
  45.         //while (!sta.empty() && !stb.empty()) {
  46.         x = sta.pop();
  47.         y = stb.pop();
  48.  
  49.         res = x+y+rem;
  50.  
  51.         val = res%10;
  52.         if(res>9){
  53.             rem = 1;
  54.         }
  55.         else{
  56.             rem = 0;
  57.         }
  58.  
  59.         System.out.println("Value: "+val+" Remainder: "+rem);
  60.  
  61.         return val;        
  62.     }
  63.     static Integer reverseNum(int num){
  64.         int revNum=0;
  65.         while (num != 0) {
  66.             revNum = revNum * 10 + num % 10;
  67.             num = num / 10;   
  68.         }
  69.         return revNum;
  70.     }
  71. }
  72.  
Apr 13 '13 #3

10K+
P: 13,264
Are you allowed to use java.util.Stack or are you supposed to write your own stack implementation?
Apr 13 '13 #4

100+
P: 107
its not a task assigned but i am learning it just to see how i can perform stack operations
Apr 13 '13 #5

Post your reply

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