Programming in C Laboratory - CS3271 - Lab Manual
Programming in C Laboratory - CS3271 - Lab Manual
I Year/II Semester
LAB MANUAL
Prepared By,
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 1 of 64
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
LIST OF EXPERIMENTS:
Note: The lab instructor is expected to design problems based on the topics listed. The
Examination shall not be restricted to the sample experiments designed.
1. I/O statements, operators, expressions
2. Decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal
5. Strings: operations
6. Functions: call, return, passing parameters by (value, reference), passing arrays to function.
7. Recursion
8. Pointers: Pointers to functions, Arrays,Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10. Files: reading and writing, File pointers, file operations, random access, processor directives.
TOTAL: 60 PERIODS
COURSE OUTCOMES:
Upon completion of the course, the students will be able to
CO1: Demonstrate knowledge on C programming constructs.
CO2: Develop programs in C using basic constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers, functions.
CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.
TEXT BOOKS:
1. ReemaThareja, “Programming in C”, Oxford University Press, Second Edition, 2016.
2. Kernighan, B.W and Ritchie,D.M, “The C Programming language”, Second Edition, Pearson
Education, 2015.
REFERENCES:
1. Paul Deitel and Harvey Deitel, “C How to Program with an Introduction to C++”, Eighth edition,
Pearson Education, 2018.
2. Yashwant Kanetkar, Let us C, 17th Edition, BPB Publications, 2020.
3. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of Programming with C", McGraw-
Hill Education, 1996.
4. Pradip Dey, Manas Ghosh, “Computer Fundamentals and Programming in C”, Second
5. Edition, Oxford University Press, 2013.
6. Anita Goel and Ajay Mittal, “Computer Fundamentals and Programming in C”, 1st Edition, Pearson
Education, 2013.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 2 of 64
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
Ex. No. 1
I/O STATEMENTS AND EXPRESSIONS
PROBLEM STATEMENT:
Programs using I/O statements and expressions.
AIM:
To write C programs to demonstrate the uses of various formatted and unformatted
ALGORITHM:
STEP 1:Start the program.
STEP 3:Get input values from the user for arithmetic operation.
STEP 4:Use input function scanf() and output function printf()to display the output after
the calculation.
PROGRAM:
#include<stdio.h>
voidmain()
{
intage, bir_year, cur_year;clrscr();
printf("Enter the year of birth:\n");
scanf("%d",&bir_year);
printf("Enter the current year:\n");
scanf("%d",&cur_year);
age=cur_year-bir_year; printf("The age is:%d
years",age);getch();
}
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 3 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
TEST CASES:
RESULT:
Thus the C program to perform input and output operations using built-in printf()
and scanf() function has been done and verified successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 4 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Ex. No. 2
DECISION-MAKING CONSTRUCTS
PROBLEM STATEMENT:
Programs using decision-making constructs.
AIM:
To write a C program to check whether the given number is positive number or
negative number
ALGORITHM:
STEP 1:Start the program.
STEP 4:Check the input value whether it is a positive number or negative number.
STEP 5:If the number is less than ZERO, then print the result as “NEGATIVE”.
PROGRAM:
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 5 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
#include<stdio.h> void
main()
{
int n; clrscr();
printf("~~~~~~~~~~~~~~~~\n");
printf("POSITIVE OR NEGATIVE\n");
printf("~~~~~~~~~~~~~~~~\n");
printf("Enter the number\n");
scanf("%d", &n);
if(n>0)
printf("The given number %d is positive\n",n);else if(n<0)
printf("The given number %d is negative",n);else
TEST CASES:
RESULT:
Thus the C program to check for positive and negative numbers using decision
making construct has been completed and verified successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 6 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Ex. No. 3
FIND WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT
PROBLEM STATEMENT:
Write a program to find whether the given year is leap year or Not? (Hint: not every
centurion year is a leap. For example 1700, 1800 and 1900 is not a leap year)
AIM:
T write a C program to check whether the given year is a LEAP year or not.
ALGORITHM:
STEP 1:Start the program.
STEP 3:Read the input from the user and store it in a variable.
STEP 5:Print the result as “It is a leap year” and goto step 7.
PROGRAM:
#include<stdio.h> void
main()
{
int n; clrscr();
printf("********************\n"); printf("Leap
year or not\n"); printf("********************\n");
printf("Enter the year\n"); scanf("%d",&n);
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 7 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
if(n%100==0)
n%400==0 ? printf("The given year %d is a leap year",n): printf("The given year %d
is a non-leap year",n);
else if(n%4==0)
printf("The given year %d is a leap year",n);else
printf("The given year %d is non leap year",n);getch();
}
TEST CASES:
RESULT:
Thus the program to check for leap year has been written in C successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 8 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Ex. No. 4
MENU DRIVEN CALCULATOR
PROBLEM STATEMENT:
Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.
AIM:
To write a C program to design a menu-based calculator to perform various basic
arithmetic operations like addition, subtraction, multiplication, division and modulo.
ALGORITHM:
STEP 1:Start the program.
STEP 3:Get two inputs from the user using scanf() function and store them in “a” and “b”
respectively.
STEP 4:Get the user option and based on the user options, perform the corresponding
arithmetic operations on the user data.
STEP 5:Store the result in a variable called “c” and display the value of “c” using printf().
PROGRAM:
#include<stdio.h> void
main()
{
int ch,a,b,c; clrscr();
printf("**********************\n"); printf("Menu
driven program\n");
printf("**********************\n"); printf("Enter
the choice\n");
printf(" 1) Addition\n 2) Subraction\n 3) Multiplication\n
4) Division\n
5) Square\n");
scanf("%d",&ch);
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 9 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
switch(ch)
{
case 1:
printf("Enter two numbers to be added:");scanf("%d
%d",&a,&b);
c=a+b;
printf("The sum of two numbers %d and %d is:%d",a,b,c);break;
case 2:
printf("Enter two numbers to be subracted:");scanf("%d
%d",&a,&b);
c=a-b;
printf("The difference of two numbers %d and
%dis:%d",a,b,c);
break;
case 3:
printf("Enter two numbers to be multiplied:");scanf("%d
%d",&a,&b);
c=a*b;
printf("The product of two numbers %d and %d is:%d",a,b,c);break;
case 4:
printf("Enter two numbers to be divided:");scanf("%d
%d",&a,&b);
c=a/b;
printf("The division of two numbers %d and %d is:%d",a,b,c);break;
case 5:
printf("Enter a number to be squared:");scanf("%d",&a);
c=a*a;
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 10 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
default: printf("Invalid
option");
}
getch();
}
TEST CASES:
RESULT:
Thus the menu-drive arithmetic calculator has been implemented in C and verified
successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 11 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
AIM:
To write a C program to check whether the given number is an Armstrong number
or Not.
ALGORITHM:
STEP 1:Start the program.
STEP 2:Declare all required variables and initialize the result variable “sum” to 0.
STEP 3:Get an input from the user and store it in “n” and “n_copy” variables.
STEP 4:Using the user input, perform the following operation until it becomes ZERO
r = n%10;
sum = sum + (r*r*r);
n = n/10;
STEP 5:compare the value of “sum” and n_copy”. If they are equal then go to step no 6
otherwise go to step no 7.
PROGRAM:
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 12 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
#include <stdio.h>void
main()
{
int n, sum=0;int r,
n_copy;
printf("Enter a four digit number\t");scanf("%d",
&n);
n_copy = n;
while(n>0)
{
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 13 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
r = n%10;
sum = sum + (r*r*r);n =
n/10;
}
if(sum == n_copy)
printf("%d is an armstrong number", n_copy);else
printf("%d is not an armstrong number", n_copy);getch();
}
TEST CASES:
RESULT:
Thus the program to check for an Armstrong number has been written in C and the
output was verified successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 14 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Ex. No. 6
SORT THE NUMBERS BASED ON THE WEIGHT
PROBLEM STATEMENT:
Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based on the
following conditions.
5 if it is a perfect cube.
4 if it is a multiple of 4 and divisible by 6.
3 if it is a prime number.
Sort the numbers based on the weight in the increasing order as shown below
AIM:
ALGORITHM:
STEP 1:Start the program.
STEP 2:Declare all required global and local variables and initialize them either inside or
outside the functions.
int getweight(int n) => to assign weight for each input value based the
given condition.
STEP 4:Sort the numbers and their weights in ascending order based on weight.
PROGRAM:
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 15 of 64
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
#include<stdio.h>
#include<math.h> int
prime(int n)
{
int flag = 1;
for(int i=2; i<(n/2); i++)
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 16 of 64
www.BrainKart.com
{
if(n%i == 0)
flag = 0;
}
if(flag)
return 1;
else
return 0;
}
int cube(int num)
{
int i, flag=0;
for(i=2;i<=num/2;i++)
{
if((i*i*i)==num)
{
flag=1;
break;
}
return flag;
}
int getweight(int n)
{
int w=0;
if(cube(n))
w+=5;
if(n%4==0 && n%6==0)w+=4;
if(prime(n))
w+=3;
return w;
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 17 of 64
www.BrainKart.com
void main()
{
int nums[15], ws[15];int
i,j,t,n;
clrscr();
printf("Enter the number of numbers");scanf("%d",&n);
printf("\nEnter the numbers");for(i=0;i<n;i++)
scanf("%d",&nums[i]);
for(i=0;i<n;i++)
ws[i]=getWeight(nums[i]);
printf("\nBefore sorting:\n");for(i=0;i<n;i++)
printf("<%d,%d>\t",nums[i],ws[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(ws[j]>ws[j+1])
{
t=ws[j+1];
ws[j+1]=ws[j]; ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
}
printf("\nSorted:\n");
for(i=0;i<n;i++)
printf("<%d,%d>\t",nums[i],ws[i]);
getch();
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 18 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the C program to calculate weight for a set numbers based on a series of
conditions and sort them based on the assigned weights has been implemented and the result
was tested.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 19 of 64
www.BrainKart.com
Ex. No. 7
AVERAGE HEIGHTS OF PERSONS
PROBLEM STATEMENT:
Populate an array with height of persons and find how many persons are above the
average height.
AIM:
To write a C program to populate a 1D array with height of persons and find how
many persons are above the average height value.
ALGORITHM:
STEP 1:Start the program.
STEP 2:Include all required header files and declare all required variables.
intaboveavg(…) => to find no. of persons are above the average height
STEP 4:Get the number of persons value from the user and store it in “n”.
STEP 5:Get the height value of “n” persons from the user and store it in an array.
PROGRAM:
#include<stdio.h>
int aboveavg(int a[],int n,int avg);int
avgheight(int a[],int n);
void main()
{
int a[5],avg,count,n,i=0; clrscr();
printf("**********************\n");
printf("Height of the person\n");
printf("**********************\n");
printf("Enter the no of persons\n");
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 20 of 64
www.BrainKart.com
scanf("%d",&n);
printf("Enter the height of the %d persons in cm\n",n);for(i=0;i<n;i++)
scanf("%d",&a[i]);
avg=avgheight(a,n);
printf("The average height of %d persons is:%d\n",n,avg);
count=aboveavg(a,n,avg);
printf("The no of persons above the avg height,%d
are:%d\n",avg,count);
getch();
}
int aboveavg(int a[],int n,int avg)
{
int i=0,c=0;
for(i=0;i<n;i++)
{
if(a[i]>avg)c++;
}
return c;
}
int avgheight(int a[],int n)
{
int total=0,i=0,mean;
for(i=0;i<n;i++)
{
total=total+a[i];
}
printf("The sum of the heights are:%d\n",total);mean=total/n;
return mean;
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 21 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the program to count how many persons are above the average height value
has been implemented in C and verified.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 22 of 64
www.BrainKart.com
Ex. No. 8
BODY MASS INDEX OF THE INDIVIDUALS
PROBLEM STATEMENT:
Populate a two dimensional array with height and weight of persons and compute
the Body Mass Index of the individuals.
AIM:
To write a C program to compute the Body Mass Index (BMI) value for the given
number of persons with their height and weight values.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include all required header files and declare all necessary variables.
STEP 4: Get 'n' persons Height and Weight values from the user.
STEP 5: Convert the height value from centimetres to meters before calculating the BMI
value.
STEP 7: Display the BMI value and BMI remarks based on the BMI value.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float h, height[10],weight[10],bmi[10];int
i=0,k=0,j=0,n; clrscr();
printf("********************\n");
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 23 of 64
www.BrainKart.com
printf("BMI CALCULATION\n");
printf("********************\n"); printf("Enter the number
of individuals:\n");scanf("%d",&n);
printf("\nEnter the height & weight of individuals:\n");for(i=0;i<n;i++)
{
printf("\nIndividual %d :\n",i+1);printf("Height
(in cm): ");
scanf("%f",&h); height[i] =
h/100;
printf("Weight (in kg): ");
scanf("%f",&weight[i]);
}
for(i=0;i<n;i++)
{
bmi[i]=weight[i]/(height[i] * height[i]);
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 24 of 64
www.BrainKart.com
printf("Overweight");
else if(bmi[i]>=18.5)
printf("Normal");
else if(bmi[i]>=16)
printf("Underweight");
else
printf("Severely Underweight");
}
getch();
}
TEST CASES:
RESULT:
Thus the C program to calculate Body Mass Index (BMI) has been implemented
and tested sucessfully.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 25 of 64
www.BrainKart.com
input: a@gh%;j
output: j@hg%;a
AIM:
To write a C program to reverse the given string without changing the position of
special characters.
ALGORITHM:
STEP 1:Start the program.
STEP 2: Include all required header files and Declare all required variables.
STEP 3: Define an user-defined function isAlphabet(), to check whether the given symbol
is an alphabet or not. If it is an alphabet, then return TRUE otherwise return
FALSE>
STEP 4: Define an another user-defined function reverse(), to retrieve the characters from
the input string one-by-one and reverse them based on the result of above
mentioned function.
STEP 5: Get a String input from the user and pass it to reverse() function.
PROGRAM:
{
char *t;t
= a;a =
b;b = t;
}
void reverse(char str[])
{
int r = strlen(str) - 1, l = 0;while (l < r)
{
if (!isAlphabet(str[l]))l++;
else if(!isAlphabet(str[r]))r--;
else
{
char temp; temp =
str[l];
str[l] = str[r]; str[r] = temp;
swap(&str[l], &str[r]);l++;
r--;
}
}
}
void main()
{
char str[15]; printf("Input
string: ");scanf("%s", str);
reverse(str);
printf("Output string: %s", str);;getch();
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 27 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the program for reversing a string without changing the special characters'
position has been implemented and tested.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 28 of 64
www.BrainKart.com
Ex. No. 10
NUMBER SYSTEM CONVERSION
PROBLEM STATEMENT:
Convert the given decimal number into binary, octal and hexadecimal numbers
using user defined functions.
AIM:
To write a C program to convert the given number from one number system to
another number system.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include all necessary header files and declare all required variables.
STEP 4: Define all user-defined functions for corresponding number system conversion.
STEP 5: Call the user-defined functions and pass the input value to it.
PROGRAM:
#include<stdio.h>
void main()
{
int n;
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 29 of 64
www.BrainKart.com
clrscr();
printf("******************************\n");
printf("Number system - Conversion\n");
printf("******************************\n");
printf("Enter the number:\n"); scanf("%d",&n);
bin(n);
hex(n);
oct(n);
getch();
}
void bin(int n)
{
int B[50],i=0,len;
printf("The binary equivalent of given number %d is:\n",n);while(n>0)
{
B[i]=n%2;
n=n/2;
i++;
}
len=i; for(i=0;i<len;i++)
{
printf("%d",B[len-i-1]);
}
}
void hex(int n)
{
int i=0,len,rem;char
H[50];
printf("\nThe hexadecimal equivalent of given number %dis:\n",n);
while(n>0)
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 30 of 64
www.BrainKart.com
{
rem=n%16;
if(rem>9)
{
H[i]=55+rem;
}
else
{
H[i]=48+rem;
}
n=n/16;
i++;
}
len=i; for(i=0;i<len;i++)
printf("%c",H[len-1-i]);
}
void oct(int n)
{
int O[50],i=0,len;
printf("\nTheocatal equivalent of the given number %dis:\n",n);
while(n>0)
{
O[i]=n%8;
n=n/8;
i++;
}
len=i; for(i=0;i<len;i++)
{
printf("%d",O[len-1-i]);
}
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 31 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the C program to convert the given number from one number system to
another number system has been implemented and tested successfully.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 32 of 64
www.BrainKart.com
Ex. No. 11
STRING OPERATIONS USING BUILT-IN FUNCTIONS
PROBLEM STATEMENT:
From a given paragraph perform the following using built-in functions:
AIM:
To write a C program to perform various string operations using built-in functions
ALGORITHM:
STEP 1: Start the program.
STEP 3: Display the menu list and get the user's option and based on the input, perform
the string operations.
STEP 6: Count the number of words in the given sentence and display the result.
STEP 7: Take the characters in the input sentence and capitalize the starting character of
each sentence and display the result.
STEP 8: Find the occurrences of the given string in the sentence and replace them with
new string value.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 33 of 64
www.BrainKart.com
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char s1[30],s2[30];
printf("\nString Operations using built-in functions\n");printf("Enter the string : ");
gets(s1);
word_count(s1);
capitalize(s1);
printf("Enter the substring\n");fflush(stdin);
gets(s2);
printf("Enter the string to replace the substring\n");fflush(stdin);
gets(s3);
replace(s1,s2,s3);
getch();
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 34 of 64
www.BrainKart.com
{
wc++;
}
i++;
}
printf("Total number of words in the string is : %d\n", wc);
}
void capitalize(char s[])
{
int i=0; char
new[20];
new[i] = toupper(s[0]);i++;
while(s[i] != '\0')
{
if(s[i]==' ' || s[i]=='\n' || s[i]=='\t')
{
new[i] = s[i];
new[i+1] = toupper(s[i+1]);i+=2;
}
else
{
new[i] = s[i];i++;
}
}
printf("After capitalizing: %s", new);
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 35 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the program to perform string operations has been implemented in C using
built-in functions.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 36 of 64
www.BrainKart.com
Ex. No. 12
TOWER OF HANOI USING RECURSION
PROBLEM STATEMENT:
. Solve towers of Hanoi using recursion.
AIM:
To write a C program to implement the Tower of Hanoi problem using recursion
technique.
ALGORITHM:
STEP 1: Start the program.
STEP 3: Declare a user-defined function to swap the disk from one poll to another in
Tower of Hanoi problem.
STEP 4: Read input values from the user and pass it to the user-defined function
STEP 5: Check the condition for disk movement from one poll to another and transfer the
disk when the condition is satisfied.
STEP 6: Repeat the above step using recursion until all disks are moved from source poll
to destination poll.
PROGRAM:
#include <stdio.h>
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 37 of 64
www.BrainKart.com
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoiare :\n");
towers(num, 'A', 'C', 'B');getch();
}
TEST CASES:
RESULT:
Thus the program for Tower of Hanoi problem has been implemented in C using
recursion technique.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 38 of 64
www.BrainKart.com
AIM:
To write a C program to sort the list of numbers using function in pass-by-reference
method.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare variables and one dimensional array for storing a list of numbers.
STEP 4: Read inputs from the user and store it in the array in main() function.
STEP 5: Call the sorting function by pass the input array elements as reference.
STEP 6: Display the list of elements of an array after the sorting operation.
PROGRAM:
#include<stdio.h> void
main()
{
int A[50],n,i=0; clrscr(); printf("***************************\n");
printf("Sorting an array elements\n");
printf("***************************\n"); printf("Enter the no of
elements in the array\n");scanf("%d",&n);
printf("Enter the element in the array:\n");for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("The sorted elements in the array are:\n");sort(A,n);
getch();
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 39 of 64
www.BrainKart.com
TEST CASES:
RESULT:
Thus the program for sorting numbers in ascending order using function in pass-by-
reference method has been written and the output was verified.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 40 of 64
www.BrainKart.com
Ex. No. 14
EMPLOYEES SALARY SLIP GENERATION
PROBLEM STATEMENT:
Generate salary slip of employees using structures and pointers.
AIM:
To write a C program to generate salary slip of employees using structures.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include all necessary header files and declare all required variables.
STEP 3: Read the number of employees value from the user and store it in "n".
STEP 4: Read basic, allowance and deductions from each employee to calculate the net
salary amount.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
struct emp
{
int empno ; char
name[10];
int bpay, allow, ded, npay ;struct emp
*next;
};
void main()
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 41 of 64
www.BrainKart.com
{
int i,n=0; char
answer;
int more_data = 1;
struct emp *current_ptr, *head_ptr;
clrscr() ;
head_ptr = (struct emp *)malloc (sizeof(struct emp));current_ptr = head_ptr;
while (more_data)
{
printf("\nEnter the employee number : ") ;scanf("%d", &
current_ptr->empno) ; printf("\nEnter the name : ") ;
scanf("%s",& current_ptr->name) ;
printf("\nEnter the basic pay, allowances & deductions : ")
;
scanf("%d %d %d", & current_ptr ->bpay, & current_ptr -
>allow,
& current_ptr ->ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ; n++;printf("Would you like to
add another employee? (y/n): ");scanf("%s", answer);
if (answer!= 'Y')
{
current_ptr->next = (struct eme *) NULL; more_data = 0;
}
else
{
current_ptr->next = (struct emp *) malloc
(sizeof(struct emp));
current_ptr = current_ptr->next;
}
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay
\n\n") ;
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 42 of 64
www.BrainKart.com
current_ptr=head_ptr;
for(i=0;i<n;i++)
current_ptr=current_ptr->next;
getch();
TEST CASES:
RESULT:
Thus the Employee's salary slip generation program has been implemented using
structures and verified.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 43 of 64
www.BrainKart.com
Ex. No. 15
INTERNAL MARKS OF STUDENTS
PROBLEM STATEMENT:
Compute internal marks of students for 5 subjects using structures and functions.
AIM:
To write a C program to compute internal marks of students for 5 subjects using
ALGORITHM:
STEP 1: Start the program.
PROGRAM:
#include<stdio.h>
#include<conio.h> struct
stud
{
char name[20]; long
int rollno;int
marks[5,3];int i[5];
}students[10];
void calcinternal(int);int main()
{
int a, b, j, n, total;clrscr();
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 44 of 64
www.BrainKart.com
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 45 of 64
www.BrainKart.com
}
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total; for(a=1;a<=n;++a)
{
for(b=0;b<5;b++)
{
total=0; for(j=0;j<=2;++j)
{
total += students[a].marks[b][j];
}
students[a].i[b]=total/3;
}}
}
TEST CASES:
RESULT:
Thus the program for computing the internal marks of students has been written in
C using structures and functions and the output was tested.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 46 of 64
www.BrainKart.com
Ex. No. 16
CREATION OF TELEPHONE DIRECTORY
PROBLEM STATEMENT:
Insert, update, delete and append telephone details of an individual or a company
into a telephone directory using random access file.
AIM:
To write a C program to implement the telephone directory application to update,
delete and append telephone details of an individual or a company using file concept.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare all required variables, file pointers and phone book structure.
STEP 3: Display the menu options and get an input option from the user.
STEP 4: Based on the user input, call the corresponding function to add, delete, search to
display the contact details.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 48 of 64
www.BrainKart.com
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");exit(1);
}
}while (iSelection <= 4);
}
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");if ( pWrite ==
NULL )
{
perror("The following error occurred ");exit(EXIT_FAILURE);
}
else
{
counter++; realloc(phonebook,
sizeof(phone));printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName); printf("Last
Name: ");
scanf("%s", phonebook[counter-1].LastName); printf("Phone
Number (XXX-XXX-XXXX): "); scanf("%s", phonebook[counter-
1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-
1].FirstName,
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 49 of 64
www.BrainKart.com
phonebook[counter-1].LastName, phonebook[counter-
1].PhoneNumber);
fclose(pWrite);
}
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 50 of 64
www.BrainKart.com
}
printf("That contact was not found, please try again.");
}
}
}
fclose(pRead);
}
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 51 of 64
www.BrainKart.com
RESULT:
Thus the implementation of telephone directory system has been implemented and
verified successfully.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 52 of 64
www.BrainKart.com
Ex. No. 17
IMPLEMENTATION OF BANKING APPLICATION
PROBLEM STATEMENT:
Count the number of account holders whose balance is less than the minimum
balance using sequential access file.
AIM:
To write a C program to implement the banking system.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include necessary header files and declare all required variables.
STEP 3: Display the list of options to the user as a menu and get the user input.
STEP 4: Based the user input, validate the values given by the user and do the calculation.
STEP 6: Repeat the steps from 3 to 5 until user exits from the program.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct Bank_Account
{
char no[10]; char
name[20]; char
balance[15];
};
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 53 of 64
www.BrainKart.com
void main()
{
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 54 of 64
www.BrainKart.com
fwrite(&acc,sizeof(acc),1,fp);fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");else
{
printf("\nA/c Number\tA/c Holder
Name
Balance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)printf("%-
10s\t\t%-
20s\t%s\n",acc.no,acc.name,acc.balance); fclose(fp);
}
break;
case '3' :
fflush(stdin);
flag=0; fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number :");gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 forwithdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is :
%s",acc.balance);
printf("\nEnter the Amount to transact : ");fflush(stdin);
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 55 of 64
www.BrainKart.com
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else
{
bal = atof(acc.balance) - atof(amt);if(bal<0)
{
printf("\nRs.%s Not available in your
A/c\n",amt);
flag=2;
break;
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 56 of 64
www.BrainKart.com
}
}
flag++;
break;
}
}
if(flag==1)
{ pos2=ftell(fp);pos =
pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");fclose(fp);
break;
case '4' :
fp=fopen("acc.dat","r");flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal=atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe number of account holder whose balance less than the
minimum balance:%d",flag);
fclose(fp);
break;
case '5':
fclose(fp);
exit(0);
}
printf("\n Press any key to continue");
getch();
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 57 of 64
www.BrainKart.com
}
while(choice!='5');
}
TEST CASES:
RESULT:
Thus the implementation of Banking System has been implemented and verified
successfully.
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 58 of 64
www.BrainKart.com
Ex. No. 18
IMPLEMENTAION OF RAILWAY RESERVATION SYSTEM
PROBLEM STATEMENT:
Create a Railway reservation system with the following modules:
Booking
Availability checking
Cancellation
Prepare chart
AIM:
To develop an application in C for railway ticket reservation system.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Include necessary header files and declare all required variables.
i. Booking
ii. Availability checking
iii. Cancellation
iv. Prepare chart
STEP 4: Get an input from the user.
STEP 5: Based the user input, validate the values given by the user and do the calculation.
STEP 7: Repeat the steps from 3 to 5 until user exits from the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int first=5,second=5,third=5;struct
node
{
int ticketno;
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 59 of 64
www.BrainKart.com
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
void availability()
{
int c;
printf("availability cheking");
printf("\n1.first class\n2.second class\n3.third
class\n");
printf("enter the option");scanf("%d",&c);
switch(c)
{
case 1:
if(first>0)
{
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 60 of 64
www.BrainKart.com
printf("seat available\n");first--;
}
else
printf("seat not available");break; case
2:
if(second>0)
{
printf("seat available\n");second--;
}
else
printf("seat not available");break; case
3:
if(third>0)
{
printf("seat available\n");third--;
}
else
printf("seat not available");break;
default:
break;
}
}
void cancel()
{
int c; printf("cancel\n");
printf("which class you want to cancel");
printf("\n1.first class\n2.second class\n3.third
class\n");
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 61 of 64
www.BrainKart.com
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 62 of 64
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
cheking\n3.cancel\n4.Chart \n
switch(n)
case 1:
booking();
break;
case 2:
availability();break;
case 3:
cancel();
break;
case 4:
chart();
break;
case 5:
exit(0);
default:
break;
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 63 of 64
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
getch();
TEST CASES:
RESULT:
Thus the implementation of Railway ticket reservation system
has beenimplemented and verified successfully.
CS3251_PIC
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
Page 64 of 64
All 2nd Semester Subjects
Professional English - II - HS3252 Engineering Graphics - GE3251
Statistics and Numerical Methods - Physics for Electronics Engineering -
MA3251 PH3254
Physics for Electrical Engineering - Physics for Civil Engineering - PH3201
PH3202
Materials Science - PH3251 Basic Electrical and Electronics
Engineering - BE3251
Physics for Information Science - Basic Civil and Mechanical Engineering -
PH3256 BE3255
Basic Electrical and Instrumentation Electric Circuit Analysis (Circuit
Engineering - BE3254 Theory) - EE3251
Programming in C - CS3251 Circuit Analysis - EC3251
Data Structures Design - AD3251