How to write an algorithm that adds the odd digits of one number to the end of a second number in C++

1 Answer

0 votes
#include <iostream>

int add_odd_digits(int n, int second_n);
   
int main(void)
{
    int n = 12734, second_n = 100;
  
    std::cout << add_odd_digits(n, second_n);
}
   
int add_odd_digits(int n, int second_n) {
    int multiply = 1, odd = 0;
      
    while (n != 0) {
        if (n % 2 != 0) {
            odd += (n % 10) * multiply;
            multiply *= 10;
        }
        
        n = n / 10;
    }
  
    return second_n * multiply + odd;
}
   
   
   
/*
run:
   
100173
 
*/

 



answered Nov 5, 2024 by avibootz
...