AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-basics: Program to find the reversed Sum

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

Your task is to add two reversed numbers and output their reversed sum.



Input Explanation:

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.



Output Explanation:

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.



Sample Input:

3
24 1
4358 754
305 794



Sample Output:

34
1998
1



Explanation: For the first sample input, 3 refers to the number of tests. In the first test 24 and 1 are the two numbers ,the reverse of 24 is 42 and for 1 it is 1. Hence the sum of 42 and 1 is 43. The reverse of 43 is 34.

programming x 150
basics x 169
Posted On : 2017-11-27 16:57:21.0
profile Divesh - anyforum.in Divesh
8800
up-rate
5
down-rate

Answers


import java.util.Scanner;

public class ReversedSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
input.nextLine();
String[] inputs = null;
for(int i=0;i<N;i++){
inputs = input.nextLine().split(" ");
System.out.println(getReversedSum(Integer.valueOf(inputs[0]),Integer.valueOf(inputs[1])));
}
}

public static int getReversedSum(Integer number1, Integer number2){
int reversedSum=0;
int reverseNum1=getReverseNumber(number1);
int reverseNum2=getReverseNumber(number2);
reversedSum=getReverseNumber(reverseNum1+reverseNum2);
return reversedSum;
}

public static int getReverseNumber(Integer number){
int reverseNumber=0;
char[] digits=number.toString().toCharArray();
int length = digits.length;
char[] reverseDigits = new char[length];
for(int i=length;i>=1;i--){
reverseDigits[length-i]=digits[i-1];
}
reverseNumber=Integer.valueOf(String.valueOf(reverseDigits));
return reverseNumber;
}

}


Note: If you are just going to paste all the inputs on console at a time, Please put one blank line at the end of inputs then paste it or if you are going to enter inputs one by one then it´s not a problem.

Posted On : 2017-11-28 23:34:28
Satisfied : 4 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596128122205
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in