
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count 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