This is the code:
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a positive integer: ";
cin >> x;
if (x <= 0) {
cout << "Illegal" << endl;
exit(1);
}
if (x <= 100) {
cout << x;
}
else {
cout << x/100 << x%10 << endl;
}
return 0;
}

**when the user inputs 456 the output is 46 or say when 4560 output is 450. Can someone please explain me how it happens?

The x/100 is an integer divide. So when 456 is divided by 100 = 4.56. Since it's an integer, the decimals (non-integer) are truncated (removed), so the first part of the cout (x/100) displays 4. Then the modulus operator divides x by ten and returns the remainder. 456 / 10 = 45 with a 6 remainder. Therefore the output is 46 =P. Please use code tags next time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.