MEA 20 Manoj Pokharel

You might also like

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

Kathmandu University

Dhulikhel, kavre

Assignment 03

Summitted by
Name; Manoj Pokharel
Mechanical (Automobile)
Roll.no; 20
1st YEAR/1sem

Summitted to
Amrit Dahal
1. Define operators. List out the operators used in C Programming.

Operators are symbol which tells the compiler to perform certain operations on
variables. For example, (*) is an operator which is used for multiplying two numbers. List of
operators in C Programming are;

• Arithmetic Operators;
Arithmetic Operators are the type of operators which take numerical values (either
literals or variables) as their operands and return a single numerical value. For example;
(+) Adds operands a+b =12
(-) Subtracts second operand from the first a-b =4
(*) Multiplies both operands a*b =32
(/) Divides numerator by denominator. a/b =2
(%) Modulus Operator returns the remainder after an integer division. a%b=0

• Relational Operators;
Relational Operators check the relationship between two operands. If the relationship
is true, it returns 1, if the relationship is false, it returns 0. For example;
(==) Equal to,
(! =) Not equal to,
(>) Greater than,
(<) Less than,
(>=) Greater than or equal to,
(<=) Less than or equal to

• Logical Operators.
A logical operator is a symbol or word used to connect two or more expressions such
that the value of the compound expression produced depends only on that of the original
expressions and on the meaning of the operator. For example,
(&&), Called Logical AND operator.
(||), Called Logical OR Operator.
(!)Called Logical NOT Operator.

• Bitwise operators
Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bits from right to left. For example;
(&) Bitwise AND
(|) Bitwise OR
(^) Bitwise exclusive OR
(<<) left shift
(>>) right shift
• Assignment operator operators
An assignment operator is the operator used to assign a new value to a variable,
property, event or indexer element in C# programming language. It can also be used for
logical operations such as bitwise logical operations or operations on integral operands and
Boolean operands. For example;
(=): This is the simplest assignment operator
(+=): This operator is combination of ‘+’ and ‘=’ operators.
(- =): This operator is combination of ‘-‘and ‘=’ operators.

• Special operators;
The Special Operators are used for special functions in C programs. For example;
&, This is used to get the address of the variable.
*, This is used as pointer to a variable.
Size of (), This gives the size of the variable.

2. Describe the five arithmetic operators in C. Summarize the rules associated with their use.
• Addition (+)
Addition is an operator which adds the given two numbers and finds their sum.
• Subtraction (-)
Subtraction is an operator which subtracts the given two numbers and finds
differences.
• Multiplication (*)
Multiplication is an operator which multiplies the given two numbers and finds
products.
• Division (/)
Division is an operator which divides the given two numbers and finds
quotient.
• Modulus (%)
Modulus is an operator which finds the remainder after the integral division of the
two given numbers.
Some rules for arithmetic operator are;
Multiplication, Division and Modulus operators are evaluated next. If an expression contains
several multiplications, division and modulus operators, evaluation proceeds from left to
right. These three are at the same level of precedence.
Addition, subtraction is evaluated last. If an expression contains several additions and
subtraction operators, evaluation proceeds from left to right. Or the associativity is from left
to right.
3. Present a brief account of increment and decrement operators with examples.

Increment Operators are used to increase the value of the variable by one and
Decrement Operators are used to decrease the value of the variable by one in C programs.
Both increment and decrement operator are used on a single operand or variable, so it is
called as a unary operator. Syntax ( ++ ) increment operator ( -- ) decrement operator
Type of Increment / decrement Operator
• pre-increment/ decrement

In pre-increment first increment/decrement the value of variable and then used inside the
expression (initialize into another variable). Syntax for pre-increment/ decrement ++
variable;/ -- variable; respectively

#include<stdio.h>void main ()
{int x,i;
i=5;
//Increment case // decrement case
x=++i; x=--i;
printf("x: %d", x); printf("x: %d", x);
printf("i: %d", i); printf("i: %d", i);
getch ();} getch (); }

Output= 6 6 output= 4 4

• post-increment/ decrement

In Post-decrement/increment first value of variable is used in the expression (initialize into


another variable) and then decrement/increment the value of variable.

