Assignment 3: COMP102: 24 - Bishakhasitaula (Meii)
Assignment 3: COMP102: 24 - Bishakhasitaula (Meii)
Assignment 3: COMP102: 24 - Bishakhasitaula (Meii)
Assignment 3: COMP102
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
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:
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;
}