
- 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
Adding one to number represented as array of digits in C++?
A number represented as array stored each digit of the number in a single element of the array. The length of the array is equal to the number of digits in the array i.e. length = 3 for four digit number. Each element of the array is a single digit number. The number is stored in such a way that the last element store the least significant digit of the number. And the first element stores the most significant digit of the number. For example,
Number − 351932 is stored as {3,5,1,9,3,2}
To add one to this number need you to add one to the last element of the array and them check if any carry needs to be propagated or not. If the number at the last bit is 9 then a carry is propagated and last element’s value becomes 0.
If bit is propagated then, the element at (n-1) position is incremented by one and carry propagation is checked. For example
Adding one t0 {3,5,7,9} gives {3,5,8,0}. Here, a carry is propagated and value of seven is increased to 8.
Example
#include <bits/stdc++.h> using namespace std; void addone(vector<int> &a) { int n = a.size(); a[n-1] += 1; int carry = a[n-1]/10; a[n-1] = a[n-1] % 10; for (int i = n-2; i >= 0; i--) { if (carry == 1) { a[i] += 1; carry = a[i]/10; a[i] = a[i] % 10; } } if (carry == 1) a.insert(a.begin(), 1); } int main() { vector<int> num{2, 3, 9, 9}; cout<<"The original number is : "; for (int i = 0; i < num.size(); i++) cout << num[i]; cout<<endl; addone(num); cout<<"The incremented value is : "; for (int i = 0; i < num.size(); i++) cout << num[i]; return 0; }
Output
The original number is 2399 The incremented value is 2400
- Related Articles
- Adding one to number represented as array of digits in C Program?
- Sum of two numbers where one number is represented as array of digits in C++
- Add 1 to number represented as array (Recursive Approach)?
- Add 1 to the number represented as array (Recursive Approach)?
- Recursively adding digits of a number in JavaScript
- Divide large number represented as string in C++ Program
- Adding digits of a number using more than 2 methods JavaScript
- Convert number to reversed array of digits JavaScript
- Check if a given number can be represented in given a no. of digits in any base in C++
- Add 1 to a number represented as linked list?
- Convert to number with digits as 3 and 8 only in C++
- Convert number to a reversed array of digits in JavaScript
- Add 1 to a number represented as a linked list?
- Program for adding 4 hex digits of a 16-bit number in 8085 Microprocessor
- Number of digits in the nth number made of given four digits in C++
