Relation Expression in C++
A condition or logical expression is an expression that can only take the values true or false. The operands in a relational expression can be arithmetic or character string expressions. When logical fields are compared, a logical one ('1') is greater than logical zero ('0'). Symbolically, this is ('1' > '0'). The following is an example of a relational expression (x < y). It takes the value true if the value of the variable x is less than the value of the variable y.
Example: (a>b), (a==b), (a<=b), (a>=b) etc.
1. Write a C++ program to define the Arithmetic Expression.
#include<iostream>
using namespace std;
int main()
{
int x, y, result;
cout<<"Enter the values of x, y : ";
cin>>x>>y;
result = (x<y) ? x : y;
cout<<"The result of the above expression is : "<<result;
return 0;
}
Explanation: In the above program we are using relational expression ( the set of operators and operands) like result= result = (x<y) ? x : y; it is also the ternary expression.
Note: This program output is tested on www.jdoodle.con
Output:
Enter the values of x, y, z and a : 2 3
The result of the above expression is : 2
2. Write a C++ program to define the Arithmetic Expression using user defined function.
#include<iostream>
using namespace std;
int relational_exp( int, int);
int main()
{
int x, y, result;
cout<<"Enter the values of x, y : ";
cin>>x>>y;
result = relational_exp(x, y);
cout<<"The result of the above expression is : "<<result;
return 0;
}
int relational_exp( int x, int y)
{
return (x<y) ? x : y ;
}
Explanation: In the above program we are using relational expression ( the set of operators and operands) like result= result = (x<y) ? x : y; it is also the ternary expression in a user defined function relational_exp() which takes two argument in it.
Note: This program output is tested on www.jdoodle.con
Output:
Enter the values of x, y, z and a : 12 10
The result of the above expression is : 10
Thanks
Mukesh Rajput
Post A Comment:
0 comments: