+ 3

Hera a problem. If we put more then 10 digits then it print 7 why? Otherwise it work well for print last digit. Anyone solve it.

#include <iostream> using namespace std; int main() { int a; cout << "Provide a number: \n"; cin >> a; cout << "Last digit of provided number is " << a%10; return 0; }

1st Feb 2022, 2:22 PM
Mohsin haider Sultan
Mohsin haider Sultan - avatar
10 Réponses
+ 3
use long int or string long int x = 17L; printf("%ld\n", x);
1st Feb 2022, 2:49 PM
SoloProg
SoloProg - avatar
+ 3
Thanks i can!
1st Feb 2022, 6:56 PM
Mohsin haider Sultan
Mohsin haider Sultan - avatar
+ 2
I want to print last digit from many digits which user input. But it work well for less than 10 digits. But it doesn't work More then 10 digits. Then how i can do.
1st Feb 2022, 2:37 PM
Mohsin haider Sultan
Mohsin haider Sultan - avatar
1st Feb 2022, 2:31 PM
SoloProg
SoloProg - avatar
+ 1
What was the number you used? the one with more than 10 digits? It might be an overflow issue.
1st Feb 2022, 2:54 PM
Ipang
+ 1
Apparently cin recognizes the overflow. Instead of wrapping around to negative numbers it assigns the maximum int value to a, which happens to end in 7. Here is a modification that tells the user how large the number can be: #include <iostream> #include <limits> using namespace std; int main() { int a; cout << "Provide a number up to " << INT32_MAX << ": \n"; cin >> a; cout << a << "\n" << "Last digit of provided number is " << a%10; return 0; }
2nd Feb 2022, 5:27 AM
Brian
Brian - avatar
+ 1
Thanks
3rd Feb 2022, 6:37 AM
Mohsin haider Sultan
Mohsin haider Sultan - avatar
0
Yeah i clearly understand.
3rd Feb 2022, 7:42 AM
Mohsin haider Sultan
Mohsin haider Sultan - avatar