Assignment 3: COMP102: 24 - Bishakhasitaula (Meii)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

24_BISHAKHASITAULA(MEII)

Assignment 3: COMP102

1.Define operators. List out the operators used in C Programming.


Answer:
Operators are the arithmetical and logical symbols that are used for performing the
desired operations in the programming language. They are used to combine
constants, variables, array and functions in order to form an expression. The data
item that operator acts on are called operands.
There are numerous operators used in C programming. They are listed as below:
a. ARITHMETIC OPERATORS:
They are generally used for mathematical operations. There are five arithmetic
operators in C.
OPERATOR PURPOSE
+ addition
- subtraction
* multiplication
/ division
% Remainder after integer division

b. RELATIONAL OPERATORS:
They are mainly used for comparisons.
OPERATOR PURPOSE
< less than
<= less than equal to
> greater than
>= greater than equal to
== equal to
!= not equal to

c. LOGICAL OPERATORS:
They are used when multiple conditions need to be tested for decision making.
OPERATOR PURPOSE
&& and
|| or
! not

d. ASSIGNMENT OPERATOR:
They are used in order to assign the value of an expression to the variable. It is
usually denoted as “=”.
e. CONDITIONAL OPERATOR:
These operators are used to assign values to variables instead of “if-else”
statements. Denoted as- “:”, “;”, “?”.
f. UNARY OPERATORS:
The two commonly used unary operators are increment operator, “++” and
decrement operator, “—”. These operators are used either to increase or decrease
the value of operand by 1.

2. Describe the five arithmetic operators in C. Summarize the rules associated with
their use.
Answer:
The five arithmetic operators are:
+: It is used for mathematical addition and known as addition operator. For
example:
X=A+B
Here, the operator “+” sums the value of A with B operands and stores the value in
X.
-: It is known as subtraction operator and used for mathematical subtraction. For
example:
X=A-B
Here, the operator “-” subtracts the value of B from A and stores the result in X.
*: It is known as multiplication operator and is used to multiply the operands.
Example:
X=A*B where, A and B are the operands.
/: It is used for mathematical division. Example:
X=A/B where X stores the value after the result is processed by dividing A operand
by B.
%: It is referred as modulus operator and is used to obtain remainder after the
integer division. Example:
X=A%B i.e., X=A mod B.
The rules are:
#Arithmetic operators should always be written in a straight line.
#The operands acted upon by arithmetic operators must represent numeric values.
#The remainder and division operator requires second operand to be non-zero
#The negative operand values are calculated as the rules of algebra.
Rules for operator precedence:
a. Operators in expressions contained within parentheses are evaluated first.
b. Multiplication, division and modulus are evaluated next and their associativity is
from left to right.
c. Addition and subtraction are evaluated after multiply, divide and remainder.
3. Present a brief account of increment and decrement operators with examples.
Answer:
They are common unary operators. Increment and decrement operators are used
to increase or decrease the value of the operand by 1 respectively. The operand
used with each of these operators must be a single variable. The increment
operator is denoted by ++ and the decrement by --. Generally, there are two types
of increment/decrement operators.
a. Pre increment/decrement operator:
It is placed to the left of the operand in the expression. It
increases/decreases the value of operand in the assigned expression
and uses the value after increment/ decrement in the further
operation. For eg:
a=2
x=++a;
Here, the variable a is increased to 2 and used in the expression to
obtain the value of x i.e., a=a+1, x=a
The final output is a=3, x=3

b. Post increment/decrement operator:


It is placed to the right of the operand of the expression. When post
operator is in use, the value of operand in the assigned expression
remains same and after the result is obtained the operand is then
made to increase or decrease. For eg:
b=3
x=b--;
Here, in the expression the value of b is unchanged and used to obtain
result i.e.
x=b--
x=3
then, the value of operand is decreased by 1. Hence, the output will
be x=3, b=2.
4. Define operator precedence. What is the relative precedence of the relational,
equality and logical operators with respect to one another and with respect to
arithmetic and unary operators?
What is their associativity?
Answer:
Operator precedence generally means grouping of the operators on the basis of its
evaluation in the certain expression. The operator having highest precedence
amongst the assigned operator in the expression is evaluated initially and then
others in the descending order.

Let us consider an expression;


a>0 && (a<=10=!b)
Here, in the above expression, the complex part is evaluated first as
(a<=10=!b)
Let us take
a=1
b=2
then, the relational operator is evaluated first i.e.
a<=10
1<=10
which is true.
Hence, the equality operator is then evaluated as:
(a<=10=!b)
(1<=10=!2)
which is also true and finally the logical operator is also evaluated i.e.
a>0 && (a<=10=!b)
1>0 && (1<=10=!2)
which is also true.
Hence, their relative precedents and associativity are listed from highest to
lowest as below:
OPERATOR CATEGORY OPERATOR ASSOCIATIVITY
Relational < <= > >= Left Right
Equality == != Left Right
Logical && || Left Right

Unary followed by arithmetic operators are always evaluated at the highest in any
expressions. Hence the relative precedence of the relational, equality and logical
operators w.r.t arithmetic and unary operators are:

