+ 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; }

8 Answers

+ 3

use long int or string
long int x = 17L;
printf("%ld\n", x);

+ 3

Thanks i can!

+ 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.

+ 1

I hope this link helps you
https://www.sololearn.com/Discuss/1294618/remainder

+ 1

What was the number you used? the one with more than 10 digits?
It might be an overflow issue.

+ 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;
}

+ 1

Thanks

0

Yeah i clearly understand.