AVAILABLE: 5
This discussion is not much different from the previous one, as it still uses "if," "else if," and "else." In the previous topic, the modulus operator was used to determine whether a number was odd or even. In this session, we will expand the concept by analyzing whether a number is positive, negative, or zero. Alongside the modulus operator "%", we will also use the logical operator "and," which in C++ is expressed as "&&". For more details, let's dive into the explanation below:
Source Code:
#include<iostream.h>
#include<conio.h>
/**
*gaexe.com
*Determine Positive, Negative, or Zero Integers
*/
void main() {
int a;
cout << "Determine if the integer is positive/negative/zero \n";
cout << "Enter a number: "; cin >> a;
if (a == 0)
cout << "The number is zero " << endl;
else if (a % 2 == 0 && a > 0)
cout << "The number is an even positive integer " << endl;
else if (a % 2 == 0 && a < 0)
cout << "The number is an even negative integer " << endl;
else if (a % 2 != 0 && a > 0)
cout << "The number is an odd positive integer " << endl;
else
cout << "The number is an odd negative integer " << endl;
getch();
}
That's all for today's basic C++ programming material. Hopefully, it's useful. If you have any questions, feel free to ask without hesitation—just leave a comment below. Thank you very much!