Byang is learning how to add numbers. However, he gets confused whenever there is a carry. To help B…

If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

``````import java.util.*;

public class solution {

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String a=sc.nextLine();
String b=sc.nextLine();

int exc,i,n,c;

int res=0;
if(a.length()<=b.length()){
n=b.length();
exc=n-a.length();
for(i=1;i<=exc;i++){
a="0"+a;
}
}
else{
n=a.length();
exc=n-b.length();
for(i=1;i<=exc;i++){
b="0"+b;
}
}

char A[]= new char[n];
char B[]= new char[n];
int AA[]= new int[n];
int BB[]= new int[n];
i=0;
c=n-1;
while(i<n){
A[i]=a.charAt(c);
AA[i]=Character.getNumericValue(A[i]);

i++;
c--;
}
i=0;
c=n-1;
while(i<n){
B[i]=b.charAt(c);
BB[i]=Character.getNumericValue(B[i]);

i++;
c--;
}

for(int j=0; j<n;j++){
int x=AA[j]+BB[j];
if(x>9){
res++;
break;
}
else{
res=res;
}

}
if(res>=1){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}

}
``````

WHAT IS WRONG IN THIS CODE???

@ph3nixsabit

Both string are on the same line in the input.

But, you are trying to take inputs from two line which is producing Runtime Error.

``````String a = sc.nextLine();
String b = sc.nextLine(); //next line? where?
``````

Just use `sc.next()` instead and your code should be accepted.

``````String a = sc.next();
String b = sc.next();
``````

Also, you can use `split()` method like this.

``````Scanner sc= new Scanner(System.in);
String[] arr = new String;
arr = sc.nextLine().split(" ");
String a = arr;
String b = arr;
``````

or shortly,

``````String[] s = sc.nextLine().split(" ");
String a = s;
String b = s;
``````

My submission using your code: https://toph.co/s/307542

However, I think that you are making it too complicated. My solution in Java is:

``````import java.util.*;

public class solution {

public static void main(String[] args) {

//scanner
Scanner sc = new Scanner(System.in);

//inputs
String a = sc.next();
String b = sc.next();

//calculations
boolean v = false;
int x;
int la = a.length();
int lb = b.length();
int min = Math.min(la, lb);

//loop
for (int i = 1; i <= min; ++i) {
x = Character.getNumericValue( a.charAt(la-i) ) + Character.getNumericValue( b.charAt(lb-i) );
if (x > 9) {
v = true;
break;
}
}

//results
if (v) System.out.println("Yes");
else System.out.println("No");

}

}
``````
``````num1, num2=input().split()
z=list(zip(num1, num2))

t=[]
for k,v in z:
k=int(k)
v=int(v)
if k+v>=10:
t.append(True)
else:
t.append(False)

if any(t):
print("Yes")
else:
print("No")
``````

This code failed in test case 6.Help me…

@Shimanto Will your code work if there are different number of digits in the two numbers?

Yes,sir.I have tasted it on 123 321 also on 4 digits of number

But will it work on two numbers like these:

``````12345
321
``````
1 Like

I see, thank you.Sir

1 Like
``````#include <iostream>
using namespace std;

int main() {

long int m,n;
cin>>m>>n;
bool matha=true;
while(n/10!=0 || m/10!=0){
if(n%10+m%10>=10)
{
cout<<"Yes"<<endl;
matha=false;
break;
}
n=n/10;
m=m/10;
}
if(matha==true)
cout<<"No"<<endl;
return 0;
}
``````

i am getting wrong answer at 7th case.

1 Like

Does your code handle the case where the left most digits of the two numbers will result in a carry?

What’s the problem in my code?

``````#include<stdio.h>
#include<string.h>

int main(){
char a,b;
scanf("%s",a);
scanf("%s",b);
int l1=strlen(a);
l1--;
int l2=strlen(b);
l2--;
if(l1<l2){

}
else{
l1=l2;
}

for(;l1>=0;l1--){
if(((a[l1]-'0')+(b[l1]-'0'))>9){
printf("Yes\n");
return 0;
}
}
printf("No\n");
return 0;
}
``````

@Burn_Fireblaze I think the clue for what is wrong is some here in this topic already. Check out the existing discussions.                       Try some other (more precise) way of counting the number of digits.

now got ac. I just forgot to add 1 with log10()

1 Like
``````#include<bits/stdc++.h>
using namespace std;
int main(){
char j,k;
string c,d;
int a,b,i,x,y=0,count=0;
cin>>c>>d;
if(c.length()>d.length()){
x=c.length();
y=c.length()-d.length();
}
else{
x=d.length();
y=d.length()-c.length();
}
for(i=y;i<x;i++){
j=c[i];
k=d[i];
a=j-'0';
b=k-'0';
if(a+b>=10){
count++;
}

}
if(count>0){
cout<<"Yes";
}
else{
cout<<"No";
}
return 0;
}
``````

getting wrong answer in 2nd last test case

Your code produce wrong answer for input value `1238 7` is should print `YES` but it is printing `NO`
Let’s see why you’re getting wrong answer.
In your code for this input value `x = 4` and `y = 3`. Then you’re starting the loop from `y` which is equal to `3`. then you’re checking the third position of `d` which is empty because string `d = "7"` which means that `d = '7'` and `d = ' '` so when you’re adding `c+d` that can give you random value like `-60` so, that will never become bigger than `10` and `count` will not increase and you will get wrong answer.
The correct approach to this problem is first reverse the two string.
this way `reverse(c.begin(), c.end()`
and for the second string `reverse(c.begin(), c.end()`
then run the loop from `0` to `min(c.length(), d.length())`
I hope this will work. Let us know if there is any problem.

1 Like

it worked! Thanks for helping me out You’re Welcome 1 Like
`````` num1,num2=input().split()
x=len(num1)
y=len(num2)
if x<y:
m=x-1
else:
m=y-1
while m>0:
m=m-1
carry=int(num1[m])+int(num2[m])
if carry>9:
checker=1
break
else:
checker=0
if checker==1:
print("Yes")
else:
print("No")
``````

it shows wrong answer at 5th test case…but why?