Introduction to C-Programming Laboratory (CSS104)
(Common to ECS, MCS, PCS,CCS)
Course Objective:
This course will help the learner to create applications / programs using C language.
List of Experiments:
1. Programs using Input, output and assignment statements
2. Programs using Branching statements
3. Programs using Looping statements
4. Programs using Functions
5. Programs using Arrays
6. Programs using Structures
7. Programs using strings
8. Programs using Pointers (both data pointers and function pointers)
9. Programs using dynamic memory allocation
10. Programs using Recursion
11. Programs using Files
12. Dividing a large program into multiple files
LEARNING OUTCOMES
The learner will be able to
Write simple programs using arrays, structures and functions.
Demonstrate dynamic memory allocation.
Construct multi file programs using file operations for a given application.
b) Sum of array of n elements
1. a) Simple and compound interest c) Ascending/Descending order
b) Swapping d) Matrix multiplication
c) Celcius to Fahrenheit e) Sparse / unit / regular square matrix
d) Centimeters to feet and inches 6. a) Polynomial addition
e) Area of triangle b) Employee pay bill preparation
2. a) Odd or Even c) Student details
b) Marks to grade 7. a) String palindrome
c) Roots of quadratic equations b) Counting characters, words and lines.
3. a) Sum of first n natural numbers 8. String processing using pointers
b) Finding n! 9. Sorting - dynamic memory allocation
c) Number Palindrome 10. a) Factorial using recursion
d) sin(x) b) Fibonacci series using recursion
4. a) GCD 11. Employee details
b) Prime/not 12. Multiple files
5. a) Maximum/minimum of an array
Ex. No: 1. Programs using Input, Output and Assignment statements
1.a) Simple and Compound Interest
Aim:
To Calculate Simple and Compound Interest.
Algorithm:
1. Read Principal amount (P), Number of Years, (N) Read Rate of Interest per Year (R)
2. Calculate
SI= (P*N*R)/100
CI = (P*(1+(R/100))N – P
3. Print SI and CI
1.b) Swapping the values of Two Variables
Aim:
To Swap the values of two variables using temporary variable
Algorithm:
1. Read value1, value2
2. Assign value1 to temp
3. Assign value2 to value1
4. Assign temp to value2
5. Display value1 and value2
1.c) Conversion of Celsius to Fahrenheit
Aim:
To Convert the temperature in Celsius to Fahrenheit
Algorithm:
1. Read Celsius value
2. Calculate Fahrenheit F = (C*9/5) +32
3. Print the converted Fahrenheit Value
1.d) Conversion of distance in centimeters to feet and inches.
Aim:
To convert the distance given in Centimeters to feet and inches.
Algorithm:
1. Read Centimeters.
2. Calculate the Feet & Inches using formula.
Temp=(12.0/30)*cm
Feet=Temp/12
Inches=Temp%12
3. Print the Feet and Inches values.
1.e) Calculation of Area of a Triangle.
Aim:
To calculate the Area of Triangle
Algorithm:
1. Read the values of a,b and c
2. Calculate
S = (a + b + c)/2
Area = sqrt(s * (s-a) * (s-b) * (s-c))
3. Print the Area of triangle.
Ex. No: 2 Programs using Branching statements
2.a) Odd or Even
Aim:
To check whether the given number is even or odd
Algorithm:
1. Read a number
2. Divide the number by 2 and get its remainder
3. If the remainder is 0 then print “Even”
4. Otherwise print “odd”
2.b) Student Mark Information
Aim:
To determine the grade based on the given marks.
Algorithm:
1. Read Marks for five subjects.
2. Calculate Total and Average
3. If Average >=90 then assign “S” Grade.
Else If Average>=85 and Average<=89 then assign “A+” Grade.
Else If Average>=80 and Average<=84 then assign “A” Grade.
Else If Average>=70 and Average<=79 then assign “B” Grade.
Else If Average>=60 and Average<=69 then assign “C” Grade.
Else If Average>=50 and Average<=59 then assign “D” Grade
Else assign “F” Grade
4. Display the Grade.
2.c Quadratic Equation
Aim:
Program to find the roots of quadratic equation (-b ± √ b2 – 4*a*c)/ (2*a)
Algorithm:
1. Read a, b and c2
2. Calculate d = b - 4*a*c
3. If d=0 then
3.1 Calculate r1 = -b/2a
3.2 Calculate r2 = -b/2a
3.3 Print “Roots are real and equal”
3.4 Print r1,r2
4. If d<0
4.1 Print “Roots are imaginary”
4.2 Real = -b/2a
4.3 Imaginary = sqrt(-d)/2a
4.4 Print Real + i Imaginary
4.5 Print Real – i Imaginary
5. If d>0
5.1 Print “Roots are real and unequal”
5.2 r1 = (-b+sqrt(d))/2a
5.3 r2 = (-b-sqrt(d))/2a
5.4 Print r1,r2
Ex. No: 3 Programs using Looping statements
3.a Sum of N Numbers
Aim:
To Find the sum of first N natural numbers
Algorithm:
1. Read n
2. Sum = 0
3. i =1
4. Repeat while i<=n
sum = sum + i
i=i+1
5. Display the value of sum
3.b Factorial of a Given Number
Aim:
To Find the factorial of the given number( 5!=120)
Algorithm
1. Read n
2. factorial = 1
3. Repeat while i <=n
factorial =factorial * i
i=i+1
4. Display the value of factorial.
3.c Palindrome
Aim:
To Design an algorithm that accepts a positive integer and check whether the number is
palindrome or not
Algorithm:
1. Read n
2. no = n
3. rev = 0
4. Repeat while n > 0
rev = rev * 10 + n % 10
n = n / 10
5. If no = rev then print “Palindrome”, otherwise print “Not palindrome”.
3.d Implemetion of Sin(x)
Aim:
To find the summation of sine series.
Sin(x) = x –x3/3! + x5/5! - …
Algorithm:
1. Read the values of x (angle in degrees) and n (number of terms in series).
2. Convert the value of angle from degrees to radians. x = (x*3.14)/180
3. Initialize sum = 1, i = 1, sign = -1
4. Repeat while i <= n
Term = sign * xi / factorial(i)
sum = sum + Term
i=i+2
sign = sign * -1
5. Print the sum value.
Ex. No: 4 Programs using Functions
4.a Greatest Common Divisor(GCD)
Aim:
To find the GCD of the given two positive non-zero integers n and m.
Algorithm:
1. Read m and n
2. Repeat while n!=0
Temp = m % n
m=n
n = Temp
3. Print the current value of m as GCD
4.b Prime or not
Aim:
To write a function that checks whether the given number is prime or not.
Algorithm:
1. Read n
2. i = 2
3. Repeat while i<=n/2
if i is a factor of n
then
return false
otherwise
i=i+1
4. return true
Ex. No: 5 Programs using Arrays
5.a Max and Min Element in an Array.
Aim:
To find the maximum and minimum number in a set of n numbers.
Algorithm:
1. Read N and the Array Elements.
2. Set MAX=MIN=a[0], i=1
3. Repeat while i<n
If a[i]>MAX then, Assign a[i] into MAX variable.
If a[i]<MIN then, Assign a[i] into MIN variable.
i=i+1
4. Display the MIN and MAX Values.
5.b Sum of elements of an array.
Aim:
To find the sum of elements of an array.
Algorithm:
1. Read N and the Array Elements.
2. Set i=0 and Sum=0
3. Repeat while i<N
Sum=Sum+A[i]
i=i+1
4. Display the Sum.
5.c Sorting an Array
Aim:
Program to arrange the elements of an array in ascending order.
Algorithm:
1. Read N and the array Elements.
2. For i = 0 to n-2
For j = n-1 downto i+1
If (a[j]<a[j-1])
Swap a[j] and a[j-1]
3. Print the elements of the sorted array
5.d Matrix Multiplication
Aim:
To multiply two square matrices
Algorithm
1. Read n and the Elements of matrices A and B
2. For i = 0 to n-1
For j =0 to n-1
C[i][j]=0
For k = 0 to n-1
C[i][j]=C[i][j]+A[i][k]+B[k][j]
3. Print the elements of the matrix C
5.d Finding the type of Matrix
Aim:
To find the type of matrix
Algorithm
1. Read n and the Elements of matrices A
2. Check every element of the matrix and count number of zero(Z) and non zero elements
(NZ)
3. if (Z > NZ) then print Sparse Matrix
4. else Check whether the diagonal value is 1, then print unit matrix
5. else print Regular Square Matrix
Ex. No: 6 Programs using Recursion
6.a Factorial Using Recursion
Aim:
To find the factorial of the given number using recursion.
Algorithm:
1.Read the value of N.
2.Call Fact function to find the factorial value.
3.Display the result.
int Fact(int n)
{
if(n==0)
return 1;
return n*fact(n-1);
}
6.b Fibonacci Series Generation Using Recursion
Aim:
Generation of Fibonacci series (0 1 1 2 3 5 8 13 ……..) using recursion.
Algorithm:
1.Read the value of n.
2. For(i=0;i<n;i++)
.Call Fibo function to find ith term and store the returned value.
3.Display the result.
int Fibo(int n)
{
If(n==0 || n==1)
return n
return Fibo(n-1)+Fibo(n-2).
}
6.c GCD of Two numbers using recursive function
Aim:
To Compute GCD of two numbers using recursive function
Algorithm:
1.Read Two Positive integers a and b.
2.Call GCD function to find GCD of two numbers.
3.Display the result.
int GCD(int a,int b)
{
If(b==0)
return a;
return GCD(b,a%b)
}
Ex. No: 7 Programs using strings
7.a Palindrome or Not
Aim:
To check whether the given string is palindrome or not.
Algorithm:
1.Read the string
2. Set flag value to 0.
3. Find the length of the given string
4. Repeat the steps while (I<=Length/2)
{
If(s[I]==s[length])
{
I++;
Length--;
}
Else
{
Flag=1;
Break;
}
}
5.Display the appropriate message.
7.b Reverse String
Aim:
To reverse the given string.
Algorithm:
1. Read the string.
2. Find the length of the string.
3. Repeat the steps while(s1[I]!=’\0’)
{
Assign S1[length-1] to S2[I].
Increment I and Decrement length.
}
4.Display the Reversed String.
7.c No of Occurrence
Aim:
Design and implement an algorithm that will count the number of times a particular word occurs
in a given text.
Algorithm:
1. Read the text, search Word and find length of the search word.
2. Initialize match count set preceding character and set pointer for word array i=1.
3. While not at end of text ( ‘\0 ‘) do
1. While not end of line do
1. Read next character
2. If current text character chr matches ith character in the search word
1. Extend partial match i by 1
2. If a word pattern match then
1. Read next character into post
2. If preceding and following character not alphabetic then
1. Update match count
3. Reinitialize pointer to word array i
4. Save following character post as preceding character
3. Else save current text character as preceding character for match
4. Reset word array pointer I to first position
2. Read past end of line
4. Return word match count
7.d Number of Characters, words and Lines
Aim:
To Find the count of characters words and lines
Algorithm:
1. Read the string.
2. Repeat the following steps until while(s[I]!=’\0’)
{
if(s[I]==’\n’)
Line++;
if(s[I]==’\n’ || s[I]==’ ‘)
W++;
C++;
I++;
}
3.Display the result.
Ex. No: 8.a) Polynomial Addition and Subtraction using Structure
Aim:
To find the addition and subtraction of two polynomials.
Algorithm:
1. Create a structure that contains four integer members such as a,b,c and d.
2. Calculate addition and subtraction.
3. Display the result.
Ex. No: 8.b) Student Details using Structure
Aim:
To maintain student details using structure.
Algorithm:
1. Create a student structure with reg.no, name, DOB and fees.
2. Read Student details using structure variable.
3. Display the result.
Ex. No: 8.c) Student Details using Array of Structure
Aim:
To maintain N student details using array of structure.
Algorithm:
1. Create a structure contains student name, regno, DOB and marks for 5 subjects. Use
structure within structure for DOB.
2. Create array of structure for n students.
3. Calculate Total and average for each student.
4. Display the result.
Ex. No: 9.a) Creation of Two different files from Master file
Aim:
Creation of two different files from a master file
Algorithm:
1. Get a set of numbers from the user and store in a master file.
2. Read the master file and store the odd and even numbers in two different files.
3. Display the result
Ex. No: 9.b) Employee details using File
Aim:
To maintain Employee information using files.
Algorithm:
1. Create an employee structure with employee number, salary and age.
2. Create a file that contains information about employees of an organization.
3. Write a function to print the employee numbers for those who are getting salary less than
Rs.4500 and aged more than 35.
Ex.No 10. Programs for Multiple Files
Aim:
To sort array of numbers using Multiple files.
Algorithm:
1. Write a program which has a function to get array of elements (file A)
2. Write a program which has a function to sort the given elements (file B)
3. Write a main program which will call the function to get array of elements
(fileA) as well as the function to sort the elements (file B) .
4. Display the result.
Ex.No.11.a) Duplicates removal in a sorted array using pointer
Aim:
Write a C program for removal of duplicates from an ordered array (use pointers to access the
array).
Algorithm:
1. Establish the array a[1..n] of n elements
2. Set loop index i to 2 to allow correct termination.
3. Compare successive pairs of elements until a duplicate is encountered then Set unique
element count j.
4. While all pairs have not been examined do
(a) if next pair not duplicate then
(a.1) add one to unique element count j.
(a.2) move later element of pair to array position determined by the unique element
count j.
Ex.No.11.b) Prime number using Function pointer
Aim:
Write a program to find whether the given number is prime or not. Write a function for
checking the number, use function pointers to call the function.
Algorithm:
1. Create a function pointer int *fp (int)
2. Create a function isprime(int n)
3. Call fp(x) to check x is a prime or not.
Ex.No 12. Matrix Multiplication with dynamic memory allocation
Aim:
To find multiplication of two matrices using dynamic memory allocation.
Algorithm
1. Read order of input matrices
2. Allocate memory using pointers to each row and column of all input and output
matrices.
3. Get elements of input matrices.
4. Multiply input matrices and store it in output matrix
5. display all matrices.