Conditional Statement
Programming
What you need to know first 2 concepts, 1 layers
The requisite-knowledge inventory for this page, bottom-up: the primitives at the base, combined upward until you reach what this page assumes. Skim the layers you already own; start wherever the ground gets unfamiliar.
- base
- ↳you are here
This program demonstrates conditional statements using if-else to check if a number is even or odd.
Implementation
#include <iostream>
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (num % 2 == 0)
std::cout << num << " is even." << std::endl;
else
std::cout << num << " is odd." << std::endl;
return 0;
} Key Concepts
std::cin: Standard input stream for reading user input>>: Stream extraction operatorif-else: Conditional statement for decision making%: Modulo operator (returns remainder after division)==: Equality comparison operator
Example Interaction
Enter a number: 7
7 is odd.