+ 3

ā€œRock the Spaceā€! (Logical operators)

ā€œAlong with other physical health requirements, astronaut candidates must have perfect vision (100%) and their height must be between 62 and 75 inches. Write a program that accordingly takes the vision percentage and height (in inches) as an input and prints "passed" if given conditions are satisfied and "failed" if otherwise.ā€ https://code.sololearn.com/crpAiAUzk0oW/?ref=app

1st Mar 2021, 3:26 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
34 Respostas
+ 8
'if' statement with multiple conditions must be put as one parenthesis expression (eventually with nested parenthesis, and ! (not) operator is missused (a not equals b is not a!b, nor a!(==b), but !(a==b) or simpliest a!=b): if (vision==100) && (height>=62) && (height<=75) { and: if (vision!100) || (height!>=62) || (height!<=75)) { should be: if (vision==100 && height>=62 && height<=75) { and: if (vision!=100 || height<62 || height>75) {
1st Mar 2021, 3:50 AM
visph
visph - avatar
+ 4
TahitišŸ· Yes but there is not any syntax like that in any programming language. You can write vision != 100 vision < 62 vision > 75 "Is not greater than or equal to" means value should be less than defined value "Is not less than or equal to" means value should be greater than defined value.
1st Mar 2021, 3:55 AM
AĶ¢J
AĶ¢J - avatar
+ 4
#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if ((vision==100) && (height>=62) && (height<=75)){ cout << "passed"; } else{ cout << "failed"; } return 0; } Is this okay??
1st Mar 2021, 4:00 AM
K.Suhanya
K.Suhanya - avatar
+ 3
K.Suhanya There is word "between" so there should not be >= in your logic.
1st Mar 2021, 4:03 AM
AĶ¢J
AĶ¢J - avatar
+ 3
Okay I Am AJ ! .I just edit else part in TahitišŸ·'s coding. Anyway thanks for your comment. šŸ˜Š
1st Mar 2021, 4:09 AM
K.Suhanya
K.Suhanya - avatar
+ 3
if (vision==100) && (height>=62) && (height<=75) { and: if (vision!100) || (height!>=62) || (height!<=75)) { should be: if (vision==100 && height>=62 && height<=75) { and: if (vision!=100 || height<62 || height>75) {
23rd May 2021, 4:03 AM
Tharul Nejana
Tharul Nejana - avatar
+ 2
TahitišŸ· in C as in almost all languages comparison operators are ==, !=, <, >, <=, >=, but not !< nor !<= nor !> nor !>= ;)
1st Mar 2021, 4:40 AM
visph
visph - avatar
+ 2
#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if ((vision==100) && (height>=62) && (height<=75)){ cout << "passed"; } else{ cout << "failed"; } return 0; } Good Luck
25th Jan 2022, 11:42 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
TahitišŸ· What is vision !100 and what is this syntax !>= and !<= You need to learn about Operators again.
1st Mar 2021, 3:47 AM
AĶ¢J
AĶ¢J - avatar
+ 1
also, you could simplify, by replacing the second 'if' statement by an 'else' related to the first 'if' statement...
1st Mar 2021, 3:52 AM
visph
visph - avatar
+ 1
TahitišŸ· Program is simple. Read the sentence again. 1 - vision should be 100 % 2 - height must be between 62 and 75. It means height > 62 && height < 75 So condition should be like this if (vision == 100 && height > 62 && height < 75) cout << "passed"; else cout << "failed";
1st Mar 2021, 4:00 AM
AĶ¢J
AĶ¢J - avatar
+ 1
and change parenthesis ^^
1st Mar 2021, 4:27 AM
visph
visph - avatar
+ 1
or 62.00001, or 74.99999
1st Mar 2021, 4:43 AM
visph
visph - avatar
+ 1
that's how problem is stated... we often need to decide less or more arbitrarly bounds. In this case the exercise seems to expect bounds not included ;)
1st Mar 2021, 5:19 AM
visph
visph - avatar
+ 1
Calvin Sheen Thomas , Yes, this helps tremendously. Thank you! I think the more times I look over the code, the more it begins to ā€œsink in.ā€
2nd Mar 2021, 6:26 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 1
Try this!!!! #include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; if(vision == 100 && height>=62 && height<=75) cout<<"passed"<<endl; else cout<<"failed"<<endl; return 0; }
8th Sep 2021, 5:51 AM
Chalat Rahul
+ 1
#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if (vision == 100 && height >= 62 && height <= 75) { cout << "passed" << endl; } else { cout << "failed" << endl; } return 0; }
21st Sep 2021, 2:16 AM
Malik Mehrab Rashid
Malik Mehrab Rashid - avatar
+ 1
#include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if(height>=62 && height<75) cout<<"passed"; else cout<<"failed"; return 0; }
13th Oct 2021, 9:01 PM
Hasan Ali
Hasan Ali - avatar
+ 1
Try this: #include <iostream> using namespace std; int main() { int vision; cin>>vision; int height; cin>>height; if (height>=62 && height<=75 && vision==100){ cout<<"passed"<<endl; } else { cout<<"failed"<<endl; } return 0; }
26th Dec 2021, 7:37 AM
Ken-Ken Dumato
Ken-Ken Dumato - avatar
+ 1
This is my code and it works somehow lol: #include <iostream> using namespace std; int main() { int vision; cin >> vision; int height; cin >> height; //your code goes here if (vision == 100 && height >= 62 && height <= 75){ cout << "passed" << endl; } else if (vision != 100 || height >= 62 || height <= 75){ cout << "failed" << endl; } return 0;
7th Feb 2022, 10:55 AM
Danijel Filipović
Danijel Filipović - avatar