OPERATOR CATEGORY OPERATOR ASSOCIATIVITY


Unary ++ -- Right Left
Arithmetic +-*/% Left Right
Relational < < = > >= Left Right
Equality == != Left Right
Logical &&|| Left Right

5. What are library functions? Why are they important?


Answer:
Library functions are built-in functions that are already defined, compiled and
stored in the C library. This type of function cannot be modified by the users. Library
functions are mostly declared in header files and stored as: file_name.h. The most
commonly used library function are standard Input/Output functions for reading
and writing files, numbers, opening and closing and so on. Likewise, there are
numerous other library functions used for specific purposes.
Library functions are used to carry out various operations and each library function
in C program performs specific operation. The library functions are portable and
can be used on any device at real-time. We can make use of functions to get pre-
defined output instead of writing own codes. Hence, it helps in time management.
Use of library function makes program compilation and execution process much
efficient and convenient. Therefore, library function holds huge significance in C
programming language.
6. Suppose x, y and z are floating-point variables that have been assigned the values
x= 8.8, y=3.5 and z= -5.2. Determine the values of each of the following arithmetic
expressions:
a) x+y+z
b) x/(y+z)
c) 2*y+3*(x-z)
d) (x/y) +z
e) x % y
f) 2*x/(3*y)
Answer:
EXPRESSION VALUES
x+y+z 7.1

x/(y+z) -5.17647059

2*y+3*(x-z) 49

(x/y) +z -2.68571429

x%y 1.8

2*x/(3*y) 1.67619048

7. List out the commonly used input/output functions in C. What is the purpose of
scanf function? How is it used in a C Program? Compare scanf with getchar
function.
Answer:
The commonly used input/output functions in C are:
1.getchar
2.putchar
3.scanf
4.printf
The purpose of scanf function is to enter data items from the standard input device
and store it in some variables for further use in the program.
In C program scanf is used to enter any combination of numerical values, single
characters and strings. Here, control strings are used with % which gets the address
of integer, and the value entered by the user is stored in that address.
scanf getchar
It is used for formatted input. It is used to read a single character from
a keyboard on the output screen.
Two arguments are passed. Only one argument is passed.

8. WAP to read three values using scanf function and print the following results:
a) sum of the values
b) average of the three values
c) largest of the three
d) smallest of the three
Answer:
a. //program to find sum of the three
#include <stdio.h>

int main()
{
float a,b,c,sum;
printf("Enter three values:");
scanf("%f%f%f",&a,&b,&c);
sum=a+b+c;
printf("Sum of three values is :%f",sum);

return 0;
}
b. //program to find average of three
#include <stdio.h>
int main()
{
float a,b,c,average;
printf("Enter three values:");
scanf("%f%f%f",&a,&b,&c);
average=(a+b+c)/3;
printf("Average of three values is :%f",average);
return 0;
}
c.//program to find greatest of three

#include <stdio.h>

int main()
{
int a,b,c;
printf("Enter three values:");
scanf("%d%d%d",&a,&b,&c);
{
if (a>b&&a>c)

{
printf("a is the greatest");
}
else if(b>a&&b>c)
{
printf("b is the greatest ");
}
else
printf("c it the greatest");
return 0;
}
}
d. //program to find smallest of three numbers
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three values:");
scanf("%d%d%d",&a,&b,&c);

{
if (a<b&&a<c)
{
printf("a is the smallest");
}
else if(b<a&&b<c)
{
printf("b is the smallest");
}
else
printf("c it the smallest");

return 0;
}
}
9. WAP that determines whether a given integer is odd or even and displays the
number and description on the same line.
Answer:

int main()
{
int a;
printf("Enter an integer:");
scanf("%d",&a);
printf("a=%d ",a);

{
if (a % 2==0)

printf("%d is even",a);
else
printf("%d is odd",a);

return 0;
}
}
10. WAP to read the values of x and y and print the results of the following
expressions in one line.
a) x+y/x-y b) x+y /2c) (x+y)(x-y)

Answer:
a.
#include <stdio.h>

int main()
{
float x,y,result;
printf("Enter an integer:");
scanf("%f%f",&x,&y);
printf("x=%f y=%f ",x,y);

result=(x+y)/(x-y);
printf("The result is %f",result);

return 0;
}
b.
#include <stdio.h>

int main()
{
float x,y,result;
printf("Enter an integer:");
scanf("%f%f",&x,&y);
printf("x=%f y=%f ",x,y);

result=(x+y)/2;
printf("The result is %f",result);

return 0;
}
c.
#include <stdio.h>

int main()
{
float x,y,result;
printf("Enter an integer:");
scanf("%f%f",&x,&y);
printf("x=%f y=%f ",x,y);

result=(x+y)*(x-y);
printf("The result is %f",result);

return 0;
}
11. WAP that calculates the area of a triangle.
Answer:
#include <stdio.h>

int main()
{
float base,height,area;
printf("Enter the base and height of the given triangle:");
scanf("%f%f",&base,&height);
printf("base=%f height=%f ",base,height);

area=(base*height)/2;
printf("The area of the triangle is %f",area);

return 0;
}

You might also like