### Dividing a number in parts

Hello! I have a homework with switch statement that I have to make a code that user can write any number what he likes (with as many digits as he likes) and the program needs to see if the last two digits of that number can be divided with 4. The problem is that as a beginner I don't know where to look for idea or code what would help program to work only with the last two digits of a number what user writes in.

Basically it would look like this:
User writes in "513254613" and the program needs to see if the last two digits (13) can be divided with 4.

I don't need that someone codes me the program only show how to get those two digits of any number so I can work with them... if it would be said that user can write 2 or 3 digits then I would know how to but the thing is that user can write number with as many digits as he wants.

I hope that someone understood me and can hep me.
You can use the modulus operator (%) to do this. Try taking a look at what it does and see if you can figure out how to use it to accomplish what you need.
closed account (3qX21hU5)
Well there are a few options to finding the last two digits of a number so you can work with them.

The easiest would probably be converting the number into a string to work with.

For example

 ``12345678910`` ``````int main() { int number = 513254613; string numberString = std::to_string(number); // Now we can get the last to digits pretty easy. int lastDigits = std::stoi(numberString.substr(numberString.size() - 2)); cout << lastDigits; }``````

Your compiler might not yet support the std::to_string and std::stoi functions so if that is the case you might want to write your own to use for now. To do so you can look into stringstream and how they work.

Another way would be to use the mathematical version which involves the modulus operator.

EDIT: Opps Zhuge got to it first.
Last edited on
I need something similar to this

 ``12345678910111213141516171819202122`` ``````#include #include using namespace std; int main() { char x; cout << "Enter a number: "; cin >> x; switch (x) { case 'x12': case 'x16': case 'x20': case 'x24': case 'x28': case 'x32': case 'x36': case 'x40': case 'x44': case 'x48': case 'x52': case 'x56': case 'x60': case 'x64': case 'x68': case 'x72': case 'x76': case 'x80': case 'x84': case 'x88': case 'x92': case 'x96': cout << "The last two digits divides with 4!\n"; break; default: cout << "The last two digits doesn't divide with 4!\n"; } cout << "Thank You!\n"; system("pause"); return 0; }``````

Like where you see case 'x32' I need that switch only works on the last digits of any kind of number (it can be with 3 or 5 or even 9 digits), I don't need that it gives me a result of 13215612/4=something, I just need that it provides me with the text that the last two digits divides or not with 4. I just don' t know how to do that and we learned today to work with switch so I think I have to wok with it but is there a way?

Zereo way seems easy but I'm really new to coding and it confuses me a lot at the beginning that it gives me error and I can't seem to find how to work around it. A lot of things doesn't go as they go in school since at home I have Win8.

Maybe this will help. The task is to Draw a flowchart and write a program that asks the user to enter an integer and determines whether this number the last two digits form a number that is divisible by 4 without a remainder.
Last edited on
closed account (3qX21hU5)
 Zereo way seems easy but I'm really new to coding and it confuses me a lot at the beginning that it gives me error and I can't seem to find how to work around it. A lot of things doesn't go as they go in school since at home I have Win8.

I believe the error you are getting most likely has to do with you either not having C++11 enable or you are using a compiler that does not yet support to_string and stoi.

What IDE/Compiler are you using?

If you don't have a compiler that supports C++11 or don't want to deal with it you are going to have to code your own string to integer conversion and integer to string conversion functions.

To do this you will need to learn how to use the stringstream which you can read about here http://www.cplusplus.com/articles/D9j2Nwbp/ .

Also here is some code that does one of them I will leave the other to you to make.

 ``12345678910111213141516`` ``````// Converts a integer into a string. string to_string(int number) { // First we define our stringstream. stringstream converter; // Next we load out integer into that stream. converter << number; // Now we create a string variable and then convert the integer in the stream into a string. // We do this by using the handy .str() function. string ret = converter.str(); // Finally we return the string we created from the function. return ret; }``````

But to be honest this is not usually how you should handle this problem of yours. The best solution in my opinion (And easier in your circumstance) would be to use Zhuge's suggestion.

Anyways best of luck with your assignment and just a head's up you also have a few errors you will need to work out in the code that you posted.
Last edited on
first of all a character is ONE character not 3

so you can't use a switch like that.

You could use a string though

and then see if the last two digits are equal to the possible ones.
Topic archived. No new replies allowed.