Problem 1 Solution | CodeChef

Problem

Reversed number is a number written in Arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the number is 1245, it will become 5421. 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.

CodeChef Problem Link

Input

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

For each case, print exactly one line containing only one integer – the reversed sum of two reversed numbers.

Constraints
  • 1 <= N ≤ 10000
Sample Input
1
24 1
Sample Output
34
Solution

Code Implementation

//
//  main.cpp
//  Problem 1
//
//  Created by Himanshu on 19/02/22.
//

#include <iostream>
using namespace std;

int reverseNum (int num) {
    int revnum=0, dgt;
    
    while(num>0) {
        dgt = num%10;
        num = num/10;
        revnum = revnum*10+dgt;
    }
    
    return revnum;
}

int main() {
    int T, a, b, sum;
    cin>>T;
    
    while(T--) {
        cin>>a>>b;
        a = reverseNum(a);
        b = reverseNum(b);
        sum = a+b;
        sum = reverseNum(sum);
        
        cout<<sum<<endl;
    }
    
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *