COL100 Lab 5: Objective
COL100 Lab 5: Objective
I semester 2016-17
Week 5, 2016
Objective
To be able to write simple C programs involving loops and conditional state-
ments.
Instructions
1. After 1 hour 45 minutes have passed, your code will be checked. Whatever
you have completed till this point will be recorded. Anything that you
complete later will not be recorded.
2. If you complete an assignment later, you can ask the TAs of your lab
session any problems and doubts that you face. There is no need to show
the TA your code, if there is no problem in it.
3. You cannot attend any lab session other that your allotted session, without
informing the TAs of the session you are attending. This too is permitted
only for genuine reasons.
4. Also, you will not get attendance, if you do not attend your own lab
session, nor will your performance be noted. (Even if you fill in the atten-
dance sheet, it will not be uploaded later.)
Programs
Press Ctrl + Alt + T to open a terminal.
cd to the directory COL100.
In this directory, create another folder, called as lab5.
cd to lab5.
NOTE: Add printf statements to see the flow of your code. It will also help
you to find out the error, if there is any.
1
1. Write a program to find 1s and 2s complement of a given 4-bit binary
number. Your program should prompt the user to enter a 4-bit binary
number and then return 1s and 2s complement of that number. Also,
your program should prompt an error message on the terminal if the num-
ber is not in binary form.
Example: Binary number: 0010
1s complement of 0010 is - 1101
2s complement of 0010 is - 1110
(Hint: while taking input from the user store individual bit of the binary
number in a unique variable)
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
2
Homework Programs
1. Write a program to perform the following bit-wise logical operations on
two 4-bit binary numbers. Your program should ask the user to enter two
4-bit numbers, one bit at a time. Also, your program should prompt an
error message on the terminal if the numbers are not in binary form.
(Hint: while taking input from the user store each bit in a unique variable.)
Logical operations: AND, OR, NAND and NOR.
Example:
Number 1 - 1100
Number 2 - 0110
Bit-wise AND - 0100
Bit-wise OR - 1110
Bit-wise NAND - 1011
Bit-wise NOR - 0001
(Your program should not use bit-wise operators.)
2. Write a program using loops and conditional statements to determine if a
number entered by the user is divisible by-
(a) 4
(b) 3
(Your program should not use modulus(%) operator.)
3. Write a program to compute the following using loops-
(a) Take two numbers as input, a and b. Now without using multiplica-
tion operand, compute the product a b.
(Hint: 5 * 3 = 5 + 5 + 5)
(b) Take two numbers as input x and y. Now without using math.pow
or power operand, compute xy .
(Hint 24 = 2 * 2 * 2 * 2).
4. Write a program to determine the greatest power of 2 which is less than
or equal to the input value n. Your program should take the value of n
from the user.
Examples:
For n = 5, return 4 (i.e. 22 < 5)
For n = 21, return 16 (i.e. 24 < 21)
For n = 29, return 16 (i.e. 24 < 29)
For n = 100, return 64 (i.e. 26 < 100)
3
(b) Write a program to produce the nth fibonacci number. Fibonacci
series looks like this: 1, 1, 2, 3, 5, 8, 13, 21, ... Notice every number
in the series is the sum of the previous two numbers.
(Hint: In Arithmetic progressions problem, the new number gets
calculated as the sum of the previous number and the common dif-
ference. Here also the new number gets calculated as the sum of the
previous two numbers, and a difference. But unlike AP, the difference
also gets updated.)
(c) A general formula to generate the fibonacci series is-
F(0) = 1, F(1) = 1,
F(n) = F(n-1) + F(n-2)
Write a program to observe that the ratio of consecutive Fibonacci
numbers converges to a fixed value i.e. F(n+1)/F(n) = where is
the Golden Ratio.
(Hint: Compute 1/1 = 1, 2/1 = 2, 3/2 = 1.5, 5/3 = 1.66667, 8/5 =
1.6, 13/8 = 1.625, 21/13 = 1.61538, ... Stop iterating the loop when
the ratio at the k th iteration is equal to (upto five decimal places)
the ratio at the (k + 1)th iteration.)
Also, compute the value of the equation (1+ 2 5)/2 and see if this
value is equal to the fixed value obtained above.
Points to Remember
1. To set proxy: Open an internet browser and set the Automatic proxy
configuration url to http://www.cc.iitd.ernet.in/cgi-bin/proxy.btech (or
4
proxy.dual if you are a Dual Degree student).
(For Firefox, open Options > Advanced > Network Tab > (Connection)
Settings > Choose Automatic proxy configuration and set the URL)