Arithmetic Expression in C++
An Arithmetic Expressions are use to perform a sequence of arithmetic operations to obtain a numeric value, which replaces the expression. Arithmetic operations are denoted by the arithmetic operators like +, -,*, / and %.
Rules to declare Arithmetic Expressions:
1. It should be numeric constant values.
2. It should be numeric variable which may be replaced by unary + or - operators.
3. An Arithmetic expression may be in parentheses like (a+b * c).
4. If both the operands are of same type then the resulting value is also of same type.
5. If one operand is integer and other is real, then the integer operand is first converted to real equivalent and then operated and hence the resulting value is real.
Example: (X + Y) * (Z + A) , 23 + 53 * radius
Different C++ program which further explain working of Arithmetic operators:
1. Write a C++ program to define the Arithmetic Expression.
#include<iostream>
using namespace std;
int main()
{
int x, y, z, a, result;
cout<<"Enter the values of x, y, z and a : ";
cin>>x>>y>>z>>a;
result = (x+y) * (z+a);
cout<<"The result of the above expression is : "<<result;
return 0;
}
Explanation: In the above program we are using expression ( the set of operators and operands) like result= result = (x+y) * (z+a);
Note: This program output is tested on www.jdoodle.con
Output:
Enter the values of x, y, z and a : 2 10 4 7
The result of the above expression is : 132
2. Write a C++ program to define the Arithmetic Expression using user defined function.
#include<iostream>
using namespace std;
int arithmetic_exp( int, int, int, int);
int main()
{
int x, y, z, a, result;
cout<<"Enter the values of x, y, z and a : ";
cin>>x>>y>>z>>a;
result = arithmetic_exp(x, y, z, a);
cout<<"The result of the above expression is : "<<result;
return 0;
}
int arithmetic_exp( int x, int y, int z, int a)
{
return (x +y) * (z+a);
}
Explanation: In the above program we are using expression ( the set of operators and operands) like result= result = (x+y) * (z+a) in a user defined function arithmetic_exp() which further takes 4 integers values as argument.
Note: This program output is tested on www.jdoodle.con
Output:
Enter the values of x, y, z and a : 2 10 4 7
The result of the above expression is : 132
3. Write a C++ program to define the Arithmetic Expression using template.
#include<iostream>
using namespace std;
template<class T>
T arithmetic_exp( T x, T y, T z, T a)
{
return (x +y) * (z+a);
}
int main()
{
int x, y, z, a, result;
cout<<"Enter the values of x, y, z and a : ";
cin>>x>>y>>z>>a;
result = arithmetic_exp(x, y, z, a);
cout<<"The result of the above expression is : "<<result;
return 0;
}
Explanation: In the above program we are using expression ( the set of operators and operands) like result= result = (x+y) * (z+a) in a user defined function arithmetic_exp() which further takes 4 integers values as argument. Here we are using template class. In template we can use int and float value both.
Note: This program output is tested on www.jdoodle.con
Output:
Enter the values of x, y, z and a : 2 10 4 7
The result of the above expression is : 132
Thanks
Mukesh Rajput
Post A Comment:
0 comments: