I'm writing a code to add the first and last integer of a number. But the output results in binary instead of integer. //Code
#include <iostream>
#include <string>
using namespace std;
int main() {
int t, n;
cin >> t;
for (int i = 0; i < t; i++) {
cin>> n;
string s = to_string(n);
char first = s[0];
char last = s[s.length() - 1];
int a = first;
int b = last;
cout << first + last;
}
return 0;
}
output code
/tmp/ujHkZRfwZL.o
1
1234
101
'9'has a decimal value of57, not9like you might expect.1234then your program, as shown, would do'1' + '4'which with ASCII will be49 + 51which indeed is101. Not binary, you just haven't tested with anything else. Try e.g.12345instead, and the result will be102.