0% found this document useful (0 votes)
31 views62 pages

Cs3271 C Lab Manual Ucetk

The document is a laboratory manual for the CS3271 C Programming Laboratory course at the University College of Engineering, Thirukkuvalai, under Anna University. It outlines the course objectives, a list of experiments, and expected outcomes for students, focusing on various C programming constructs and applications. Additionally, it includes sample source codes for specific programming tasks and exercises to reinforce learning.

Uploaded by

manikadan5543
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)
31 views62 pages

Cs3271 C Lab Manual Ucetk

The document is a laboratory manual for the CS3271 C Programming Laboratory course at the University College of Engineering, Thirukkuvalai, under Anna University. It outlines the course objectives, a list of experiments, and expected outcomes for students, focusing on various C programming constructs and applications. Additionally, it includes sample source codes for specific programming tasks and exercises to reinforce learning.

Uploaded by

manikadan5543
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/ 62

lOMoARcPSD|45840416

Cs3271 c lab manual ucetk

computer science (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Gokul Gokul ([email protected])
lOMoARcPSD|45840416

UNIVERSITY COLLEGE OF ENGINEERING, THIRUKKUVALAI


(A Constituent College of Anna University :: Chennai)
THIRUKKUVALAI, NAGAPATTINAM-610204

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS3271- C PROGRAMMING LABORATORY

RECORD NOTE BOOK

NAME :

REGISTER NO:

BRANCH :

YEAR/SEM : /

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

UNIVERSITY COLLEGE OF ENGINEERING, THIRUKKUVALAI


(A Constituent College of Anna University :: Chennai)
THIRUKKUVALAI, NAGAPATTINAM-610204

BONAFIDE CERTIFICATE

Register Number

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Certified that this is a bonafide record of practical work


done by of Computer Science and Engineering in the
CS3271- PROGRAMMING IN C laboratory during the academic year 2021-2022 .

STAFF INCHARGE HEAD OF THE DEPARTMENT


[Dr. S. SRIDHARAN] [Dr. K. L. NEELA]
AP/CSE AP/CSE

Submitted for practical Examination held on


in the CS3271- PROGRAMMING IN C Laboratory of the Department of Computer Science

and Engineering University College of Engineering, Thirukkuvalai Campus – 610 204.

INTERNAL EXAMINER EXTERNAL EXAMINER

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

(REGULATION 2021)
AS PER ANNA UNIVERSITY SYLLABUS
SYLLABUS
CS3271 PROGRAMMING IN C LABORATORY LTPC
0042
COURSE OBJECTIVES:
 To familiarise with C programming constructs.
 To develop programs in C using basic constructs.
 To develop programs in C using arrays.
 To develop applications in C using strings, pointers, functions.
 To develop applications in C using structures.
 To develop applications in C using file processing.

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.

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

TABLE OF CONTENT
LIST OF EXPERIMENTS

EXP DATE PAGE SIGNATURE


NAME OF THE EXPERIMENT NO
NO

1 Programs using I/O statements and expressions.

2 Programs using decision-making constructs.

Write a program to find whether the given year is leap


3 year or Not? (Hint: not every centurion year is a leap. For
example 1700, 1800 and 1900 is not a leap year).
Design a calculator to perform the operations, namely,
4 addition, subtraction, multiplication, division and square of a
number.
Check whether a given number is Armstrong number or
5
not?
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
6
it is a prime number. Sort the numbers based on the weight
in the increasing order as shown below <10,its
weight>,<36,its weight><89,its weight>.
Populate an array with height of persons and find how
7
many persons are above the average height.
Populate a two dimensional array with height and weight of
8 persons and compute the Body Mass Index of the individuals.

Given a string ―a$bcd./fg‖ find its reverse without changing


9 the position of special characters.
(Example input:a@gh%;j and output:j@hg%;a)
Convert the given decimal number into binary, octal and
10
hexadecimal numbers using user defined functions.
Using built-in functions find the total number of words in a
11(a)
paragraph.
Using built-in functions capitalize the first word of each
11(b)
sentence in a paragraph.

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Using built-in functions replace a given word with another


11(c)
word in a paragraph.

12 Solve towers of Hanoi using recursion.

13 Sort the list of numbers using pass by reference.

Generate salary slip of employees using structures and


14
pointers.
Compute internal marks of students for five different
15
subjects using structures and functions.
Insert, update, delete and append telephone details of an
16 individual or a company into a telephone directory using
random access file.
Count the number of account holders whose balance is less
17
than the minimum balance using sequential access file.
18 Mini project

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Programs using I/O statements and expressions

SOURCE CODE:

#include <stdio.h>
void main()
{
int a, b, c,d,e;
float f;
clrscr();
printf("Input two integers\n");
scanf("%d%d", &a, &b);
c = a + b; //addition
d=a-b; //subtraction
e=a*b; //multiplication
f=a/b; // division
printf("(%d) +(%d) = (%d)\n", a, b, c);
printf("(%d) -(%d) = (%d)\n", a, b, d);
printf("(%d) *(%d) = (%d)\n", a, b, e);
printf("(%d) + (%d) = (%f)\n", a, b, f);
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Programs using decision-making constructs.

SOURCE CODE:

#include <stdio.h>
void main()
{
int a, b;
float f;
clrscr();
printf("Input two integers\n");
scanf("%d%d", &a, &b);
if(a>b)
{
printf(“%d is larger ”, a);
}
else
{
printf(“%d is larger ”, b);
}
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Write a program to find whether the given year is leap year or Not?

SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
clrscr();
printf("Enter the Year (YYYY) : ");
scanf("%d",&year);
if(year%4==0 && year%100!=0 || year%400==0)
printf("\nThe Given year %d is a Leap Year");
else
printf("\nThe Given year %d is Not a Leap Year");
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Design a calculator to perform the operations, namely, addition,


subtraction, multiplication, division and square of a number.
SOURCE CODE:

#include <stdio.h>
#include <conio.h>
void main()
{
int fn,sn,c;
float r;
char c1;
clrscr();
printf("Enter the First Number : ");
scanf("%d",&fn);
printf("\nEnter the Second Number : ");
scanf("%d",&sn);
do
{
printf("\nWhich operations you want to perform (+,-,*,/,s)\n");
c=getch();
switch(c)
{
case '+':
r = fn+sn;
break;
case '-':
r = fn-sn;
break;
case '*':
r = fn*sn;
break;
case '/':
r = fn/(float)sn;
break;
case 's':
r = fn*fn;
break;
} // switch
if(c=='/')
printf("\nResult = %f\n",r);
else
printf("\nResult = %.f\n",r); printf("\ndo
you want continue (y/n) :"); c1 = getch();
} while (c1=='y' || c1=='Y');
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Check whether a given number is Armstrong number or not?

SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
long int num,result;
long int armstrong(long);
int base;
clrscr();
printf("Enter the given number : ");
scanf("%ld",&num);
result = armstrong(num);
if(result==num)
printf("\nThe given number %ld is an Armstrong number",num);
else
printf("\nThe given number %ld is not an Armstrong number",num);
getch();
} // main
long int armstrong(long int num)
{
long int num1=num,res;
double r,count=0; double
sum=0.0;
for(;num1!=0;count++,num1/=10);
num1=num;
while(num1!=0)
{
r = num1%10;
sum = sum+pow(r,count);
num1 = num1/10;
}
res = sum;
return res;
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

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 <10,its weight>,<36,its weight><89,its weight>

SOURCE CODE:

#include <stdio.h>
#include <math.h>
void main()
{
int nArray[50],wArray[50],nelem,sq,i,j,t;
clrscr();
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(nArray[i] > nArray[j])
{
t = nArray[i];
nArray[i] = nArray[j];
nArray[j] = t;
}
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
// sq =(int) sqrt(nArray[i]);
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;

if(nArray[i]%4==0 && nArray[i]%6==0)


wArray[i] = wArray[i] + 4;

if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

for(i=0; i<nelem; i++)


printf("<%d,%d>", nArray[i],wArray[i]);
getch();

int prime(int num)


{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;
break;
}
return flag;
}
int percube(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{
flag=1;
break;
}
return flag;
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Populate an array with height of persons and find how many persons are
above the average height.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
//Read Number of persons
printf("Enter the Number of Persons : ");
scanf("%d",&n);
//Read the height of n persons
printf("\nEnter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
//display
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count);
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Populate a two dimensional array with height and weight of persons and
compute the Body Mass Index of the individuals.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
void main()
{
int stu[100][2];
int index[100];
int i,n;
float h;
clrscr();
printf("Enter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter the Height(cm) and Weight(kg) of student %d :",i+1);
scanf("%d%d",&stu[i][0],&stu[i][1]);
h = (float)(stu[i][0]/100.0);
index[i] = (float)stu[i][1]/(float)(h*h);
} printf("\nStu.No.\tHeight\tWeight\tBMI\tResult\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,stu[i][0],stu[i][1],index[i]);
if(index[i]<15)
printf("Starvation\n");
else if(index[i]>14 && index[i] < 18)
printf("Underweight\n");
else if(index[i] > 17 && index[i] < 26)
printf("Healthy\n");
else if(index[i] > 25 && index[i] < 31)
printf("Over weight\n");
else if(index[i] > 30 && index[i] < 36)
printf("Obese\n");
else
printf("Severe Obese\n");
} // for loop
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Reverse the string without changing the position of special characters.

SOURCE CODE:

#include <stdio.h>
#include <string.h>
#include <conio.h>
void swap(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}

// Main program
void main()
{
char str[100];
// Function Prototype
void reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();
printf("Enter the Given String : ");
scanf("%[^\n]s",str);
reverse(str);
printf("Reverse String : %s",str);
getch();
}

void reverse(char str[100])


{
// Initialize left and right pointers
int r = strlen(str) - 1, l = 0;

// Traverse string from both ends until


// 'l' and 'r'
while (l < r)
{
// Ignore special characters
if (!isAlpha(str[l]))
l++;

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

else if(!isAlpha(str[r]))
r--;

else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
}

// To check x is alphabet or not if it an alphabet then return 0 else 1

int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z') );
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Convert the given decimal number into binary, octal and hexadecimal
numbers using user defined functions.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
void swap(char *s1, char *s2)
{
char temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
void reverse(char *str, int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(&str[start], &str[end]);
start++;
end--;
}
}
char* convert(int num, char str[100], int base)
{
int i = 0;
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
str[i] = '\0'; // Append string terminator
// Reverse the string

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

reverse(str, i);
return str;
}
void main()
{
char str[100];
int n;
clrscr();
printf("Enter the given decimal number : ");
scanf("%d",&n);
printf("The Binary value : %s\n",convert(n,str,2));
printf("The Octal value : %s\n",convert(n,str,8));
printf("The Hexa value : %s\n",convert(n,str,16));
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Using built-in functions find the total number of words in a paragraph.

SOURCE CODE:

#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;

printf("enter the string\n");


scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
printf("number of words in given string are: %d\n", count + 1);
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Using built-in functions capitalize the first word of each sentence in a paragraph.

SOURCE CODE:
#include <stdio.h>
#define MAX 100
int main()
{
char str[MAX]={0};
int i;
clrscr();
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

//capitalize first character of words


for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it small/lowercase
}
}

printf("Capitalize string is: %s\n",str);

return 0;
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Using built-in functions replace a given word with another word in a


paragraph

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
count_data();
void main()
{
// calling function
count_data();
getch();
}
count_data()
{
FILE *fp,*fp_rep;
char ch,ch1,temp_str[50],rep_str[10],new_str[10];
int count=0; clrscr();
fp=fopen("c:\\windows\\desktop\\input.txt","r");
fp_rep=fopen("c:\\windows\\desktop\\input1.txt","w");
printf("\nEnter String to find:");
scanf("%s",rep_str);
printf("\nEnter String to replace:");
scanf("%s",new_str);
while((ch=getc(fp))!=EOF)
{
if(ch==' ')
{
temp_str[count]='\0';
if(strcmp(temp_str,rep_str)==0)
{
printf("Do u want to replace(y/n):");
ch1=getche();
if(ch1=='y')
{
fprintf(fp_rep," %s",new_str);
count=0;
}
else
{ fprintf(fp_rep," %s",temp_str);count=0;}
}
else
{

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

fprintf(fp_rep," %s",temp_str);
count=0;
}
}else
{
temp_str[count++]=ch;
}
}
if(strcmp(temp_str,rep_str)==0)
{

printf("Do u want to replace(y/n):");


ch1=getche();
if(ch1=='y')
{
fprintf(fp_rep,"%s ",new_str);
count=0;
}
else
{
fprintf(fp_rep,"%s ",temp_str);
count=0;
}
}else
{
fprintf(fp_rep,"%s ",temp_str);
} fclose(fp);
fclose(fp_rep);
remove("c:\\windows\\desktop\\input.txt");
rename("c:\\windows\\desktop\\input1.txt","c:\\windows\\desktop\\input.txt");
fflush(stdin);
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Solve towers of Hanoi using recursion.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{
if (n == 1)
{
printf("\n Move disk 1 from Peg %c to Peg %c", from, to);
return;
}
towerofhanoi(n-1, from, aux, to);
printf("\n Move disk %d from Peg %c to Peg %c", n, from, to);
towerofhanoi(n-1, aux, to, from);
}

int main()
{
int n;
clrscr();
printf("Enter the number of disks : ");
scanf("%d",&n); // Number of disks
towerofhanoi(n, 'A', 'C', 'B'); // A, B and C are names of peg
getch();
return 0;
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Sort the list of numbers using pass by reference.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>

void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : ");
scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

void sortarray(int* arr,int num)


{
int i,j,temp;
for(i=0;i<num-1;i++)
for(j=0;j<num-i-1;j++)
if(arr[j] > arr[j+1])
{
temp=arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Generate salary slip of employees using structures and pointers

SOURCE CODE:

#include<stdio.h>
#include<dos.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]={
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{

int EMPNO;
void gen_payslip(int);
clrscr();

printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");


scanf("%d",&EMPNO);
if(EMPNO>0 && EMPNO<13)
gen_payslip(EMPNO);
else
printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
getch();
}
void gen_payslip(int EMPNO)
{

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1: PAYRATE=400;
printf("\tDESIGNATION: CLERK");
break;
case 2: PAYRATE=300;

printf("\tDESIGNATION: SALESMEN");
break;
case 3: PAYRATE=250;
printf("\tDESIGNATION: HELPER");
break;
case 4: PAYRATE=350;
printf("\tDESIGNATION: COMP.OPTR");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d
",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\t ");
printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
printf("\n\t ");
printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
printf("\n\n\t ");
printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
printf("\n\t ");
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Compute internal marks of students for five different subjects using


structures and functions.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
struct student
{
int rl;
char nm[20];
int m1;
int m2;
int m3;
int m4;
intm5;
int t;
float per;
};
void main()
{
struct student a;
clrscr();
printf(" Enter RollNo, Name amd five sub marks\n"); scanf("%d%s%d%d
%d",&a.rl,&a.nm,&a.m1,&a.m2,&a.m3,&a.m4,&a.m5);
a.t=a.m1+a.m2+a.m3+a.m4+a.m5;
a.per=a.t/5.0; printf("rollno=
%d\n",a.rl); printf("Name=
%sk\n",a.nm); printf("m1=
%d\n",a.m1); printf("m2=
%d\n",a.m2);
printf("m3=%d\n",a.m3);
printf("m4=%d\n",a.m4);
printf("m5=%d\n",a.m5);
printf("total=%d\n",a.t);
printf("per=%f\n",a.per);
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Insert, update, delete and append telephone details of an individual or a


company into a telephone directory using random access file.

SOURCE CODE:

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct
person{ char
name[20]; long
telno;
};

void appendData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}

void showAllData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t===============\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

void findData(){
FILE *fp;
struct person obj;
char name[20];
int totrec=0;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display Specific Records*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0){
printf("\n\nName : %s",obj.name);
printf("\nTelephone No : %ld",obj.telno);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main()
{ char choice;
while(1)
{ clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) Exit\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call append record
appendData();
break;
case'2' : //call find record
findData();
break;

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

case'3' : //Read all record


showAllData();
break;
case'4' :
case 27 : exit(1);
}
}
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Count the number of account holders whose balance is less than the
minimum balance using sequential access file.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10]; char
name[20]; char
balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum
Balance\n");
printf("5. Delete All\n");
printf("6. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' : fflush(stdin);
fp=fopen("acc.dat","a");

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

printf("\nEnter the Account Number : ");


gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance);
fseek(fp,0,2);
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 for withdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is : %s",acc.balance);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
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;

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

break;
}
} 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' :
remove("acc.dat");
break;
case '6' :
fclose(fp);
exit(0);

}
printf("\nPress any key to continue....");
getch();

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

} while (choice!='6');
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

Mini project

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
int first=5,second=5,thired=5;
struct node
{
int ticketno;
int phoneno; char
name[100]; char
address[100];
}s;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s.name);
printf("\nphonenumber:");
scanf("%d",&s.phoneno);
printf("\naddress:");
scanf("%s",s.address);
printf("\nticketnumber only 1-10:");
scanf("%d",&s.ticketno);
}
void availability()
{
int c;
printf("availability cheking");
printf("\n1.first class\n2.second class\n3.thired class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:if(first>0)
{
printf("seat available\n");
first--;
}
else
{
printf("seat not available");
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

break;
case 2: if(second>0)
{
printf("seat available\n");
second--;
}
else
{
printf("seat not available");
}
break;
case 3:if(thired>0)
{
printf("seat available\n");
thired--;
}
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.thired class\n");
printf("enter the option");
scanf("%d",c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
thired++;
break;
default:
break;
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

printf("ticket is canceled");
}
main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
printf("1.booking\n2.availability cheking\n3.cancel\nenter your option:");
scanf("%d",&n);
switch(n)
{
case 1: booking();
break;
case 2: availability();
break;
case 3: cancel();
break;
default:
break;
}
getch();
}

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

OUTPUT:

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])


lOMoARcPSD|45840416

UCE TK DEPT. OF CSE C PROGRAMMING

Downloaded by Gokul Gokul ([email protected])

You might also like