No of Carry Operations

 No of Carry Operations


Problem Description


Given two numbers, the task is to find the number of carry operations required when two numbers are added as below..

1234

+

5678

--------

6912

--------

4+8 = 2 and carry 1

carry+3+7 = carry 1

carry+2+6 = 9, carry 0

carry+1+5 = 6

We need two carry operations in this example.

Input:

First line of input consists of an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains two unsigned integers(n1,n2) less than 10 digits.


Output:

For each test case in a new line print the number of carry operations that would be required while adding n1 and n2.

Logic Test Case 1


Input (stdin)

3


123 456


145 25


345 15


Expected Output


0


1


1

Logic Test Case 2


Input (stdin)

5


555 555


782 345


123 897


656 736


134 766


Expected Output


3


2


3


2


2





Code Area

import java.io.*;

import java.util.*;

public class TestClass {

public static void main(String[] args) { 

        Scanner sc=new Scanner(System.in);

int t=sc.nextInt();

int count=0,carry=0;

for(int i=0;i<t;i++)

{

    count=0;carry=0;

String a=sc.next();

String b=sc.next();

int len1=a.length();

int len2=b.length();

while((len1!=0) || (len2!=0))

{

    int x=0,y=0;

    if(len1>0){

     x=a.charAt(len1-1)-'0';

    len1--;}

    

    if(len2>0){

      y=b.charAt(len2-1)-'0';

    len2--;}

    

    int sum=x+y+carry;

    

    if(sum>=10)

    {

        carry=1;

        count++;

    }

    else{

        carry=0;

    }

}

System.out.println(count);

 

        }

}

}


Post a comment

0 Comments