0% found this document useful (0 votes)
69 views5 pages

COL100 Lab 5: Objective

The document provides instructions for a lab assignment on writing C programs involving loops and conditional statements. Students are asked to complete 4 programming problems by the end of the 1 hour and 45 minute lab session. An additional set of homework programs is provided to work on outside of the lab session. Useful Linux commands and tips for using the vim text editor are also included.

Uploaded by

Siddharth Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views5 pages

COL100 Lab 5: Objective

The document provides instructions for a lab assignment on writing C programs involving loops and conditional statements. Students are asked to complete 4 programming problems by the end of the 1 hour and 45 minute lab session. An additional set of homework programs is provided to work on outside of the lab session. Useful Linux commands and tips for using the vim text editor are also included.

Uploaded by

Siddharth Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COL100 Lab 5

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. Write a program using loops and conditional statements to determine the


numbers between 0 and n which are-
(a) Multiples of 2
(b) Multiples of 2 but not 5
(c) Multiples of 2 or 5 or both
Your program should take the value of n from the user.
3. Write a program to generate the following pattern of * with n columns
on terminal using loops. Your program should prompt the user to enter
the value of n i.e., the number of columns in the pattern.
Example: For n = 5

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

4. Write a program to check whether a number is a palindrome or not. Note


that a number is a palindrome if the reverse of that number is same as the
original number. Your program should take a number as an input from
the user and prompt a message on terminal saying whether that number
is a palindrome or not.
Example 1:
Enter a number: 54345
54345 is a palindrome
Example 2:
Enter a number: 23451
23451 is not a palindrome

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)

5. (a) Write a program to print the first n terms of an Arithmetic Progres-


sion. Your program should prompt the user to input the first term a,
the common difference d, and the count of terms n that need to be
printed. Then the program should print the first n numbers of the
series.

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.

Useful Commands in Linux


1. Open terminal: Ctrl + Alt + T
2. Terminate current Linux command: Ctrl + C
3. Make a new directory: mkdir dirname

4. Copy: cp src dest


5. Rename: mv originalname newname
6. Delete: rm filename

7. Change working directory: cd path


8. List contents of a folder: ls
9. List contents of a folder including hidden files: ls -a
10. Print current directory: pwd

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)

Optional : Use vim editor


1. Open a file: vim filename.txt
2. Insert in a file: i ( insert mode) (Use Esc to come out of the insert mode)

3. Navigation: arrow keys


4. Undo u
5. Redo Ctrl+R
6. Saving a file :w

7. Closing a file without saving :q!


8. Saving and closing a file :wq
9. Deleting a line dd

10. Copying a line yy


11. Pasting a line p

You might also like