How to add two numbers of any length in java?
Say for example, in java long size is 64 bit. So the maximum range is -9223372036854775808 to 9223372036854775807. Am i
Create a stack class and get numbers as string from user and convert them into string and push them into stacks. Here I've written the full code for the addition of two large numbers. stack class also included. Just type in cmd javac mystack.java then java mystack
import java.util.*;
public class mystack {
int maxsize=0;
int top=-1;
int array []=new int [0];
public mystack (int size)
{
maxsize=size;
array=new int [maxsize];
}
public void push (int x)
{
top=top+1;
array[top]=x;
}
public int pop ()
{
int elt=array[top];
top--;
return elt;
}
public boolean stackisfull()
{
return(top==maxsize-1);
}
public boolean stackisempty()
{
return(top==-1);
}
public int peak ()
{
int peak =array[top];
return peak;
}
public static void main (String args[]){
Scanner in=new Scanner (System.in);
System.out.println("Enter the 1st number");
String number1 = in.nextLine();
System.out.println();
System.out.println("Enter the 2nd number");
String number2 = in.nextLine();
System.out.println();
String temp="";
if(number1.length()>number2.length())
{
temp=number1;
number1=number2;
number2=temp;
}
int k=0;
mystack S1 = new mystack (number1.length());
for(int i=0;i