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 217 views
217 views asked Oct 19, 2023 by avibootz
2 answers 240 views
2 answers 257 views
2 answers 282 views
282 views asked Oct 19, 2023 by avibootz
2 answers 265 views
265 views asked Oct 19, 2023 by avibootz
1 answer 195 views
195 views asked Oct 19, 2023 by avibootz
2 answers 256 views
256 views asked Oct 19, 2023 by avibootz
...