Concatenation of 3 integers

My code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>

using namespace std;

int main() {
    int a, b, c, i=0, p=1;
    cin >> a >> b >> c;
    int cb = b;

     while (b != 0) {
        i++;
        b=b/10;
        p = p*10;
    }
    int concat1 = a * p + cb;
    int copyconcat1 = concat1;
   // cout << concat1;
    int cc = c;
    while (c > 0) {
        concat1 = concat1 * 10;
        c = c / 10;
    }
    int res = concat1 + cc;
   cout << res; 
    return 0;
}

Input : 1 5 6 => output 156

The problem is : input 1 0 0 => output 1 or 1 0 7 => 17
And -56 1 1 -5589 instead of -5611.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
   long long a, b, c, copyb, copyc, concat1, result;
   int p = 1;
   cout << "Enter a b c: ";   cin >> a >> b >> c;

   copyb = b;
   do {
      b /= 10;
      p *= 10;
   } while ( b );
    
   concat1 = a * p + copyb;

   copyc = c;
   do {
      concat1 *= 10;
      c /= 10;
   } while( c );
   result = concat1 + copyc;
   cout << result; 
}


I'm not sure that it makes sense to do this with possible negatives, though. What do you expect to get from
-56 -1 -1
?
If a is negative, the final number must be negative. a = -56, b=1, c=1 => new number = -5611.
In my code and in your code -56 1 1 => -5589
Yes, it can easily be fixed if a alone is negative simply by storing its sign.

The problem is ... what to do if b and/or c is negative. You haven't indicated that.

Define your problem fully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
   long long a, b, c, copyb, copyc, concat1, result;
   int p = 1, s = 1;
   cout << "Enter a b c.  a can be negative but can't be zero. b and c must not be negative.   ";
   cin >> a >> b >> c;
   if ( a < 0 ) s = -1;

   copyb = b;
   do {
      b /= 10;
      p *= 10;
   } while ( b );
    
   concat1 = a * p + s * copyb;

   copyc = c;
   do {
      concat1 *= 10;
      c /= 10;
   } while( c );
   result = concat1 + s * copyc;
   cout << result; 
}


Enter a b c.  a can be negative but can't be zero. b and c must not be negative.   -56 1 1
-5611

Last edited on
Only its sign a is important because b and c take the sign of a. EX:-56 -1 -1 => -5611
bstroe wrote:
Only its sign a is important because b and c take the sign of a. EX:-56 -1 -1 => -5611


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   long long a, b, c, copyb, copyc, concat1, result;
   int p = 1, s = 1;
   cout << "Enter a b c.  a can be negative but can't be zero. Any sign of b and c is ignored.   ";
   cin >> a >> b >> c;
   if ( a < 0 ) s = -1;

   copyb = abs( b );
   do {
      b /= 10;
      p *= 10;
   } while ( b );
    
   concat1 = a * p + s * copyb;

   copyc = abs( c );
   do {
      concat1 *= 10;
      c /= 10;
   } while( c );
   result = concat1 + s * copyc;
   cout << result; 
}

Enter a b c.  a can be negative but can't be zero. Any sign of b and c is ignored.   -56 -1 -1
-5611 
Thank you!
Topic archived. No new replies allowed.