ALO
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Explanation:
- #include <iostream>
: Includes the input/output stream library in C++ for handling input and output operations.
- using namespace std;
: Allows the usage of functions and objects in the std
namespace without explicitly specifying it.
- int main() { ... }
: The starting point of execution for the C++ program.
- int num;
: Declares an integer variable named num
to store user input.
- cout << "Enter a number: ";
: Displays the message asking the user to enter a number.
- cin >> num;
: Takes user input from the console and stores it in the num
variable.
- if (num % 2 == 0) { ... } else { ... }
: Checks if the number entered by the user is even or odd using the modulo operator %
. If the remainder after dividing num
by 2
is 0
, it's even; otherwise, it's odd.
- cout << num << " is even." << endl;
: Prints a message indicating that the entered number is even if the condition is true.
- cout << num << " is odd." << endl;
: Prints a message indicating that the entered number is odd if the condition is false.
- return 0;
: Indicates the successful termination of the main()
function.