Ternary Operator
Here is a program that takes the marks from a student (out of 100), and converts it to a grade. Use ternary operators to output the students grade.
A for marks >= 90 B for marks >= 70 and < 90 C for marks >= 50 and < 70 D for marks < 50
This is not a great use for ternary operators, but it will improve your capability of using them.
#include <iostream>
int main()
{
std::cout << "Input your marks";
int marks;
std::cin >> marks;
std::cout << "Your grade is: " << /*Your ternary operators here*/;
}
Answer
#include <iostream>
int main()
{
std::cout << "Input your marks";
int marks;
std::cin >> marks;
std::cout << "Your grade is: " <<
(marks >= 90 ? "A"
: (marks >=70 ? "B"
: (marks >= 50 ? "C" : "D")));
}