0

Output integers 0 and 1

Hi! This is probably something super simple, but i would like to write a simple arithmetic code that only outputs integer values 0 and 1 (not boolean).

28th Feb 2019, 3:47 AM
jigoku_tenshi
4 Réponses
+ 2
Ohh, OK. The idea is simple, modulus (%) should do the job fine. for (int i = 0; ; i++) std::cout << i % 2; E.g. 0%2 = 0 1%2 = 1 2%2 = 0 3%2 = 1 ...
28th Feb 2019, 11:15 AM
Fermi
Fermi - avatar
+ 4
Boolean values in C++ can actually be interpreted as 0 and 1, and will interact with other datatypes as such. They can be stored in integer variables and boolean variables. Putting that aside, are you looking to generate 1 or 0 randomly, or... ?
28th Feb 2019, 5:05 AM
Fermi
Fermi - avatar
+ 2
No, I’m trying to make a for loop that uses an arithmetic that outputs 0,1,0,1,0,1,..... infinitely
28th Feb 2019, 10:07 AM
jigoku_tenshi
+ 1
Thank you very much!!
1st Mar 2019, 12:45 AM
jigoku_tenshi