Count the number of carry operations required to add two numbers
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: 6912 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.
- Initialise 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++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to count the number of // carry operations int count_carry(string a, string b) { // Initialise the value of carry to 0 int carry = 0; // Counts the number of carry operations int count = 0; // Initialise len_a and len_b with the sizes of strings int len_a = a.length(), len_b = b.length(); while (len_a != 0 || len_b != 0) { // Assigning the ascii value of the character 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--; } // Add both numbers/digits int sum = x + y + carry; // If sum > 0, icrement count // and set carry to 1 if (sum >= 10) { carry = 1; count++; } // Else, set carry to 0 else carry = 0; } return count; } // Driver code 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
// Java implementation of // above approach import java.io.*; class GFG { // Function to count the number // of carry operations static int count_carry(String a, String b) { // Initialise the value // of carry to 0 int carry = 0; // Counts the number of // carry operations int count = 0; // Initialise len_a and len_b // with the sizes of strings int len_a = a.length(), len_b = b.length(); while (len_a != 0 || len_b != 0) { // Assigning the ascii value // of the character 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--; } // Add both numbers/digits int sum = x + y + carry; // If sum > 0, icrement count // and set carry to 1 if (sum >= 10) { carry = 1; count++; } // Else, set carry to 0 else carry = 0; } return count; } // Driver code 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); } } // This code is contributed by Shashank |
Python3
# Python3 implementation of
# above approach
# Function to count the number
# of carry operations
def count_carry(a, b):
# Initialise the value of
# carry to 0
carry = 0;
# Counts the number of carry
# operations
count = 0;
# Initialise len_a and len_b
# with the sizes of strings
len_a = len(a);
len_b = len(b);
while (len_a != 0 or len_b != 0):
# Assigning the ascii value
# of the character
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;
# Add both numbers/digits
sum = x + y + carry;
# If sum > 0, icrement count
# and set carry to 1
if (sum >= 10):
carry = 1;
count += 1;
# Else, set carry to 0
else:
carry = 0;
return count;
# Driver code
a = “9555”;
b = “555”;
count = count_carry(a, b);
if (count == 0):
print(“0”);
elif (count == 1):
print(“1”);
else:
print(count);
# This code is contributed by mits
C#
// C# implementation of // above approach using System; class GFG { // Function to count the number // of carry operations static int count_carry(string a, string b) { // Initialise the value // of carry to 0 int carry = 0; // Counts the number of // carry operations int count = 0; // Initialise len_a and len_b // with the sizes of strings int len_a = a.Length, len_b = b.Length; while (len_a != 0 || len_b != 0) { // Assigning the ascii value // of the character 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--; } // Add both numbers/digits int sum = x + y + carry; // If sum > 0, icrement count // and set carry to 1 if (sum >= 10) { carry = 1; count++; } // Else, set carry to 0 else carry = 0; } return count; } // Driver code 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); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP implementation of above approach // Function to count the number // of carry operations function count_carry($a, $b) { // Initialise the value of // carry to 0 $carry = 0; // Counts the number of carry // operations $count = 0; // Initialise len_a and len_b // with the sizes of strings $len_a = strlen($a); $len_b = strlen($b); while ($len_a != 0 || $len_b != 0) { // Assigning the ascii value // of the character $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--; } // Add both numbers/digits $sum = $x + $y + $carry; // If sum > 0, icrement count // and set carry to 1 if ($sum >= 10) { $carry = 1; $count++; } // Else, set carry to 0 else $carry = 0; } return $count; } // Driver code $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"; // This code is contributed by jit_t ?> |
4
Recommended Posts:
- Count the number of operations required to reduce the given number
- Count operations of the given type required to reduce N to 0
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum number of operations required to sum to binary string S
- Minimum number of given operations required to make two strings equal
- Minimum number of given operations required to reduce the array to 0 element
- Find the number of operations required to make all array elements Equal
- Find the minimum number of operations required to make all array elements equal
- Maximum count of equal numbers in an array after performing given operations
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of given operations required to convert a string to another string
- Addition of two numbers without propagating Carry
- Minimum operations required to change the array such that |arr[i] - M| <= 1
- Operations required to make the string empty
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



