
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count the number of carry operations required to add two numbers in C++
We are given two numbers num_1 and num_2. The goal is to count the number of carry operations required if the numbers are added. If numbers are 123 and 157 then carry operations will be 1. (7+3=10, 1+2+5=8, 1+1=2 ).
Let us understand with examples
Input − num_1=432 num_2=638
Output − Count of number of carry operations required to add two numbers are − 2
Explanation − From right to left adding digits and counting carry −
(2+9=10, carry 1 ) count=1, (1+3+3=7, carry 0 ) count=1, (4+6=10, carry 1 ) count=2
Input − num_1=9999 num_2=111
Output − Count of number of carry operations required to add two numbers are − 4
Explanation − From right to left adding digits and counting carry −
(9+1=10, carry 1 ) count=1, (1+9+1=11, carry 1 ) count=2, (1+9+1=11, carry 1 ) count=3, (1+9=10, carry 1) count=4
The approach used in the below program is as follows
We will convert both the numbers into strings. Start traversing strings from the end, convert character to an integer, add both and also previous carry ( 0 for 1st iteration ), if value>10 set carry as 1. If the carry is 1 increment count of carry.
Take two numbers as num_1 and num_2.
Function carry_add_two_numbers(num_1, num_2) takes both number and returns count of carry required when both are added.
Convert both numbers to string using to_string(x) and store in str_1 and str_2.
Take lengths of both strings using length() as lenght_str_1 and length_str_2.
Take initial count as 0 and initial carry also like 0.
While both lengths are non-zero.
Keep converting from the last character to integer and store integers in variables i and j.
Reduce lengths of both strings.
Take variable to add as i+j+carry.
If add>10 then increment count (as it is carried). And set cary=1.Otherwise set carry=0 for next iteration.
After the end of all iterations, the count will have a total number of carries.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int carry_add_two_numbers(int num_1, int num_2){ string str_1 = to_string(num_1); int length_str_1 = str_1.length(); string str_2 = to_string(num_2); int length_str_2 = str_2.length(); int count = 0, carr = 0; while(length_str_1 != 0 || length_str_2 != 0){ int i = 0, j = 0; if (length_str_1 > 0){ i = str_1[length_str_1 - 1] - '0'; length_str_1--; } if (length_str_2 > 0){ j = str_2[length_str_2 - 1] - '0'; length_str_2--; } int add = i + j + carr; if (add >= 10){ carr = 1; count++; } else{ carr = 0; } } return count; } int main(){ int num_1 = 234578; int num_2 = 1234; int count = carry_add_two_numbers(num_1, num_2); cout<<"Count of number of carry operations required to add two numbers are: "<<count; return 0; }
Output
If we run the above code it will generate the following output −
Count of number of carry operations required to add two numbers are: 2
- Related Articles
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Count the number of operations required to reduce the given number in C++
- Program to count number of operations required to all cells into same color in Python
- Program to count number of operations required to convert all values into same in Python?
- 8086 program to add two 16 bit BCD numbers with carry
- 8086 program to add two 16-bit numbers with or without carry
- Minimum number of given operations required to make two strings equal using C++.
- Count operations of the given type required to reduce N to 0 in C++
- C++ code to count number of operations to make two arrays same
- Number of carries required while adding two numbers in JavaScript
- Addition of two numbers without propagating Carry?
- Program to count number of common divisors of two numbers in Python
- Number of operations required to make all array elements Equal in Python
- Program to find number of given operations required to reach Target in Python
- Program to find number of operations required to remove palindromic sublists in C++
