Arithmetic Expression in C
Arithmetic Expression in C
An arithmetic expression in C is composed of operators and operands. Operators act on operands to a result. Commonly use arithmetic operators are +, -, *, / and %
The plus sign (+) is used to add two values, the minus sign (-) to subtract one value from another, the asterisk(*) to multiply two values, the division (/) to divide a value, and the modulus (%) to obtain the remainder of integer division. These are known as binary operators since they operate on two values or variables.
Following are examples of arithmetic expressions :
result = x – y;
total = principle + interest;
numsquare = x * x;
celcius = (fahrenheit – 32) / 1.8
Program
#include <stdio.h> int main (void) { int a = 100; int b = 2; int c = 25; int d = 4; int result; result = a - b; // subtraction printf ("a - b = %i\n", result); result = b * c; // multiplication printf ("b * c = %i\n", result); result = a / c; // division printf ("a / c = %i\n", result); result = a + b * c; // precedence printf ("a + b * c = %i\n", result); printf ("a * b + c * d = %i\n", a * b + c * d); return 0; }
Output
a - b = 98 b * c = 50 a / c = 4 a + b * c = 150 a * b + c * d = 300
Arithmetic expression Evaluation
Addition (+), Subtraction(-), Multiplication(*), Division(/), Modulus(%), Increment(++) and Decrement(–) operators are the “Arithmetic expressions”. These operators work in between operands. like A+B, A-B, A–, A++, etc. While we perform the operation with these operators based on specified precedence order as like below
A+B*C/D-E%F
Arithmetic Expression evaluation order
Program
#include <stdio.h> int main() { //declaring variables int a,b,result_art_eval; //Asking the user to enter 2 numbers printf("Enter 2 numbers for Arithmetic evaluation operation\n"); //Storing 2 numbers in varaibles a and b scanf("%d\n%d",&a,&b); //Arithmetic evaluation operations and its result displaying result_art_eval = a+b*a/b-a%b; printf("================ARITHMETIC EXPRESSION EVALUATION==============\n"); printf("Arithmetic expression evaluation of %d and %d is = %d \n",a,b,result_art_eval); return 0; }
Output
Enter 2 numbers for Arithmetic evaluation operation 10 20 ================ARITHMETIC EXPRESSION EVALUATION============== Arithmetic expression evaluation of 10 and 20 is = 30
Also, read the C Storage Classes