Count the number of carry operations required to add two numbers
Last Updated :
20 Aug, 2022
Given two numbers, the task is to find the number of carry operations required when two numbers are added as below.
1234
+
5678
——–
6912
——–
Examples:
Input: n = 1234, k = 5678
Output: 2
4+8 = 2 and carry 1
carry+3+7 = carry 1
carry+2+6 = 9, carry 0
carry+1+5 = 6
Input: n = 555, k = 555
Output: 3
Approach: Store the values of n and k in strings.
- Initialize the carry variable and count variable to 0.
- Now, check from the last index of the strings till both the strings come to an end(one string may be smaller than the other).
- Add both the values(take care of ascii value) with carry in every iteration and check if that sum is greater than 10 or not.
- If it is greater than 10 then simply increment the value of count by 1 and make carry equal to 1, else make carry equal to 0.
- At last, print your answer which is count.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_carry(string a, string b)
{
int carry = 0;
int count = 0;
int len_a = a.length(), len_b = b.length();
while (len_a != 0 || len_b != 0) {
int x = 0, y = 0;
if (len_a > 0) {
x = a[len_a - 1] - '0' ;
len_a--;
}
if (len_b > 0) {
y = b[len_b - 1] - '0' ;
len_b--;
}
int sum = x + y + carry;
if (sum >= 10) {
carry = 1;
count++;
}
else
carry = 0;
}
return count;
}
int main()
{
string a = "9555" , b = "555" ;
int count = count_carry(a, b);
if (count == 0)
cout << "0\n" ;
else if (count == 1)
cout << "1\n" ;
else
cout << count << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int count_carry(String a, String b)
{
int carry = 0 ;
int count = 0 ;
int len_a = a.length(),
len_b = b.length();
while (len_a != 0 || len_b != 0 )
{
int x = 0 , y = 0 ;
if (len_a > 0 )
{
x = a.charAt(len_a - 1 ) - '0' ;
len_a--;
}
if (len_b > 0 )
{
y = b.charAt(len_b - 1 ) - '0' ;
len_b--;
}
int sum = x + y + carry;
if (sum >= 10 )
{
carry = 1 ;
count++;
}
else
carry = 0 ;
}
return count;
}
public static void main (String[] args)
{
String a = "9555" , b = "555" ;
int count = count_carry(a, b);
if (count == 0 )
System.out.println( "0\n" );
else if (count == 1 )
System.out.println( "1\n" );
else
System.out.println(count);
}
}
|
Python3
def count_carry(a, b):
carry = 0 ;
count = 0 ;
len_a = len (a);
len_b = len (b);
while (len_a ! = 0 or len_b ! = 0 ):
x = 0 ;
y = 0 ;
if (len_a > 0 ):
x = int (a[len_a - 1 ]) + int ( '0' );
len_a - = 1 ;
if (len_b > 0 ):
y = int (b[len_b - 1 ]) + int ( '0' );
len_b - = 1 ;
sum = x + y + carry;
if ( sum > = 10 ):
carry = 1 ;
count + = 1 ;
else :
carry = 0 ;
return count;
a = "9555" ;
b = "555" ;
count = count_carry(a, b);
if (count = = 0 ):
print ( "0" );
elif (count = = 1 ):
print ( "1" );
else :
print (count);
|
C#
using System;
class GFG
{
static int count_carry( string a, string b)
{
int carry = 0;
int count = 0;
int len_a = a.Length,
len_b = b.Length;
while (len_a != 0 || len_b != 0)
{
int x = 0, y = 0;
if (len_a > 0)
{
x = a[len_a - 1] - '0' ;
len_a--;
}
if (len_b > 0)
{
y = b[len_b - 1] - '0' ;
len_b--;
}
int sum = x + y + carry;
if (sum >= 10)
{
carry = 1;
count++;
}
else
carry = 0;
}
return count;
}
public static void Main ()
{
string a = "9555" , b = "555" ;
int count = count_carry(a, b);
if (count == 0)
Console.Write( "0\n" );
else if (count == 1)
Console.Write( "1\n" );
else
Console.Write(count);
}
}
|
PHP
<?php
function count_carry( $a , $b )
{
$carry = 0;
$count = 0;
$len_a = strlen ( $a );
$len_b = strlen ( $b );
while ( $len_a != 0 || $len_b != 0)
{
$x = 0;
$y = 0;
if ( $len_a > 0)
{
$x = $a [ $len_a - 1] - '0' ;
$len_a --;
}
if ( $len_b > 0)
{
$y = $b [ $len_b - 1] - '0' ;
$len_b --;
}
$sum = $x + $y + $carry ;
if ( $sum >= 10)
{
$carry = 1;
$count ++;
}
else
$carry = 0;
}
return $count ;
}
$a = "9555" ;
$b = "555" ;
$count = count_carry( $a , $b );
if ( $count == 0)
echo "0\n" ;
else if ( $count == 1)
echo "1\n" ;
else
echo $count , "\n" ;
?>
|
Javascript
<script>
function count_carry(a, b)
{
let carry = 0;
let count = 0;
let len_a = a.length, len_b = b.length;
while (len_a != 0 || len_b != 0)
{
let x = 0, y = 0;
if (len_a > 0)
{
x = a[len_a - 1] - '0' ;
len_a--;
}
if (len_b > 0)
{
y = b[len_b - 1] - '0' ;
len_b--;
}
let sum = x + y + carry;
if (sum >= 10)
{
carry = 1;
count++;
}
else
carry = 0;
}
return count;
}
let a = "9555" , b = "555" ;
let count = count_carry(a, b);
if (count == 0)
document.write( "0" + "</br>" );
else if (count == 1)
document.write( "1" + "</br>" );
else
document.write(count);
</script>
|
Time Complexity: O(|a| + |b|)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...