How to append int to int in C++

2 Answers

0 votes
#include <iostream>

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    
    while(y >= pow)
        pow *= 10;
        
    return x * pow + y;        
}
 
int main(void) {
    int a = 12, b = 938; 
      
    a = concatenate(a, b);
     
    std::cout << a;
}
 
 
 
 
 
/*
run:
 
12938
 
*/

 



answered Oct 19, 2023 by avibootz
0 votes
#include <iostream>

int main(void) {
    int a = 12, b = 938; 
    
    std::string str1 = std::to_string(a);
    std::string str2 = std::to_string(b);
    
    str1 += str2;
      
    a = std::stoi(str1);
     
    std::cout << str1;
}
 
 
 
 
 
/*
run:
 
12938
 
*/

 



answered Oct 19, 2023 by avibootz

Related questions

2 answers 216 views
216 views asked Oct 19, 2023 by avibootz
2 answers 239 views
2 answers 254 views
2 answers 279 views
279 views asked Oct 19, 2023 by avibootz
2 answers 264 views
264 views asked Oct 19, 2023 by avibootz
1 answer 195 views
195 views asked Oct 19, 2023 by avibootz
2 answers 252 views
252 views asked Oct 19, 2023 by avibootz
...