Expressions
An expression is a formula in which operands are linked to each other by the use of operators to compute a
value. An operand can be a function reference, a variable, an array element or a constant.
There are four types of expressions exist in C:
• Arithmetic expressions
• Relational expressions
• Logical expressions
• Conditional expressions
1.1 Example 1: Working of post-increment and pre-increment operator
Increment operators are used to increase the value by one while decrement works opposite increment. Decrement
operator decrease the value by one.
Pre-increment (++i) - Before assigning the value to the variable, the value is incremented by one.
Post-increment (i++) - After assigning the value to the variable, the value is incremented.
The following is the syntax of pre and post increment.
+ + variable_name; ==P re ? increment
variable_name + +; ==P ost ? increment
Code:
1
Output:
1.2 Example 2: Write C Program to solve simple Arithmetic Operators
Algorithm 1 Steps in pseudo code:
1: Step1: Start
2: step2: assign value in variable a, assign value in variable b and declare variable c
3: step3: Calculate c (c = a + b)
4: step4: Display result of addition, value of c on the monitor with the message "a+b = "
5: step5: Calculate c (c = a - b)
6: step6: Display result of subtraction, value of c on the monitor with the message "a-b = "
7: step7: Calculate c (c = a * b)
8: step8: Display result of multiplication, value of c on the monitor with the message "a*b = "
9: step9: Calculate c (c = a / b)
10: step10: Display result of division, value of c on the monitor with the message "a/b = "
11: step11: Calculate c (c = a % b)
12: step12: Display result of modulo-division , value of c on the monitor with the message "Remainder when a
divided by b = "
13: step13: end
Code:
2
Output:
1.3 Example 3: Write C Program to find the Volume of a Cylinder(take input
from user).
Algorithm 2 Steps in pseudo code:
1: Step1: Start
2: step2: declare float variable vol, r and h
3: step3: Display a message on the monitor "enter radius: "
4: step4: read r
5: step5: Display a message on the monitor "enter height: "
6: step6: read h
7: step7: Calculate vol (vol =(22*r*r*h)/7)
8: step8: Display result of Volume of a Cylinder, value of vol on the monitor with the message "VOC: "
9: step9: end
Code:
Output:
3
2 Discussion & Conclusion
Based on the focused objective(s) to understand about different types of expressions in C program, the additional
lab exercise made me more confident towards the fulfilment of the objectives(s).
3 Lab Task (Please implement yourself and show the output to the
instructor)
1. Write a C program to enter two numbers and perform all arithmetic operations.
2. Write a C Program to Calculate Area and Circumference of Circle.
3. Write a C program to enter length in centimeter and convert it into meter and kilometer.
4. Write a C Program to Calculate Area of a Rectangle, take height and width as user input.
4 Lab Exercise (Submit as a report)
1. Write a C Program to Calculate Area of a Square, take length of one side as user input.
2. Write a C program to enter temperature in °Celsius and convert it into °Fahrenheit.
3. Write a C program to enter temperature in Fahrenheit(°F) and convert it into Celsius(°C).
4. Write a C program to enter marks of five subjects and calculate total and average marks.
5 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.
4