Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,972 questions

51,915 answers

573 users

How to convert an array of digits to an integer add 1 and convert it back to an array of digits in C++

1 Answer

0 votes
#include <iostream>

int convert_array_of_digits_to_int_number(int arr[], int arr_size) {
    int n = 0;
       
    for (int i = 0; i < arr_size; i++) {
        n = n * 10 + arr[i];
    } 
      
    return n;
}
 
void convert_int_number_to_array_of_digits(int digits[], int n, int size) {
    int i = size - 1;
       
    while (n > 0) {
        digits[i] = n % 10;
        n = n / 10;
        i--;
    }
}
  
int main() {
    int arr[] = {9, 4, 6, 9};
    int arr_size = sizeof(arr) / sizeof(arr[0]);
      
    int n = convert_array_of_digits_to_int_number(arr, arr_size);
     
    n++;
     
    convert_int_number_to_array_of_digits(arr, n, arr_size);
      
    std::cout << "n = " << n << "\n";
     
    for (int i = 0; i < arr_size; i++) {
        std::cout << arr[i] << ", ";
    }
}
  
  
  
  
/*
run:
  
n = 9470
9, 4, 7, 0,
  
*/

 



answered May 6, 2024 by avibootz
...