#include<stdio.hvoid main ()
{int x,i;
i=5;
//Increment case // decrement case
x=i++; x=i--;
printf("x: %d",x); printf("x: %d",x);
printf("i: %d",i); printf("i: %d",i);
getch();} getch();}

output 5 6 output 5 4
4. Define operator precedence. What are the relative precedence of the relational, equality
and logical operators with respect to one another and with respect to arithmetic and unary
operators? What are their associativities?

Operator precedence determines the grouping of terms in an expression and decides how
an expression is evaluated.
The relative precedence of rational, equality and logical operators with respect to one
another is given below-
• Relation operator (<<=.>>=) is given first order precedence.
• Equality operator (==! = ) is given second order precedence.
• Logical And (&&) and Logical OR (I I) is given third and fourth order precedence
respectively. The relative precedence of rational, equality and logical operators with respect
to arithmetic and unary operator are given below
Category operator precedence Associativity
Unary +-! ~++-- (type)* First Left to Right
&size of
multiplicative */% second Left to Right
Additive +- Third Left to Right
Relational <<=>>= Fourth Left to Right
Equality ==!= Fifth Left to Right
Logical &&,II Sixth Left to Right

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

Library functions are built-in functions that are grouped together and placed in a common
location called library. These are used to perform the most common operations like
calculations. These library functions are created by the persons who designed and created C
compilers.
User can make the long code simple, easier and fast by using library function wich are
already exist there. With the help of library function output can be generated with more
efficiency which makes it more reliable.

6. Suppose x, y and z are floating-point variables that have been assigned the values x= 8.8,
y=3.5and 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)
Ans
a) x+y+z
8.8+3.5-5.2
= 7.1
b) x/(y+z)
8.8 / (3.5-5.2)
= -5.17
c) 2*y+3*(x-z)
2*3.5 + 3(8.8 + 5.2)
= 49
d) (x/y) +z
8.8/3.5 - 5.2
= -2.69
e) x % y
8.8/3.5
= 2.51
f) 2*x/(3*y)
2*8.8 / 3 * 3.5
= 1.67

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.

The commonly used input functions in c are scanf, getchar, getcha, getch, gets
The commonly used output functions in c are printf, putch, putchar,puts

Scanf is input/output function that enables user to enter the value in program with specific
memory location to the variables.
Scanf is a c function to read input until encountering whitespace or newline whereas
getchar reads a character only from standard input stream
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

#include<stdio.h>
int main()
{
float num1, num2, num3,sum, avg;
printf("Enter three Numbers: ");
scanf("%f %f %f",&num1, &num2, &num3);
sum = num1 + num2 + num3;
avg = sum / 3;
printf("Entered numbers are: %.2f, %.2f and %.2f\n",
num1, num2, num3);
printf("Sum=%.2f\n", sum); //for sum
printf("Average=%.2f\n",avg ); // for average
//for greatest among three
if(num1 > num2 && num1 > num3)
printf("%f is greatest among three",num1);
else if(num2 > num3)
printf("%f is greatest among three",num2);
else
printf("%f is greatest among three",num3);
// for smallest among three
if(num1 < num2 && num1 < num3)
printf("%f is smallest among three",num1);
else if(num2 < num3)
printf("%f is smallest among three",num2);
else
printf("%f is smallest among three",num3);
return 0;
}
9. WAP that determines whether a given integer is odd or even and displays the number and
description on the same line.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("given number is %d which is even.", num);
else
printf("given number is %d which is odd", num);
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/2 c) (x+y)(x-y)

#include<stdio.h>
int main()
{
int x,y;
printf("Please enter the value of x: \n");
scanf("%d", &x);
printf("Please enter the value of y: \n");
scanf("%d", &y);
//a) (x + y) / (x - y)
int a = (x + y) / (x - y);
printf("The value of the above expression is: %d \n", a);
//b)(x + y) / 2
int b;
b = (x + y) / 2 ;
printf("The value of the above expression is: %d \n", b);
//c)(x + y) (x - y)
int c;
c = (x + y) * (x - y);
printf("The value of the above expression is: %d \n", c);
return 0;
}
11. WAP that calculates the area of a triangle.
#include <stdio.h>
int main()
{
float base, height, area;
printf("Enter base of the triangle: ");
scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);
area = (base * height) / 2;
printf("Area of the triangle = %.2f sq. units", area);
return 0;
}

You might also like