Guys, got the answer in C ; THank you for all the help .
Hey ,
I did go through the other threads on this(ADDREV Problem )(https://www.spoj.pl/problems/ADDREV/ ) but sadly, i am not able to get an answer by any of the three programs that i have written. ( in C , python and java ) . I am attaching the code snippets of all three. Thanks a lot.
Python :
def find_rev(a):
d=0
while(a>=1):
d=d*10+a%10
a=a/10
return d
n=input('enter a number')
for i in range(int(n)):
num1=input('enter the first number')
num2=input('enter the second number')
num=0
num1=find_rev(int(num1))
num2=find_rev(int(num2))
num=num1+num2
num=find_rev(num)
print num
With Python, i get runtime error.
With C, i get wrong answer.
#include<stdio.h>
long rev(long);
int main()
{
long int n;
long int n1;
long int n2;
long int i=0;
scanf("%ld",&n);
//printf("%d",n);
for(i=0;i<n;i++)
{
//printf("\n%d",i);
//printf("\nenter the two numbers");
scanf("%ld%ld",&n1,&n2);
n=rev(rev(n1)+rev(n2));
printf("%ld\n",n);
}
return 0;
}
long rev(long a)
{
long d=0;
while(a>=1)
{
d=d*10+a%10;
a=a/10;
}
return d;
}
With Java, i get a compilation error .
import java.util.*;
//import java.io.*;
public class spoj_prob {
public static void main(String args[])
{
long n=0;
System.out.println("enter a number \n");
Scanner in=new Scanner(System.in);
n=in.nextLong();
long n1=0;
long n2=0;
long sum=0;
for(int i=0;i<n;i++)
{
System.out.println("enter two numbers \n ");
n1=in.nextLong();
n2=in.nextLong();
n1=rev(n1);
n2=rev(n2);
System.out.println(n1);
System.out.println(n2);
sum=rev(n1+n2);
System.out.println(sum);
}
}
static long rev(long a)
{
long d=0;
while(a>=1)
{
d=d*10+a%10;
a=a/10;
}
return d;
}
}
Of course, those errors are reported by the SPOJ Judge. The programs work fine on my system . Test cases i use are :
2
999999999 11
999 11
Answer 101 101
Also 3
34 54
123 091
00034 00054