Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I repeat a number cycle using mod operator?

I have to loop through 1 <= i <= 20 and every time I mod any i, I want to get a value between 1,2,3. Sorry I have not found any useful resources online. That’s why I posted it here. Thanks in advance.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In C++ the % sign acts as the modulo operator.

If you do

i % 3;

The possible values are 0, 1, and 2.

Using that as a starting point, we can shift by 1:

(i % 3) + 1;

The possible values are now 1, 2, and 3.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading