English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
2

Assign to the boolean variable 'possibleCandidate' the value false if the int variable 'n' is even and greater than 2, or if the variable 'n' is less than or equal to 0; otherwise, assign true to 'possibleCandidate'.

Assume 'possibleCandidate' and 'n' are already declared and 'n' assigned a value.

2006-09-14 12:50:11 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

okay lets reread what your saying.
- We have to assign a boolean variable called 'possibleCandidate' which we set the value to false
- Now we need to make a conditional stating that if the variable n is even and greater than 2 OR the variable n is less than or equal to 0 [ SET THAT TO FALSE ]
- But set the other side to TRUE.


bool possibleCandidate = false;
int n = 0;

if(n%2 == 0)) {
if ( n > 2 || n<= 0) {
possibleCandidate = false;
} else {
possibleCandidate = true;
}
}


Thats how I understood your question. Rearrange hte nested loop to fit your needs.

Good Luck

2006-09-14 13:00:26 · answer #1 · answered by ? 6 · 0 2

A more succinct version of Mohamed's answer that exactly matches the description of the problem:

boolean possibleCandidate = !(n % 2 == 0 && n > 2 || n <= 0);

I'm not saying it's more readable; I just wanted to show you another way of arriving at the same answer.

You could do some meddling to the expression by applying De Morgan's law: http://en.wikipedia.org/wiki/De_Morgan%27s_laws

2006-09-14 14:23:11 · answer #2 · answered by Arthaey 2 · 0 2

if (n % 2 == 0 && n > 2 || n <= 0){
possibleCandidate = false;
}else{
possibleCandidate = true;
}

2016-08-31 13:27:10 · answer #3 · answered by ? 1 · 2 0

fedest.com, questions and answers