Tuesday, June 25, 2013

How to add two numbers,each number has more than 1 lakh digits

                                             Today i posted about "How to add two numbers,each number has more than 1 lakhs digits?".If i ask how to add two numbers everyone said, by using '+' operators through primitive type variables.In any programming language,each primitive type has their limits.For example in java, int primitive type range is -2,147,483,648 to 2,147,483,647 and int maximum range is 10 digit number. No such type of primitives to store more than 100000 digits in any language. By using primitives we cannot solve this problem  so we use String type variables.
                                For solving this problem take two String variables, take input from user and store into this variables.After convert String variables into character arrays by using String class method toCharArray() method.
For Example:
  String str="1234567890";
 char ch[]=(int)str.toCharArray();
for(int index=0;index<ch.length;index++){
 System.out.println(ch[index]);
}

output is 1 2 3 4 5 6 7 8 9 0

After converting into character array,take first array of last index with second array of last index.

For more clarity see below program:
Click on for Addition.java



No comments:

Post a Comment