split from an integer in c ++

2

I made the following program that says the age of the person and the number she thought, but the final result generates a number of 4 figures according to the calculations below suppose I thought of the number 12:

Multiply by 2: 12 * 2 = 24

add 5: 24 + 5 = 29

Multiply by 50: 29 * 50 = 1450

std::cout<<"Digite o rsultado: 1450";
std::cin>>result=1450;

int var1(2017-250);
int var2(1450+1767);

std::cout<<"\n\tDigite seu ano de nascimento: ";
std::cin>>anonasc;

int var3(3217-1994);

The result contained in var3 would be 1223 where 12 would be the number I had thought of and 23 my age, but do you have any way to separate this result so that it shows separately the number and age of the person? >

I know if I do:

var3=1223/100;

It returns me

var3=12

But how about I get the number 23 like I do?

    
asked by anonymous 19.09.2017 / 07:53

2 answers

2

The most correct would be to var3 % 100 to catch 23

We can not forget that % (module) gives the rest of the division and so it picks up what could not be divided.

Example:

1223 / 100 = 12

If we do 12 * 100 we have 1200 , so what's left and not able to divide was 1223 - 1200 = 23

Let's look at the same principle of division and remainder by analyzing a simple case of 5 to divide by 2 :

Hereweseethat:

  • 5/2=2
  • 5%2=1whichistheamountleftover.

Notethattheentirepartitionwasconsidered.

Online example of getting both parts of the number

Conclusion

Whenever you want to divide a number into two parts, you have to pick up the division and the rest of the division by the multiple of 10 that matters (10, 100, 1000, etc ...)

    
19.09.2017 / 11:38
2

I do not program in C++ but for you to recover 23 do the following:

int number = 1223;
int digit = number % 50;

See working at ideone

  

I do not know if this is the best solution, maybe someone with experience can better answer the question.

    
19.09.2017 / 08:24