

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Adding one to number represented as array of digits in C Program?
In this section we will see one interesting problem. Suppose one number is given. We have to increase this number by 1. This is extremely simple task. But here we will place the number as an array. each digit of that number is placed as an element of the array. If the number is 512, then it will be stored as {5, 1, 2}. And also we have to increase the number using recursive approach. Let us see the algorithm to get the clear idea.
Algorithm
increment(arr, n, index) −
Initially the default value of index is 0 begin if index < n, then if arr[index] < 9, then arr[index] := arr[index] + 1 else arr[index] := 0 increment(arr, n, index + 1) end if if index = n, then arr[n] := 1 n := n + 1 end if end
Example
#include <iostream> #include <cmath> #define MAX 20 using namespace std; void increment(int num_arr[], int &n, int index = 0){ if(index < n){ if(num_arr[index] < 9){ //if digit is less than 9, add 1 num_arr[index]++; }else{ //otherwise increase number recursively num_arr[index] = 0; increment(num_arr, n, index+1); } } if(index == n){ num_arr[n] = 1; //add extra carry n++; //increase n } } void dispNumber(int num_arr[], int n){ for(int i = n-1; i>= 0; i--){ cout << num_arr[i]; } cout << endl; } int numToArr(int num_arr[], int number){ int i = 0; int n = log10(number) + 1; for(int i = i; i< n; i++){ num_arr[i] = number % 10; number /= 10; } return n; } main() { int number = 1782698599; int num_arr[MAX]; int n = numToArr(num_arr, number); cout << "Initial Number: "; dispNumber(num_arr, n); increment(num_arr, n); cout << "Final Number: "; dispNumber(num_arr, n); }
Output
Initial Number: 1782698599 Final Number: 1782698600
- Related Questions & Answers
- Adding one to number represented as array of digits in C++?
- Sum of two numbers where one number is represented as array of digits in C++
- Divide large number represented as string in C++ Program
- 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
- Add 1 to a number represented as linked list?
- Program for adding 4 hex digits of a 16-bit number in 8085 Microprocessor
- Add 1 to a number represented as a linked list?
- Program to add two numbers represented as strings in Python
- Program to find numbers represented as linked lists in Python
- Multiply Large Numbers represented as Strings in C++
- Program to find sum of digits until it is one digit number in Python
- Adding digits of a number using more than 2 methods JavaScript
- C++ Program to Sum the digits of a given number
Advertisements