# Add 1 to number represented as array | Recursive Approach

Given an array which represents a number, add 1 to the array. Suppose an array contain elements [1, 2, 3, 4] then the array represents decimal number 1234 and hence adding 1 to this would result 1235. So new array will be [1, 2, 3, 5].

Examples:

```Input :  [1, 2, 3, 4]
Output : [1, 2, 3, 5]

Input :  [1, 2, 9, 9]
Output : [1, 3, 0, 0]

Input:  [9, 9, 9, 9]
Output: [1, 0, 0, 0, 0]
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :
1. Add 1 to the last element of the array, if it is less than 9.
2. If it is 9, then make it 0 and recurse for the remaining element of the array.

## C++

 `// C++ Program to add 1 to an array ` `// representing a number ` `#include ` `using` `namespace` `std; ` ` `  `// function to add one and print the array ` `void` `sum(``int` `arr[], ``int` `n) ` `{ ` `     `  ` ``int` `i = n; ` ` `  ` ``// if array element is less than 9, then ` ` ``// simply add 1 to this. ` ` ``if``(arr[i] < 9) ` ` ``{ ` `     ``arr[i] = arr[i] + 1; ` `     ``return``; ` ` ``} ` ` `  ` ``// if array element is greater than 9, ` ` ``// replace it with 0 and decrement i ` `    ``arr[i] = 0; ` `    ``i--; ` ` `  `    ``// recursive function ` `    ``sum(arr, i); ` `     `  `    ``// If 1 was appended at head ` `    ``// of array then, print it ` `    ``if``(arr > 0) ` `        ``cout << arr << ``", "``; ` ` `  `    ``// print the array elements  ` `    ``// after adding one ` `    ``for``(``int` `i = 1; i <= n; i++) ` `    ``{ ` `            ``cout << arr[i]; ` `             `  `            ``if``(i < n) ` `                ``cout << ``", "``; ` `    ``} ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` `    ``// number of elements in array ` `    ``int` `n = 4; ` ` `  `    ``// array elements, index of array ` `    ``// should be 1 based, hence, 0 is ` `    ``// added here at arr ` `    ``int` `arr[] = {0, 1, 9, 3, 9}; ` ` `  `    ``// function calling ` `    ``sum(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java Program to add 1 to an array ` `// representing a number ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``// function to add one and print the array ` `    ``void` `sum(``int``[] arr, ``int` `n) ` `    ``{ ` `        ``int` `i = n; ` ` `  `        ``// if array element is less than 9, then ` `        ``// simply add 1 to this. ` `        ``if` `(arr[i] < ``9``) { ` `            ``arr[i] = arr[i] + ``1``; ` `            ``return``; ` `        ``} ` ` `  `        ``// if array element is greater than 9, ` `        ``// replace it with 0 and decrement i ` `        ``arr[i] = ``0``; ` `        ``i--; ` ` `  `        ``// recursive function ` `        ``sum(arr, i); ` ` `  `        ``// If 1 was appended at head ` `        ``// of array then, print it ` `        ``if` `(arr[``0``] > ``0``) ` `            ``System.out.print(arr[``0``] + ``", "``); ` ` `  `        ``// print the array elements ` `        ``// after adding one ` `        ``for` `(i = ``1``; i <= n; i++) { ` `            ``System.out.print(arr[i]); ` ` `  `            ``if` `(i < n) ` `                ``System.out.print(``", "``); ` `        ``} ` `    ``} ` ` `  `    ``// driver code ` `    ``static` `public` `void` `main(String[] args) ` `    ``{ ` `        ``GFG obj = ``new` `GFG(); ` `         `  `        ``// number of elements in array ` `        ``int` `n = ``4``; ` ` `  `        ``// array elements, index of array ` `        ``// should be 1 based, hence, 0 is ` `        ``// added here at arr ` `        ``int``[] arr = { ``0``, ``1``, ``9``, ``3``, ``9` `}; ` ` `  `        ``// function calling ` `        ``obj.sum(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python 3

 `# Pyhton 3 Program to add 1 to an ` `# array representing a number ` ` `  `# function to add one and print  ` `# the array ` `def` `sum``(arr, n): ` ` `  `    ``i ``=` `n ` `     `  `    ``# if array element is less than ` `    ``# 9, then simply add 1 to this. ` `    ``if``(arr[i] < ``9``): ` `     `  `        ``arr[i] ``=` `arr[i] ``+` `1` `        ``return` `     `  `    ``# if array element is greater ` `    ``# than 9, replace it with 0 ` `    ``# and decrement i ` `    ``arr[i] ``=` `0` `    ``i ``-``=` `1` `     `  `    ``# recursive function ` `    ``sum``(arr, i) ` `     `  `    ``# If 1 was appended at head ` `    ``# of array then, print it ` `    ``if``(arr[``0``] > ``0``): ` `        ``print``(arr[``0``], ``", "``, end``=``"") ` `         `  `    ``# print the array elements  ` `    ``# after adding one ` `    ``for` `i ``in` `range``(``1``, n``+``1``): ` `         `  `        ``print``(arr[i], end``=``"") ` `        ``if``(i < n): ` `            ``print``(``", "``, end``=``"") ` `     `  `# driver code ` `# number of elements in array ` `n ``=` `4` ` `  `# array elements, index of array ` `# should be 1 based, hence, 0 is ` `# added here at arr ` `arr ``=` `[``0``, ``1``, ``9``, ``3``, ``9``] ` ` `  `# function calling ` `sum``(arr, n) ` ` `  `# This code is contributed by ` `# Smitha Semwal `

