LAB 4 Manual
LAB 4 Manual
NNATIONAL
ATITIO ONALUNIVERSITY
VERSITOF
UNINIV COMPUTER
Y OFOF COMPUTAND
ER EMERGING
ER AND EMERGSCIENCES
INGG
SCICIENCESES
OPERATORS:
C Arithmetic Operators
There are many operators in C for manipulating data which include arithmetic Operators,
Relational Operators, Logical operators and many more which will be discussed
accordingly. Some of the fundamental operators are:
Operators Description Example
Example code:
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
== Equal to 5 == 3 is evaluated to 0
Example code:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
return 0;
}
Example code:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
Example code:
include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
REFERENCE SITE:
https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/
Example code:
#include<stdio.h
main() {
int a =
20; int b =
10; int c =
15; int d =
5; int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n",
e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" ,
e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" ,
e );
return 0;
}
When compile and execute the above program, it produces the following result:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Exercises
Q 1: Write a C program that accepts an employee's ID, total worked hours of a month and the
amount he received per hour. Print the employee's ID and salary (with two decimal places) of
a particular month.
Output:
Q 7: Write a C program to Find the Roots of a Quadratic Equation. Take user input values of b,a, c
−𝑏±√𝑏2−4𝑎𝑐
𝑥= Given (a≠0.)
2𝑎
Q 8: Write c program that find the result of the following operations: you are allowed to
initialize any values.
Observe and write in comment how that operation produce results.
● 5+4
● 10/2
● True OR False
● 20 MOD 3
Q 9: Aiman is fond of collecting different countries’ postage stamps. She is so passionate that
she collected 7 Pakistani stamps, 4 UK stamps, and 3 German and 3 Australian stamps. Based
on the input, identify how many international stamps she has.
Q 10: