top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a function in JAVA that takes 2 numbers in string and forms a string containing the addition of these 2 numbers?

+2 votes
1,187 views

Assumption:

  • The input strings will contain only numeric digits
  • The input strings can be of any large lengths
  • The lengths of the two input string need not be the same
  • The input strings will represent only positive numbers

Example

If input strings are “1234” and “56”, the output string should be “1290”
If input strings are “56” and “1234”, the output string should be “1290”
If input strings are “123456732128989543219” and “987612673489652”, the output string should be “123457719741663032871”

posted May 9, 2017 by Azhar Keer

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer
 public addition(){
  String max=input1.length()>input2.length()?input1:input2;
  String min=input1.length()
  int a,b,add;
  int carry=0;
  String output="";
  int minLen=min.length()-1;
  int maxLen=max.length()-1;
  while(minLen>-1){
  a=Integer.parseInt(max.charAt(maxLen--)+"");
  b=Integer.parseInt(min.charAt(minLen--)+"");
  add=a+b+carry;
  carry=0;
  if(add>9){
  carry=1;
  add=add%10;
  }
  output=add+output;
  System.out.println(output);
  }
  max=input1.substring(0,input1.length()-input2.length());
  add=Integer.parseInt(max);
  add+=carry;
  return add+""+output;
}
answer Aug 27, 2017 by Arindam Nath
can you pls give in c language???
That is the shortest code, thanks for the help
Similar Questions
+2 votes

Assumption:
The input strings will contain only numeric digits
The input strings can be of any large lengths
The lengths of the two input string need not be the same
The input strings will represent only positive numbers
Example

If input strings are “1234” and “56”, the output string should be “1290”
If input strings are “56” and “1234”, the output string should be “1290”
If input strings are “123456732128989543219” and “987612673489652”, the output string should be “123457719741663032871”

+2 votes

Weight of vowels that appear in the string should be ignored and All non-alphabetic characters in the string should be ignored.

Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:
Let us assume the word is “Hello World!!”
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

+1 vote

Given a singly linked list of integers, write a function in java that returns true if the given list is palindrome, else returns false

...