## C#

 `// C# Program to add 1 to an array ` `// representing a number ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``// function to add one and print the array ` `    ``void` `sum(``int` `[]arr, ``int` `n){ ` `         `  `    ``int` `i = n; ` ` `  `    ``// if array element is less than 9, then ` `    ``// simply add 1 to this. ` `    ``if``(arr[i] < 9) ` `    ``{ ` `      ``arr[i] = arr[i] + 1; ` `      ``return``; ` `    ``} ` ` `  `    ``// if array element is greater than 9, ` `    ``// replace it with 0 and decrement i ` `    ``arr[i] = 0; ` `    ``i--; ` ` `  `    ``// recursive function ` `    ``sum(arr, i); ` `     `  `    ``// If 1 was appended at head ` `    ``// of array then, print it ` `    ``if``(arr > 0) ` `        ``Console.Write(arr + ``", "``); ` ` `  `    ``// print the array elements  ` `    ``// after adding one ` `    ``for``( i = 1; i <= n; i++) ` `    ``{ ` `            ``Console.Write(arr[i]); ` `             `  `            ``if``(i < n) ` `                ``Console.Write(``", "``); ` `    ``} ` `} ` ` `  `    ``// driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `      ``GFG obj =``new` `GFG(); ` `         `  `    ``// number of elements in array ` `    ``int` `n = 4; ` ` `  `    ``// array elements, index of array ` `    ``// should be 1 based, hence, 0 is ` `    ``// added here at arr ` `    ``int` `[]arr = {0, 1, 9, 3, 9}; ` ` `  `    ``// function calling ` `    ``obj.sum(arr, n); ` `    ``} ` `} `

Output :

```1, 9, 4, 0
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Smitha Dinesh Semwal

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7280014,"math_prob":0.94799924,"size":5816,"snap":"2019-51-2020-05","text_gpt3_token_len":1835,"char_repetition_ratio":0.1520991,"word_repetition_ratio":0.38486573,"special_character_ratio":0.3536795,"punctuation_ratio":0.15665585,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9995627,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-21T06:46:27Z\",\"WARC-Record-ID\":\"<urn:uuid:9b5c8bc3-9163-47e0-a450-8002fb93b63c>\",\"Content-Length\":\"170135\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbdae2fc-d5d2-400e-985e-b5be2d4d9641>\",\"WARC-Concurrent-To\":\"<urn:uuid:05f47da7-0409-4346-82a4-b9cbc12ef8eb>\",\"WARC-IP-Address\":\"23.199.63.170\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/add-1-number-represented-array/\",\"WARC-Payload-Digest\":\"sha1:QEGUQNBHDNOOUSDLGMMDR3BB77MJ6EJY\",\"WARC-Block-Digest\":\"sha1:4WNTCNBXBPAPWGJOGF46PQ76MOVMECPV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250601615.66_warc_CC-MAIN-20200121044233-20200121073233-00029.warc.gz\"}"}