Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Pre-lab activities:
In-lab activities:
• Note down errors observed while executing program and remedy for that.
• Note down corrections made to the code during the lab session
• Answer to vivo-voce
• Get the observation corrected
• Note down inferences on the topic covered by the programs executed
Post-lab activities:
• Solve the given exercises
• Devise possible enhancements that can be made to the solved problem to
simplify the logic
Executed programs should be recorded in the lab record and corrected within
one week after completion of the experiment.
General Instructions:
• Student should sign in the log register before accessing the system.
• Student is only responsible for any damage caused to the equipment in the
laboratory during his session.
• Usage of pen drives is not allowed in the lab.
• If a problem is observed in any hardware equipment, please report to the lab
staff immediately; do no attempt to fix the problem yourself.
• Systems must be shut down properly before leaving the lab.
• Please be considerate of those around you, especially in terms of noise level.
While labs are a natural place for conversations regarding programming,
kindly keep the volume turned down
Exercise l
Solving problems such as temperature conversion, student grading, income tax calculation,
etc., which expose students to use basic C operators
Exercise 2
2’s complement of a number is obtained by scanning it from right to left and complementing
all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a
C program to find the 2’s complement of a binary number.
Exercise 3
a) Write a C program to find the sum of individual digits of a positive integer.
b) A Fibonacci sequence is defined as follows: the first and second terms in the sequence are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence.
Write a C program to generate the first n terms of the sequence.
c) Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
d) Write a program which checks a given integer is Fibonacci number or not.
Exercise 4
a) Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
b) Write a C program to find the roots of a quadratic equation.
Exercise 5
a) The total distance travelled by vehicle in ‘t’ seconds is given by distance = ut+1/2at 2 where
‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find
the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. The program
should provide the flexibility to the user to select his own time intervals and repeat the
calculations for different values of ‘u’ and ‘a’.
b) Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result. (Consider the operators +,- ,*, /, % and use
Switch Statement)
Exercise 6
a) Simple programming examples to manipulate strings.
b) Verifying a string for its palindrome property
Exercise 7
Write a C program that uses functions to perform the following operations:
i. To insert a sub-string in to given main string from a given position.
ii. To delete n Characters from a given position in a given string.
iii. To replace a character of string either from beginning or ending or at a specified location
Exercise 8
Write a C program that uses functions to perform the following operations using Structure:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complex numbers
Exercise 9
***
Exercise 1
1. Temperature conversion
a) Centigrade to Fahrenheit
Input: temp in 0c
Output: temp in F
Step 1: Start
Step 2: read temperature in centigrade
Step 3: fc × 9/5 +32
Step 4: print result
Step 5: end
b) Fahrenheit to centigrade
Input: temp in F
Output: temp in 0c
Step 1: Start
Step 2: read temperature in Fahrenheit
Step 3: cf-32 × 5/9
Step 4: print result
Step 5: end
2. Student Grading
Input: marks in 6 subjects
Output: grade
Step 1: Start
Step 2: read marks in six subjects
Step 3: compute total
Step 4: compute average
Step 5: check if average > 75
Step 6: print grade as A
Step 7: else if average is <75 and >60
Step 8: print grade as B
Step 9: else if average is <60 and >35
Step 10: print grade as C
Step 11: else print result as fail
Step 12: end
3. Income tax ( no tax upto 1.6 lakh, 10% upto 5 lakh, 20% for 5 to 8 lakh, 30% for
above 8 lakh)
Input : annual income (ai) , non taxable amount (nta) = 1,60,000-00
Output: tax amount
Step 1: start
Step 2: read annual income or monthly income
Step 3: check if ai < nta
Step 4: print tax is 0
Step 5: else if annual income is in between nta and 5 lakh
Step 6: tax (ai – nta) * 10/100
Step 7: else if annual income > 5 lakh and less than 8 lakh
Step 8: tax(5 lakh-nta)*10/100 + (ai-5 lakh)*20/100
Step 9: else if annual income > 5 lakh and less than 8 lakh
Step 10: tax(5 lakh-nta)*10/100 + (3 lakh)*20/100 + (ai- 8 lakh)*30/100
Step 11: print tax
Step 12: end
Exercise 2
2’s complement of a given binary number
Input: char st[20]; integer i, j
Output:
Step 1: Start
Step 2: read binary no as string in to st
Step 3: repeat steps 4, 5 and 6 until i from (string length of st -1) to 0
Step 4: check if st[i] equal to ‘1’
Step 5: ji
Step 6: break
Step 7: repeat steps 8 to 11 until j from j-1 to 0
Step 8: check if st[j] equal to ‘1’
Step 9: st[j] ’0’
Step 10: else
Step 11: st[i] ’1’
Step 12: print result string st
Step 2: read n
Step 3: repeat steps 4 to 10 for i : 0 to n
Step 4: flag 0
Step 5: repeat steps 6 to 8 for j: 2 to i/2
Step 6: if i mod j equals to 0
Step 7: flag 1
Step 8: break;
Step 9: if flag equal to 0
Step 10: print i
Step 11: Stop
Exercise 4
a) To Calculate the following sum, Sum = 1-x2/2!+ x4/4!- x6/6!+ x8/8!- x10/10!
Input: integer x
Output: sum of the series
Step 1: Start
Step 2: read x, sum 1, s 1, i 2, j 0
Step 3: repeat steps 4 to 9 for i: form 2 to 10 in steps of 2
Step 4: p 1, f 1
Step 5: repeat steps 6 and 7 for j: from 1 to i
Step 6: pp*x
Step 7: ff*j
Step 8: s s * (-1)
Step 9: sum sum + s * (p / f)
Step 10: print sum
Step 11: Stop
Exercise 5
a) The total distance traveled by vehicle in ‘t’ seconds is given by d=ut+0.5at 2. Write a C
program to find the distance traveled in regular intervals of time for different values of
u & t.
Input: integer u, a, interval, no_of_intervals
Output: Distance traveled at regular intervals
Step 1: Start
Step 2: ch ’y’
b) Write a c program, which takes two integer operands and one operator from the
user, performs the operation and then prints the result.
Input: integer a, b & operator
Output: Result based on operator
Step 1: Start
Step 2: read two integers a, b and the operator
Step 3: if operator is ’+’ goto step 8
Step 4: if operator is ’-’ goto step 9
Step 5: if operator is ’*’ goto step10
Step 6: if operator is ’/’ goto step 11
Step 7: if operator is ’%’ goto step 12
Step 8: print result of a+b
Step 9: print result of a-b
Step 10: print result of a*b
Step 11: print result of a/b
Step 12: print result of a%b
Step 13: Stop
Exercise 6
a) Simple programming examples to manipulate strings.
/* strlen, strcpy, strrev, strcat*/
Input : st1, st2
Output:
Step 1: Start
else
Step 9: print ‘given string is not palindrome’
Step 10: end
Exercise 7
Write a c program that uses functions to perform the following operations
i) To insert a sub-string into given main string form a given position
ii) To delete n char form a given position in a given string
iii) To replace a character of string either from beginning or ending or at a specified
location
Exercise 8
Write a C Program that uses functions to perform the following operations using
Structure:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
Exercise 9
a) Addition of Two Matrices
Step 1: begin
Step 2: repeat steps 3, 4 for i: from 1 to r1
Step 3: repeat step 4 for j: form 1 to c1
Step 4: d[i][j]a[i][j] + b[i][j]
Step 5: end
Algorithm for sub routine mat_print(int pm[][], int r1, int c1)
Step 1: begin
Step 2: repeat steps 3 to 5 for i: from 1 to r
Step 3: repeat step 4 for j: form 1 to c
Step 4: print pm[i][j]
Step 5: print ‘ ‘
Step 6: end
Exercise 10
a) To find factorial of given number using both recursive and non-recursive functions.
Algorithm: /* factorial_recursive & non-recursive */
Input: integer n
Output: factorial of given number
Step 1: Start
Step 2: read n
Step 3: factorial call sub-routine fact(n)
Step 4: print factorial
Step 5: factorial call sub-routine fact_rec(n)
Step 6: print factorial
Step 7: Stop
Algorithm for sub-routine fact(int n)
Step 1: begin
Step 2: f 1, i 1
Step 3: repeat step 4 for i: from 1 to n
Step 4: ff*i
Step 5: return f
Step 6: end
Algorithm for sub routine fact_rec(int n)
Step 1: begin
Step 2: f 1
Step 3: if n equal to 0 go to step 4 else go to step 5
Step 4: return 1
Step 5: f n* fact_rec(n-1)
Step 6: return f
Step 7: end
b) To find GCD of given two numbers using both recursive and non-recursive functions.
Algorithm: /* GCD_recursive & non-recursive */
Input: integer a, b
Output: GCD of a, b
Step 1: Start
Step 2: read a, b
Step 3: call sub routine gcd_non_rec(a, b)
Step 4: print GCD
Step 5: call sub routine gcd_rec (a, b)
Step 6: print GCD
Step 7: Stop
Algorithm for Sub Routine gcd_non_rec(int a, int b)
Step 1: begin
Step 2: check if a < b then
Step 3: swap a,b values
Step 4: repeat steps 5 to 9 until b not equals 0
Step 5: ca mod b
Step 6: if c equals to 0
Step 7: return b
Step 8: ab
Step 9: bc
Step 10: end
Algorithm for Sub Routine gcd_rec(int a, int b)
Step 1: begin
Step 2: check if b equal to 0
Step 3: return a
Step 4: else
Step 5: call sub-routine gcd_rec(b, a mod b)
Step 6: end
Exercise 11
a) Write a C functions to find both the largest and smallest number of an array of
integers.
Input: integer n
Output: largest = max, smallest = small
Step 1: Start
Step 2: read n
Step 3: repeat step 4 for i : 0 to n
Step 4: read a[i]
Step 5: maxa[0], smalla[0]
Step 6: repeat steps 7 to 10 for i: from 1 to n
Exercise 12
a) Write a C function to generate Pascal’s Triangle.
Input: integer n
Output:
Step 1: Start
Step 2: read n (no of rows), bin1
Step 3: repeat steps 4 to 11for i: from 0 to n
Exercise 13
a) Write a C function to read in two numbers, x and n, and then compute the sum of
this geometric progression: 1+x+x2+x3+………….+xn
Input: integer x, n
Output:
Step 1: Start
Step 2: read x and n, p0
Step 3: check if n less than 0
Step 4: print error message and goto step 2
Step 5: else
Step 6: sum1
Step 7: repeat step 8 to 10 for i: from 1 to n
Step 8: repeat step 9 for j: from 1 to i
Step 9: p p*x
Step 10: sum sum + p
Step 11: print x, n, sum
Step 12: Stop
b) Write a C function to read in two numbers, x and n(no. of terms), and then compute
sin(x) and cos(x).
sin(x) = x - x3/3! + x5/5! – x7/7! + . . . .
Step 1: Start
Step 2: read n, x
Step 3: sum x, pt x
Step 4: repeat steps 5 to 8 until i < n
Step 5: nt pt * (x*x) / (2*i)(2*i+1)
Step 6: sum + sign * nt
Step 7: pt nt
Step 8: sign *= -1
Step 9: print sum of sin series
Step 10: Stop
Exercise 14
a. Pointer based function to exchange value of two integers using passing by address.
Step 1: Start
Step 2: read a,b
Step 3: print ‘ a, b values ’ i.e., before swaping
Step 4: call sub-routine swap(&a, &b)
Step 5: print ‘ a,b values’ i.e., after swaping
Step 6: Stop
Exercise 15
Examples which explores the use of structures, union and other user defined variables
a) Write a c program to store student details like regd no, name, marks in 3 subjects
for n number of students.
Input:
Output:
Step 1: Start
Step 2: declare structure student
Step 3: loop; for i from 0 to n
Step 4: read rno, name, marks in 3 sub’s
Step 5: loop: for i from 0 to n
Step 6: print student details
Step 7: Stop
Exercise 16
a) Write a program which copies one file to another
Step 1: Start
Step 2: declare File pointers *fp1, *fp2
Step 3: read filename for source
Step 4: fp1fopen(source,”w”)
Step 5: check if fp1 equal to NULL
Step 6: print file not opened, go to step 26
Step 7: else
Step 8: repeat steps 9, 10 char != 27 (ASCII value 27 for ESC key)
Step 9: read char
Step 10: fputc(ch, fp1)
Step 11: close file pointer(fp1)
Step 12: read filename for destination
Step 13: fp1fopen(source, “r”)
Step 14: check if fp1 equal to NULL
Step 15: print file not found, go to step 26
Step 16: else
Step 17: fp2fopen(destination,”w”)
Step 18: check if fp2 equal to NULL
Step 19: print file not open go to step 26
Step 20: else
Step 21: repeat steps 15, 16 until fp1!= EOF
Step 22: ch fgetc(fp1)
Step 23: fputc(ch, fp2)
Step 24: close file pointer fp1
Step 25: close file pointer fp2
Step 26: Stop
b) Write a C program to reverse first n chars in a file.
Step 1: Start (no of arguments, arguments values)
Step 2: declare file pointer *fp
Step 3: fp fopen (argv[1],”r+”)
Step 4: Check if fp equal to NULL
Step 5: print file not found
Step 6: else
Step 7: nargv[2]
Step 8: repeat step 9 for i: from 0 to n
Step 9: st[i]fgetc(fp)
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
Task Sheet-III
1 1) Write a program to calculate length of the string using pointers.
2 2) Write a function to swap two variables using pointers.
3 3) Program to illustrate pointer arithmetic.
4 4) Write a function to calculate sum of two numbers using pointers to functions.
5 5) Write a program to perform matrix multiplication using pointers.
6 6) Write a program to find the largest in an array using pointers.
7 7) Program to arrange the given numbers in ascending order.
8 8) Write a C program using pointer for string comparison.
9 9) Program to reverse the string using pointers.
10 10 The names of employees of an organization are stored in three arrays, namely, first
name, second name, and last name. Write a program using pointers to concatenate the
three parts into one string to be called name.
Task Sheet-IV
1) Write a C program to compute the monthly pay of 100 employees using each employee’s
name, basic-pay. The DA is computed as 52% of the basic pay. Gross-salary (Basic-
pay+DA).Print the employees name and gross salary.
1 2) Write a program to calculate and print student wise total for 50 students and 3
Task Sheet-V
1 1) Write a C program to read a text file and to count Number of characters, Number
of words and Number of sentences and write in an output file.
2 2) Write a C program to read the text file containing some paragraph. Use fseek() and
read the text after skipping n characters from the beginning of the file.
3 3) Write a C program to read the text file containing some paragraph. Print the first n
characters from the beginning of the file.
4 4) Write a program to print the output of the following format in an OUTPUT file.
Number Square Cube
2 4 8
3 9 27
5) Write a C program to read data from a file named INPUT containing numbers.
Write all even numbers to file called EVEN and odd numbers to file called ODD.
6) Program to print the n characters from mth position in the file.
7) Program to print the current position value of the file pointer.
8) Write a C program to open a pre-existing file and add information at the end of file.
Display the contents of the file before and after appending
9) Program to copy one file to another.
10) Write program to read a text file and convert the file contents in capital (upper-
case) and write the contents in an output file.
i=20;
printf("%d",i);
}
Input problems
1. Use the correct conversion specifier for the declared data type in a scanf(). A float
is %f, an int is %d, and a double is %lf (lower case L). If you declare a variable to be
an int, don't try to input it with a %f specifier.
3. Use the addressing operator (&) for your variables used as targets for input with
scanf()'s, unless the target is already an address (such as the name of an array or a
pointer variable).
4. The scanf() function can't be used with the %s specifier to input a string that contains
white space (like "Electric sander"). Use another function, like gets().
Output problems
1. Do not use the addressing operator (&) for arguments in a call to printf( ).
2. When using puts( ), a newline character ('\n') is automatically appended, so don't use
it for a prompt if you want the user's input on the same line.
if (x > 0) /* correct */
...
2. Use braces ( { } ) around the body of an if or else branch that contains multiple
statements. You don't need them with a single statement in the body.
3. If you have a dual-alternative selection to implement, use the if...else rather than
switch – it's cleaner.
2. The loop index (loop control variable) is not updated inside the loop
1. You can't compare two strings with with a relational or equality operator (like >= or
==). You must use one of the comparison functions from string.h, like strcmp() or
strncmp( ), or write your own function.
2. To assign (copy, duplicate) one string to another, use one of the copy functions from
string.h like strcpy( ) or strncpy( ), not the assignment operator (=).
You can fopen() a file more than once in a program, but a fclose() must be executed
between fopen()'s. Review your algorithm to make sure you really need multiple file open
and closes.