:: SHARE THIS POST ::
Home C# Interview QuestionsWhat is mixed mode arithmetic expression with an example?
In a statement or expression if one the operand is real (float) and another one is integer then expression is called as Mixed Mode Arithmetic Expression. If in an expression either operand is of real then output is always in real format. If both operands are real then output will be in real formats.
Example
Real operand and Integer operand = Real operand (Output)
Real operand and Real operand = Real operand (Output).
"*" - Multiply use for Multiplication
"/" - Divide use for division
"+" - Plus use for addition
"-" - Minus use for subtraction
"%" - Modulus use for getting a reminder
"**" - Square use for getting square number
Rule 1
Evaluate Expressions always from Left to Right
For Example: 3 + 5 - 4 = 4
Rule 2
Priority of an operator is also considered while calculating an expression
Operator Priorities in descending order
1. (**) Known as Square operator which executes From Right To Left .
For example 5**2 is equal to 25
2. (*) Known as multiplication operator which executes From Left to Right
For example 5 * 2 is equal to 10
3. (/) Known as Division operator which executes From Left to Right
For example 6 / 2 is equal to 3
4. (+) Known as Plus or Addition operator which executes From Left to Right
For example 6 + 2 is equal to 8
5. (-) Known as Minus or Subtraction operator which executes From Left to Right
For example 6 - 2 is equal to 4
Mixed mode operator example
2 + 2 * 5 ** 3
==> 2 + 2 * (5**3)
==> 2 + 2 * 125
==> 2 + (2 * 125)
==> 2 + 250 = 252
Rule 3
If the operands of this operator are of the same type means either Integer or Real, compute the same for result in that operator only.
For Example
Both Real Numbers
double a = 2.0;
double b = 3.0;
Double add = 2.0 + 3.0 = 5.0 (Result in Real Number only)
Both Integer Number
int a = 2;
int b = 3;
int add = 2 + 3 = 5 (Result in Integer Number only)
Rule 4
If one of the operator is Integer and other one is real then convert integer number to real number and compute the result in real number only.
For Example
double a = 5.0;
int b = 5;
Result = 5.0 = 5 = 10.0 (Result in Real Number only).
So this all about evaluating mixed mode arithmetic expression. Let me know your thoughts while working on mixed mode arithmetic expression. Kindly post your comments.
More C# Articles To Read Next:
What is an event handler and explain how it is designed ?
Views - 6285 |Category - C#
Is C# is Modern, Type Safe, Versionable and Inter operability Language ?
Views - 9916 |Category - C#
Discuss Boxing and Unboxing in the context of value types and reference types
Views - 7401 |Category - C#
Understanding Switch Statement with the help of an example
Views - 5141 |Category - C#
Checked and Unchecked operators using an example
Views - 5843 |Category - C#
All fields marked with * are mandatory