#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace std;
int main()
{
longlong 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
?
#include <iostream>
usingnamespace std;
int main()
{
longlong 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
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
longlong 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