How to cut off the main numbers? C ++

How can I turn off the leading digits of a number to display only the last two digits without using a library. For instance:

1923 - 23

2001 to 01

1234 - 34

123 to 23

only with

#include <iomanip> #include <iostream> 

Thanks!

+4
source share
2 answers

If you just work with integers, I would suggest just making mod% 100 for simplicity:

 int num =2341; cout << num%100; 

41 will appear.

And if you only need zero output:

 std::cout << std::setw(2) << std::setfill('0') << num%100 << std::endl; 
+8
source

If your numbers are in int form (and not in string form), you should consider using the modulo operator .

If the numbers are in the form of char [], there is a simple solution that includes indexing in a string, for example:

 char *myString = "ABCDE"; int lengthOfMyString = 5; cout << myString[lengthOfMyString - 3] << myString[lengthOfMyString - 5] << myString[lengthOfMyString - 4]; //outputs the word CAB 
+6
source

All Articles