I came across this program in my book and read about it.
I don't quite understand the if(number % 2L) statement really means. A ran on google and went to a couple open chats about it and got nothing. All I can make of this is.Code:#include <iostream> using namespace std; int main() { long number=0; cout << endl << "Enter an integer number less than 2 billion: "; cin >> number; if(number % 2L) cout << endl << "your number is odd." << endl; else cout << endl << "your number is even." << endl; return 0; }
The if statment runs starts the if loop.
Number is the variable.
What does the % 2L mean?



LinkBack URL
About LinkBacks



. Like they said above, % just gets the remainder of the devision operation. Therefore 10%2=0 and 10%3=1. 10/2 devides cleanly, no remainder, whereas 10/3 is 3 with a remainer of 1(hence 10%3=1). Whenever you do x%2, you are basically checking if the number is even or odd (even if the result is 0, odd if the result is 1). Well, hope that helped!