252.Design the data structure to provide the mathematical operations .....
Microsoft Interview Questions and Answers
(Continued from previous question...)
252.Design the data structure to provide the mathematical operations .....
Question:
Design the data structure to provide the mathematical operations +, - ,/ , * etc for the very very large numbers.
also implement the + function for two such very very large numbers ...say numbers with 1 Million digits.
maybe an answer:
class operation
{
public static void main(String[] args)
{
ArrayList<Integer> no1=new ArrayList<Integer>();
ArrayList<Integer> no2=new ArrayList<Integer>();
String n1="1231231231242342342342342342";
String n2="122342345657568678678562342342";
int c=0;
while(c<n1.length())
{
no1.add(Integer.parseInt(n1.substring(c,c)));
c++;
}
c=0;
while(c<n2.length())
{
no2.add(Integer.parseInt(n2.substring(c,c)));
c++;
}
add(no1,no2);
}
public static void add(ArrayList<Integer> no1, ArrayList<Integer> no2)
{int temp=0, c;
ArrayList<Integer> no3=new ArrayList<Integer>();
while(c<no1.length() && c<no2.length())
{
no3.add((no1.get(c)+no2.get(c)+temp)%10);
temp=(no1.get(c)+no2.get(c))/10;
}
if(c<no2.length())
while(c<no2.length())
{
no3.add((no2.get(c)+temp)%10);
temp=0;
}
if(c<no1.length())
while(c<no1.length())
{
no3.add((no1.get(c)+temp)%10);
temp=0;
}
}
(Continued on next question...)
Other Interview Questions
|