Week 3 Assignment

You might also like

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

Home Assignment for Week 3 of CSEN Lab

Program 1:
Input two integer numbers from keyboard.
Print their binary form.
Try bitwise OR, AND and XOR operations on these two numbers,
and print out the result in both
binary and standard decimal form.

Code:

Output:
Program 2:

Write the arithmetic expressions (discussed in the theory class on


20th March).
a) 3*4 + 5*6
b) 3 * 4 % 5 / 2
c) 3 * (4 % 5) /2
d) 3 * 4 % (5/2)
In each case explain your answer using rules of associativity and
precedence.
Try their floating point equivalents, if possible.
Calculate the values if possible, in both cases.

Solution:
In case a), the multiplications are evaluated first and then the
addition, leading to the result of 12+30=42

In case b), all the operands are of the same precedence, hence the
expression is evaluated according to associativity from left to
right. Hence the value is 1.

In case c), the parenthesis is evaluated first, and after that all the
operaors have same precedence so they are evaluated left to right.
Hence the answer is 6.

in case d), same thing happens as case c). Parenthesis are


evaluated first and after that all the remaining operators have same
precedence so they are evaluated left to right, so the answer
becomes 0.

We cannot use floating point numbers with the modulus operator,


however in case a), they can be used. In that case, the same rules
will apply and multiplication will have higher precedence than
addition.
Program 3:

Declare an unsigned long double, double, floating point, long


integer, integer, short integer,
character variable.
Input a value in the character variable. Now try to assign this
variable to the short integer
variable, and print its value. Repeat this in the hierarchy of type
conversion. Comment if there is
any loss of value, or if the compiler is reporting problem.

Code:

Output:
Program 4:

Repeatedly Multiply / divide an Integer by two using right / left


shift operators.
- check when the number becomes zero

Code:

Output:

You might also like