C Lab Record
C Lab Record
C Lab Record
191CS11A
PROGRAMMING IN C LABORATORY
NAME :
REGISTER NO :
ROLL NO :
PROGRAMME :
YEAR :
SEMESTER :
1
CERTIFICATE
Name …………………………………………………………………………………………………….
Certified that this is the bonafide record of work done by the above student in the
191CS11A – PROGRAMMING IN C LABORATORY during the academic year 2022-23.
Submitted for the University Practical Examination held on.................................. at VEL TECH
MULTITECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE , #42,
AVADI-VELTECH ROAD , AVADI, CHENNAI-600062.
Signature of Examiners
Date …………….
2
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
SUBJECT NAME : 191CS11A – Programming in C Laboratory
YEAR/SEM : I/I
REGULATION : 2019
Vision
To emerge as centre for academic excellence in the field of Computer Science and Engineering by exposure to research
and industry practices.
Mission
M1 - To provide good teaching and learning environment with conducive research atmosphere in the field of Computer
Science and Engineering.
M2 - To propagate lifelong learning.
M3 - To impart the right proportion of knowledge, attitudes and ethics in students to enable them take up positions of
responsibility in the society and make significant contributions.
3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
An ability to identify, formulate and solve hardware and software problems using
PSO3
sound computer engineering principles.
4
PROGRAMME OUTCOMES (POs)
PO’s PROGRAMME OUTCOMES
Apply knowledge of mathematics, natural science, engineering fundamentals and system
fundamentals, software development, networking & communication, and information
PO1
assurance & security to the solution of complex engineering problems in computer science
and engineering.
Ability to identify, formulate and analyze complex Computer Science and Engineering
problems in the areas of hardware, software, theoretical Computer Science and applications
PO2
to reach significant conclusions by applying Mathematics, Natural sciences, Computer
Science and Engineering principles.
Design solutions for complex computer science and engineering problems and design
PO3 systems, components or processes that meet specified needs with appropriate consideration
for public health and safety, cultural, societal, and environmental considerations.
Ability to use research based knowledge and research methods to perform literature
survey, design experiments for complex problems in designing, developing and maintaining
PO4
a computing system, collect data from the experimental outcome, analyze and interpret
valid/interesting patterns and conclusions from the data points.
Ability to create, select and apply state of the arttools and techniques in
PO5 designing,developing and testing a computing system or its component.
Apply reasoning informed by contextual knowledge to assess societal, health, safety, legal
and cultural issues and the consequent responsibilities relevant to professional engineering
PO6 practice in system development and solutions to complex engineering problems related to
system fundamentals, software development, networking & communication, and
information assurance & security.
Understand and evaluate the sustainability and impact of professional engineering work in
the solution of complex engineering problems related to system fundamentals, software
PO7
development, networking & communication, and information assurance & security in
societal and environmental contexts.
Apply ethical principles and commit to professional ethics and responsibilities and norms
PO8
of computer science and engineering practice.
Recognize the need for, and have the preparation and ability to engage in independent and
PO12 lifelong learning in the broadest context of technological change.
5
COURSE OBJECTIVES
COURSE OUTCOMES
Course
CO Statements
Outcome
CO1 Apply the decision making constrains and loops to develop a program in C.
CO2 Develop C programs involving functions, recursion, pointers, and structures.
CO3 Design applications using sequential and random-access file processing.
CO4 Create simple applications using basic constructs, arrays and strings.
PO11
PO12
PO 1
PO 2
PO 3
PO 4
PO 5
PO 6
PO 7
PO 8
PO 9
me
CO1 3 2 2 2 2 - - 1 - - - 2
CO2 3 2 2 2 2 - - 1 - - - 2
CO3 3 2 2 2 2 - - 1 - - - 2
CO4 3 2 2 2 2 - - 1 - - - 2
CO 3 2 2 2 2 - - 1 - - - 2
6
Table of Contents
Ex Name of the Experiment Page Teacher’s
No. Date No. Sign
1 Programs using I/O statements and expressions.
3 Write a program to find whether the given year is leap year or Not?
Populate an array with height of persons and find how many persons
6
are above the average height.
Populate a two dimensional array with height and weight of personsand
7
compute the Body Mass Index of the individuals.
Given a string ―a$bcd./fg‖ find its reverse without changing the
8
position of special characters.
Convert the given decimal number into binary, octal and hexadecimal
9
numbers using user defined functions.
a given paragraph perform the following using built-in functions:
a. Find the total number of words.
10
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.
Mini Projects
7
EX.No.:1 DATE:
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
AIM
ALGORITHM
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>
#include<conio.h>
void main()
{
int age, 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();
}
OUTPUT
Entertheyear of birth:2000
Enter the current year: 2021
The age is: 21 years
RESULT:
Thus the C program to perform input and output operations using built-in printf() and
scanf() function has been done and verified successfully.
8
EX.No. 2 DATE:
ALGORITHM
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”.
Otherwise display the result as “POSITIVE”.
PROGRAM
#include<stdio.h>
#include<conio.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
printf("The given number is neither positive nor negative.",n);
getch();
}
9
OUTPUT
~~~~~~~~~~~~~~~~~~~
POSITIVE OR NEGATIVE
~~~~~~~~~~~~~~~
Enter the number: -45
The given number -45 is negative.
RESULT
Thus the C program to check for positive and negative numbers using decision
making construct has been completed and verified successfully.
10
EX.No.:3 DATE:
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)
ALGORITHM
PROGRAM
#include<stdio.h>
#includ<conio.h>
void main()
{
int n;
clrscr();
printf("********************\n");
printf("Leap year or not\n");
printf("********************\n");
printf("Enter the year\n");
scanf("%d",&n);
if(n%400 ==0)
printf("The given year %d is a leap year.",n);
else if (n%100 == 0)
printf("The given year %d is a leap year.",n);
11
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();
}
OUTPUT
***************
Leap Year or not
***************
Entertheyear
2012
The given year2012isaleapyear.
Entertheyear
2009
The given year 2009 is nonleapyear.
RESULT
Thus a C Program to check leap year was executed and the output was verified.
12
EX.No.:4 DATE:
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 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 calculator program\n");
printf("**********************\n");
printf("Enter the choice\n");
printf("1)Addition\n2)Subraction\n3)Multiplication\n 4) Division\n 5) Square\n");
scanf("%d",&ch);
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("Thedifferenceoftwonumbers%dand%d is:%d",a,b,c);
break;
case 3:
printf("Enter two numbers to be multiplied:");
scanf("%d %d",&a,&b);
13
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;
printf("The square of a number %d is:%d",a,c);
break;
default: printf("Invalidoption");
}
getch();
}
OUTPUT
****************************
Menu driven calculator program
***************************
RESULT
Thus the menu-drive arithmetic calculator has been implemented in C and verified
successfully.
14
EX.No.:5 DATE:
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
#include <stdio.h>
void main()
{
int n, sum=0;
int r,n_copy;
printf("Enter a number\t");
scanf("%d",&n);
n_copy = n;
while(n>0)
{
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);
15
getch();
}
OUTPUT
Enteranumber:
153
153 is an armstrong number.
RESULT
Thus, the program to check for an Armstrong number has been written in C and the output
was verified successfully.
16
EX.No.:6 DATE:
To write a C Program to populate a 1D array with height of persons and find howmanypersonsareabovethe average
height.LGORITHM
STEP 2: Include all required header files and declare all required variables.
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>
#include<conio.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 number ofpersons\n");
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("Thenoofpersonsabove theavg height%d are:%d\n",avg,count);
getch();
}
int aboveavg(int a[],int n,int avg)
17
{
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;
}
OUTPUT
****************
Height of the person
*****************
EntertheNumberofPersons:5
EntertheHeightofeachpersonincentimeter
150
155
162
158
154
RESULT
Thus, the program to count how many persons are above the average height value has
been implemented in C and verified.
18
EX.No.:7 DATE:
AIM
To write a CProgram to Populate a two-dimensional array with height and weight of persons and
compute their Body Mass Index of the individuals.
ALGORITHM
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;
printf("********************\n");
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",&height[i]);
printf("Weight (in kg): ");
scanf("%f",&weight[i]);
19
}
for(i=0;i<n;i++)
{
h =height[i]/100;
bmi[i]=(weight[i]/(height[i] * height[i])) * 10000;
}
printf("Weight (in kg): ");
printf("\nThe BMI of the given individuals are as follows:\n");
printf("\n================================================");
printf("\nPerson\tHeight\tWeight\tBMI\tBMI Category");
printf("\n================================================");
for(i=0;i<n;i++)
{
printf("\n%d\t%.1f\t%.1f\t%.2f\t",i+1,height[i],weight[i],bmi[i]);
if(bmi[i]>=35)
printf("Severely obese");
else if(bmi[i]>=30)
printf("Moderately obese");
else if(bmi[i]>=25)
printf("Overweight");
else if(bmi[i]>=18.5)
printf("Normal");
else if(bmi[i]>=16)
printf("Underweight");
else
printf("Severely Underweight");
}
getch();
}
20
OUTPUT
********************
BMI CALCULATION
********************
Enter the number of individuals:
2
Individual 1 :
Height (in cm): 145
Weight (in kg): 67
Individual 2 :
Height (in cm): 154
Weight (in kg): 50
31.866825
21.082813
Weight (in kg):
The BMI of the given individuals are as follows:
================================================
Person Height Weight BMI BMI Category
================================================
1 145.0 67.0 31.87 Moderately obese
2 154.0 50.0 21.08 Normal
RESULT
Thus a C Program to find Body Mass Index of the individual was executed and the output was obtained.
21
EX.No.:8 DATE:
AIM
To write a C program to reverse the given string without changing the position of
Special characters.
For example,
input: a@gh%;j
output: j@hg%;a
ALGORITHM
1. Start
2. Declare variables.
3. Read a String.
4. Check each character of string for alphabets or a special character by using isAlpha().
5. Change the position of a character vice versa
6. if it isalphabet otherwise remains same.
7. Repeat step4 until reach to the mid of the position of a string.
8. Display the output of the reverse string without changing the position of special characters.
9. Stop
PROGRAM
#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
22
printf("Enter the Given String:");
gets(str);
reverse(str);
printf("\nReverse String : %s",str);
getch();
}
void reverse(char str[100])
{
//Initialize left and right pointers
int r = strlen(str)-1,
l= 0;
else
{
swap(&str[l], &str[r]);
l++;
r--;
}
}
}
//To check x is alphabet or not if it analphabet then return 0 else 1
int isAlpha(int x)
{
return ( (x>='A' && x <='Z') || (x >='a' && x<='z') );
}
OUTPUT
RESULT
Thus a C Program for reverse of a given String was executed and the output was obtained.
23
EX.No.:9 DATE:
NUMBER SYSTEM CONVERSION
AIM
To write a C program to convert the given number from one number system to another number
system.
ALGORITHM
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;
clrscr();
printf("******************************\n");
printf("Number system - Conversion\n");
printf("******************************\n");
24
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 quivalent of given number %d is:\n",n)
while(n>0)
{
rem=n%16; if(rem>9)
{
H[i] = 55+rem;
}
else
{
H[i] = 48+rem;
25
}
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("\Equivalent of the given number in %d is:\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]);
}
}
OUTPUT
Enterthegivendecimalnumber:555
TheBinaryvalue: 1000101011
TheOctalvalue:1053
TheHexa-decimal value:22B
RESULT
Thus the C program to convert the given number from one number system to another
number system has been implemented and tested successfully.
26
EX.No.:10 DATE:
AIM
To write a C program to perform various string operations as given below using built-in functions
in string.hand stdlib.h.
a) Find the total number ofwords.
b) Capitalize the first word of eachsentence.
c) Replace a given word with anotherword.
ALGORITHM
STEP 3: Display the menu list and get the user's option and based on the input, perform the
stringoperations.
STEP 5: Get an input string from the user and perform the requested operation.
STEP 6: Count the number of words in the given sentence and display theresult.
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.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void replace(char*,char*,char*);
27
int main()
{
char choice, str[200];
int i, words;
char s_string[200], r_string[200];
/* Input text from user */
printf("Enter any text:\n ");
gets(str);
do
{
printf("\n1.Find the total number of words\n");
printf("2.Capitalize the first word of each sentence\n");
printf("3. Replace a given word with another word \n");
printf("4.Stop\n");
printf("Enter your choice:");
choice=getchar();
switch(choice)
{
case'1':
i= 0;
words= 1;
/*Runs a loop till end of text*/
while(str[i]!= '\0')
{
/*If the current character(str[i]) is whitespace*/
if(str[i] == '\0' || str[i] == '\n' || str[i] == '\t')
{
words++;
}
i++;
}
printf("\nTotal number of words = %d", words);
break;
case'2':
i=0;
/*Runs a loop till end of text*/
while(str[i]!= '\0')
{
28
/*Checks to see if a character is a punctuation mark used to denote the end of
a sentence. (!. ?)*/
if(str[i]=='!'||str[i]=='.'||str[i]=='?')
{
i++;
while(str[i] != “ “ || str[i] != '\n' || str[i] != '\t' || str[i] != '\0')
{
putchar(toupper(str[++i]));i++;
}
}
else
putchar(str[i]);
i++;
}
break;
case'3':
/*Get the search and replace string from the user.
• Write a user-defined function to replace the first occurrence of the search string
with the replace string.
• Recursively call the function until there is no occurrence of the search string.
*/
printf("\nPlease enter the string to search:");
fflush(stdin);
gets(s_string);
printf("\nPlease enter the replace string");
fflush(stdin);
gets(r_string);
replace(str,s_string,r_string);
puts(str);
break;
case'4':
exit(0);
}
printf("\nPress any key to continue...");
getch();
29
}while (choice<=4);
return 0;
}
void replace(char *str, char *s_string, char *r_string)
{
//a buffer variable to do all replace things
char buffer[200];
//to store the pointer returned from str
char *ch;
int ch_str;
//first exit condition
if(!(ch = strstr(str, s_string)))
return;
//copy all the content to buffer before the first occurrence of the search string
strncpy(buffer,str, ch_str);
//prepare the buffer for appending by adding a null to the end of it
buffer[ch_str]=0;
//append using sprint function
sprintf(buffer+(ch_str),"%s%s",r_string,ch+strlen(s_string));
//empty str for copying
str[0]= 0;
strcpy(str,buffer);
//pass recursively to replace other occurrences
return replace(str,s_string, r_string);
}
30
OUTPUT
RESULT
Thus a C Program String operation was executed and the output was obtained.
31
EX.NO.11 DATE:
TOWERS OF HANOI USING RECURSION
AIM
ALGORITHM
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
/*
Rules of Tower of Hanoi:
• Only a single disc is allowed to be transferred at a time.
• Each transfer or move should consist of taking the upper disk from one of the stacks and then placing it on
the top of another stack i.e. only a topmost disk on the stack can be moved.
• Larger disk cannot be placed over smaller disk
• placing of disk should be in increasing order.
*/
#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;
32
}
towerofhanoi(n-1, from, aux, to);
printf("\nMove disk %d from peg %c to peg %c", n, from, to);
towerofhanoi(n-1,aux, to,from);
}
int main()
{
int n;
printf("Enter the number of disks : ");
scanf("%d",&n); //Numberofdisks
towerofhanoi(n,'A','C','B'); //A, B and C are names of peg
getch();
return 0;
}
OUTPUT
RESULT
Thus a C Program Towers of Hanoi using Recursion was executed and the output was obtained.
33
EX.NO.:12 DATE:
SORTING USING PASS BY REFERENCE
AIM
To write a C Program to Sort the list of numbers using pass by reference.
ALGORITHM
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>
#include<conio.h>
void main()
{
int n, a[100], i;
void sortarray(int*, int);
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;i++)
for(j=i+1;j<num;j++)
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
34
arr[j]=temp;
}
}
OUTPUT
EntertheNumberofElementsinanarray:5
EntertheArrayelements
33
67
21
45
11
AfterSorting....
11
21
33
45
67
RESULT
Thus a C Program to sort the given numbers using pass by reference was executed and the output was obtained.
35
EX.NO.:13 DATE:
ALGORITHM
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"
structemp
{
intempno;
char name[10];
intbpay,allow,ded,npay;
structemp*next;
};
voidmain()
{
inti,n=0;
intmore_data=1;
structemp*e, *current_ptr,*head_ptr;
char answer;
head_ptr=(structemp*)malloc(sizeof(structemp));
current_ptr=head_ptr;
while(more_data)
{
36
{
printf("\nEntertheemployeenumber:");
scanf("%d", ¤t_ptr->empno) ;
printf("\nEnterthename: ");
scanf("%s",¤t_ptr->name);
printf("\nEnterthebasicpay,allowances&deductions:");
scanf("%d%d%d",¤t_ptr->bpay,¤t_ptr->allow,¤t_ptr->ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
n++;
printf("Wouldyouliketoaddanotheremployee?(y/n):");
scanf("%c", answer);
if(answer!='Y')
{
current_ptr->next=(struct emp*)NULL;
more_data= 0;
}
else
{
current_ptr->next=(structemp*)malloc(sizeof(structemp));
current_ptr=current_ptr->next;
}
}
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
current_ptr=head_ptr;
for(i=0 ;i<n;i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n",
current_ptr->empno,
current_ptr->name,
current_ptr->bpay,
current_ptr->allow,
current_ptr->ded,
current_ptr->npay) ;
current_ptr=current_ptr->next;
}
getch();
}
37
OUTPUT
Enterthenumberofemployees:
2
Enter the employee number : 101
Enterthename: Arun
Enterthebasicpay, allowances&deductions :
5000
1000
250
Entertheemployeenumber:102
Enterthename: Babu
Enterthebasicpay,allowances&deductions:
7000
1500
750
Emp.No. Name Bpay Allow Ded Npay
101 Arun50001000 2505750
102 Babu700015007507750
RESULT
Thus a C Program to generate the salary slip of employees was executed and the output was verified.
38
EX.No.:14 DATE:
STUDENT’S MARK- LIST CREATION
AIM
TowriteaCProgramto computeinternalmarksofstudentsforfive differed subjectsusingstructuresandfunctions.
ALGORITHM
1. Start
2. Declarevariables
3. Readthenumberofstudents.
4. Readthestudentmarkdetails
5. Calculate internalmarkbyi=totalofthreetestmarks/3foreachsubjectperstudent.
6. Displaytheoutputofthecalculationsforallthestudents.
7. Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
structstud
{
charname[20];
long int rollno;
int marks[3][2];
intinternal[3];
}students[10];
voidcalcinternal(int);
intmain()
{
inta,b,j,n;
printf(“Enter the number of students : \n");
scanf("%d",&n);
for(a=1;a<=n;++a)
{
printf(“\n\nEnterthedetailsofstudent%d:”,a);
39
printf("\n\nEnter student %d Name : ", a);
scanf("%s",students[a].name);
printf("\n\nEnterstudent%dRollNumber:",a);
scanf("%ld",&students[a].rollno);
int total=0;
for(b=0;b<3;++b)
{
for(j=0;j<2;++j)
{
printf("\n\nEnter the test %d mark of subject-%d : ",j+1, b+1);
scanf("%d", &students[a].marks[b,j]);
}
}
}
calcinternal(n);
for(a=1;a<=n;++a)
{
printf("\n\n\t\t\t\tMarkSheet\n");
printf("\nNameofStudent:%s",students[a].name);
printf("\t\t\t\tRollNo:%ld",students[a].rollno);
printf("\n ");
for(b=0; b<3;b++)
{
printf("\n\n\tSubject%dinternal\t\t:\t%d",b+1,students[a].internal[b]);
}
printf("\n\n \n");
getch();
}
return(0);
}
voidcalcinternal(intn)
{
int a,b,j,total;
for(a=1;a<=n;++a)
{
for(b=0;b<=3;b++)
{
total=0;
for(j=0;j<=2;++j)
{
total+=students[a].marks[b,j];
}
students[a].internal[b]=total/3;
}
}
}
40
OUTPUT
Enterstudent1Name:H.Xerio
Enterstudent1RollNumber:536435
Enterthetest1mark ofsubject-1:46
Enter the test2 mark of subject-1 : 56
Enter the test1 mark of subject-2 : 85
Enter the test2mark of subject-2 : 75
Enter the test1mark of subject-3 : 66
Enter the test2 mark of subject-3 : 86
MarkSheet
NameofStudent:H.Xerio RollNo:536435
subject1internal 59
subject2internal 78
subject3internal 74
RESULT
ThusaCProgramfor generating the mark- sheet of students by calculating the Internal marks
wasexecutedandtheoutputwasobtained.
41
EX.NO.:15 DATE:
TELEPHONE DIRECTORY
AIM
TowriteaCProgramtoadd,delete,display,Searchandexitoptionsfortelephonedetailsofanindividualintoatelephone
directoryusingrandomaccessfile.
ALGORITHM
1. Start.
2. Declarevariables,Filepointerandphonebookstructures.
3. Createmenuoptions.
4. Readtheoption.
5. Developproceduresforeachoption.
6. Calltheprocedure(Add,delete,display,Searchandexit)foruserchosenoption.
7. Displaythemessageforoperationsperformed.
8. Stop
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedefstructPhonebook_Contacts
{
char FirstName[20];
char LastName[20];
charPhoneNumber[20];
}phone;
void AddEntry(phone * );
voidDeleteEntry(phone*);
voidPrintEntry(phone*);
voidSearchForNumber(phone*);
42
intcounter=0;
char FileName[256];
FILE*pRead;
FILE*pWrite;
intmain(void)
{
phone*phonebook;
phonebook = (phone*) malloc(sizeof(phone)*100);
intiSelection= 0;
if(phonebook ==NULL)
{
printf("OutofMemory.Theprogramwillnowexit");return1;
}
else
{
}
do
{ printf("\n\t\t\tPhonebookMenu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if(iSelection==1)
{
AddEntry(phonebook);
}
if(iSelection==2)
{
DeleteEntry(phonebook);
}
if(iSelection==3)
{
PrintEntry(phonebook);
43
}
if(iSelection==4)
{
SearchForNumber(phonebook);
}
if(iSelection==5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
return0;
}
}while(iSelection<=4);
}
voidAddEntry(phone*phonebook)
{
pWrite=fopen("phonebook_contacts.dat","a");
if( pWrite== NULL)
{
perror("Thefollowingerroroccurred");exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook,sizeof(phone));
printf("\nFirstName:");
scanf("%s", phonebook[counter-1].FirstName);
printf("LastName: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s",phonebook[counter-1].PhoneNumber);
printf("\n\tFriendsuccessfullyaddedtoPhonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n",
phonebook[counter-1].FirstName,
phonebook[counter-1].LastName,
phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
voidDeleteEntry(phone* phonebook)
{
intx=0;
44
inti=0;
chardeleteFirstName[20];
chardeleteLastName[20];
printf("\nFirst name: ");
scanf("%s",deleteFirstName);
printf("Last name: ");
scanf("%s",deleteLastName);
for(x =0;x <counter;x++)
{
if(strcmp(deleteFirstName,phonebook[x].FirstName)==0)
{
if(strcmp(deleteLastName,phonebook[x].LastName)==0)
{
for(i= x;i<counter-1;i++)
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber,phonebook[i+1].PhoneNumber);
}
printf("Recorddeletedfromthephonebook.\n\n");
--counter;
return;
}
}
}
printf("Thatcontactwasnotfound,pleasetryagain.");
}
voidPrintEntry(phone*phonebook)
{
intx=0;
printf("\nPhonebookEntries:\n\n");
pRead=fopen("phonebook_contacts.dat","r");
if( pRead== NULL)
{
perror("Thefollowingerroroccurred:");
exit(EXIT_FAILURE);
}
else
{
for(x =0;x<counter;x++)
{
printf("\n(%d)\n",x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number:%s\n",phonebook[x].PhoneNumber);
}
45
}
fclose(pRead);
}
voidSearchForNumber(phone*phonebook)
{
intx=0;
charTempFirstName[20];
charTempLastName[20];
printf("\nPleasetypethenameofthefriendyouwishtofind anumberfor.");
printf("\n\nFirstName: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for(x =0;x<counter;x++)
{
if(strcmp(TempFirstName,phonebook[x].FirstName)==0)
{
if(strcmp(TempLastName,phonebook[x].LastName)==0)
{
printf("\n%s %s's phone number is %s\n",
phonebook[x].FirstName,phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}
OUTPUT
PhonebookMenu
(1) AddFriend
(2) Delete Friend
(3) DisplayPhonebookEntries
(4) SearchforPhoneNumber
(5) ExitPhonebook
Whatwouldyouliketodo?
1
FirstName: Ram
LastName:Mohan
PhoneNumber(XXX-XXX-XXXX):717-675-0909
FriendsuccessfullyaddedtoPhonebook
46
Phonebook Menu
RESULT
47
EX.NO.:16 DATE:
BANKING APPLICATION
AIM
To write a C Program toCountthenumberofaccount holder whose balance is less than the minimum
balance using sequential access file.
ALGORITHM
1. Start
2. Declare variables and file pointer.
3. Display the menu options.
4. Read the Input for transaction processing.
5. Check the validation for the input data.
6. Display the output of the calculations.
7. Repeat step 3 until choose to stop.
8. Stop
PROGRAM
/*Count thenumberofaccountholderswhosebalanceislessthantheminimumbalanceusing sequentialaccessfile.
*/
#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];
};
structBank_Accountacc;
void main()
{
long intpos1,pos2,pos;
48
FILE*fp;
char*ano,*amt;
charchoice;
inttype,flag=0;
floatbal;
do
{
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2.Display\n");
printf("3.DepositorWithdraw\n");
printf("4.NumberofAccountHolderWhoseBalanceislessthantheMinimumBalance\n");
printf("5. Stop\n");
printf("Enteryourchoice:");
choice=getchar();
switch(choice)
{
case'1':
fflush(stdin);
fp=fopen("acc.dat","a");
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case'2' :
fp=fopen("acc.dat","r");if(fp==NULL)
printf("\nFileisEmpty");
else
{
printf("\nA/c Number\tA/c Holder Name\tBalance\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("\nEntertheAccountNumber:");
49
gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEntertheType1fordeposit& Type2forwithdraw:");
scanf("%d",&type);
printf("\nYourCurrentBalanceis:%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.%sNotavailableinyourA/c\n",amt);
flag=2;
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);
}
elseif(flag==0)
printf("\nA/cNumberNotexists...Checkitagain");
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++;
50
}
printf("\nTheNumberofAccountHolderwhoseBalancelessthantheMinimumBalance:%d",flag);
fclose(fp);
break;
case'5':
fclose(fp);
exit(0);
}
printf("\nPressanykeytocontinue........................ ");
getch();
}while(choice!='5');
}
OUTPUT
1. AddaNewAccountHolder
2. Display
3. DepositorWithdraw
4. NumberofAccountHolderWhoseBalanceis lessthantheMinimumBalance
5. Stop
Enteryourchoice:1
Enter the Account Number : 547898760
Enter the Account Holder Name: Rajan
EntertheInitialAmounttodeposit:2000
Press anykeytocontinue....
1. AddaNewAccountHolder
2. Display
3. DepositorWithdraw
4. NumberofAccountHolderWhoseBalanceislessthantheMinimumBalance
5. Stop
Enteryourchoice:4
TheNumberofAccountHolderwhoseBalancelessthantheMinimumBalance:0
RESULT
ThusaCProgramforBankingApplicationwasexecutedandtheoutputwasobtained.
51
MINI PROJECT(S)
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int i,j;
int main_exit;
void menu();
struct date{
int month,day,year;
};
struct {
char name[60];
int acc_no,age;
char address[60];
char citizenship[15];
double phone;
char acc_type[10];
float amt;
struct date dob;
struct date deposit;
struct date withdraw;
}add,upd,check,rem,transaction;
}
void fordelay(int j)
{ int i,k;
for(i=0;i<j;i++)
k=i;
}
void new_acc()
{
int choice;
FILE *ptr;
52
ptr=fopen("record.dat","a+");
account_no:
system("cls");
printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD \xB2\xB2\xB2\xB2");
printf("\n\n\nEnter today's date(mm/dd/yyyy):");
scanf("%d/%d/%d",&add.deposit.month,&add.deposit.day,&add.deposit.year);
printf("\nEnter the account number:");
scanf("%d",&check.acc_no);
while(fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citize
nship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if (check.acc_no==add.acc_no)
{printf("Account no. already in use!");
fordelay(1000000000);
gotoaccount_no;
}
}
add.acc_no=check.acc_no;
printf("\nEnter the name:");
scanf("%s",add.name);
printf("\nEnter the date of birth(mm/dd/yyyy):");
scanf("%d/%d/%d",&add.dob.month,&add.dob.day,&add.dob.year);
printf("\nEnter the age:");
scanf("%d",&add.age);
printf("\nEnter the address:");
scanf("%s",add.address);
printf("\nEnter the citizenship number:");
scanf("%s",add.citizenship);
printf("\nEnter the phone number: ");
scanf("%lf",&add.phone);
printf("\nEnter the amount to deposit:$");
scanf("%f",&add.amt);
printf("\nType of account:\n\t#Saving\n\t#Current\n\t#Fixed1(for 1 year)\n\t#Fixed2(for 2 years)\n\t#Fixed3(for 3
years)\n\n\tEnter your choice:");
scanf("%s",add.acc_type);
fclose(ptr);
printf("\nAccount created successfully!");
add_invalid:
printf("\n\n\n\t\tEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if(main_exit==0)
close();
else
53
{
printf("\nInvalid!\a");
gotoadd_invalid;
}
}
void view_list()
{
FILE *view;
view=fopen("record.dat","r");
int test=0;
system("cls");
printf("\nACC. NO.\tNAME\t\t\tADDRESS\t\t\tPHONE\n");
fclose(view);
if (test==0)
{ system("cls");
printf("\nNO RECORDS!!\n");}
view_list_invalid:
printf("\n\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if(main_exit==0)
close();
else
{
printf("\nInvalid!\a");
gotoview_list_invalid;
}
}
void edit(void)
{
int choice,test=0;
FILE *old,*newrec;
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
printf("\nEnter the account no. of the customer whose info you want to change:");
scanf("%d",&upd.acc_no);
while(fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizens
hip,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if (add.acc_no==upd.acc_no)
54
{ test=1;
printf("\nWhich information do you want to change?\n1.Address\n2.Phone\n\nEnter your choice(1 for address and 2 for
phone):");
scanf("%d",&choice);
system("cls");
if(choice==1)
{printf("Enter the new address:");
scanf("%s",upd.address);
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,upd.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
system("cls");
printf("Changes saved!");
}
else if(choice==2)
{
printf("Enter the new phone number:");
scanf("%lf",&upd.phone);
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,upd.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
system("cls");
printf("Changes saved!");
}
}
else
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test!=1)
{ system("cls");
printf("\nRecord not found!!\a\a\a");
edit_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
edit();
else
{printf("\nInvalid!\a");
gotoedit_invalid;}
}
55
else
{printf("\n\n\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
}
void transact(void)
{ int choice,test=0;
FILE *old,*newrec;
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
if(add.acc_no==transaction.acc_no)
{ test=1;
if(strcmpi(add.acc_type,"fixed1")==0||strcmpi(add.acc_type,"fixed2")==0||strcmpi(add.acc_type,"fixed3")==0)
{
printf("\a\a\a\n\nYOU CANNOT DEPOSIT OR WITHDRAW CASH IN FIXED ACCOUNTS!!!!!");
fordelay(1000000000);
system("cls");
menu();
}
printf("\n\nDo you want to\n1.Deposit\n2.Withdraw?\n\nEnter your choice(1 for deposit and 2 for withdraw):");
scanf("%d",&choice);
if (choice==1)
{
printf("Enter the amount you want to deposit:$ ");
scanf("%f",&transaction.amt);
add.amt+=transaction.amt;
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
printf("\n\nDeposited successfully!");
}
else
{
printf("Enter the amount you want to withdraw:$ ");
scanf("%f",&transaction.amt);
add.amt-=transaction.amt;
56
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
printf("\n\nWithdrawn successfully!");
}
}
else
{
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
}
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test!=1)
{
printf("\n\nRecord not found!!");
transact_invalid:
printf("\n\n\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==0)
transact();
else if (main_exit==1)
menu();
else if (main_exit==2)
close();
else
{
printf("\nInvalid!");
gototransact_invalid;
}
}
else
{
printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
}
void erase(void)
{
FILE *old,*newrec;
int test=0;
57
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
printf("Enter the account no. of the customer you want to delete:");
scanf("%d",&rem.acc_no);
while (fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizens
hip,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if(add.acc_no!=rem.acc_no)
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.
phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
else
{test++;
printf("\nRecord deleted successfully!\n");
}
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test==0)
{
printf("\nRecord not found!!\a\a\a");
erase_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
erase();
else
{printf("\nInvalid!\a");
gotoerase_invalid;}
}
else
{printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
void see(void)
{
FILE *ptr;
58
int test=0,rate;
int choice;
float time;
float intrst;
ptr=fopen("record.dat","r");
printf("Do you want to check by\n1.Account no\n2.Name\nEnter your choice:");
scanf("%d",&choice);
if (choice==1)
{ printf("Enter the account number:");
scanf("%d",&check.acc_no);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on
%d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
59
printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
}
}
}
else if (choice==2)
{ printf("Enter the name:");
scanf("%s",&check.name);
while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f
%d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizens
hip,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if(strcmpi(add.name,check.name)==0)
{ system("cls");
test=1;
printf("\nAccount No.:%d\nName:%s \nDOB:%d/%d/%d \nAge:%d \nAddress:%s \nCitizenship No:%s \nPhone
number:%.0lf \nType Of Account:%s \nAmount deposited:$%.2f \nDate Of
Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citize
nship,add.phone,
add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
if(strcmpi(add.acc_type,"fixed1")==0)
{
time=1.0;
rate=9;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on
%d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
}
else if(strcmpi(add.acc_type,"fixed2")==0)
{
time=2.0;
rate=11;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on
%d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on
%d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
60
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
}
}
}
fclose(ptr);
if(test!=1)
{ system("cls");
printf("\nRecord not found!!\a\a\a");
see_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
see();
else
{
system("cls");
printf("\nInvalid!\a");
gotosee_invalid;}
}
else
{printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);}
if (main_exit==1)
{
system("cls");
menu();
}
else
{
system("cls");
close();
61
}
void close(void)
{
printf("\n\n\n\nThis C Mini Project is developed by Code With C team!");
}
void menu(void)
{ int choice;
system("cls");
system("color 9");
printf("\n\n\t\t\tCUSTOMER ACCOUNT BANKING MANAGEMENT SYSTEM");
printf("\n\n\n\t\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 WELCOME TO THE MAIN MENU
\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\n\n\t\t1.Create new account\n\t\t2.Update information of existing account\n\t\t3.For transactions\n\t\t4.Check
the details of existing account\n\t\t5.Removing existing account\n\t\t6.View customer's list\n\t\t7.Exit\n\n\n\n\n\t\t
Enter your choice:");
scanf("%d",&choice);
system("cls");
switch(choice)
{
case 1:new_acc();
break;
case 2:edit();
break;
case 3:transact();
break;
case 4:see();
break;
case 5:erase();
break;
case 6:view_list();
break;
case 7:close();
break;
}
int main()
{
char pass[10],password[10]="codewithc";
int i=0;
printf("\n\n\t\tEnter the password to login:");
scanf("%s",pass);
/*do
{
//if (pass[i]!=13&&pass[i]!=8)
{
62
printf("*");
pass[i]=getch();
i++;
}
}while (pass[i]!=13);
pass[10]='\0';*/
if (strcmp(pass,password)==0)
{printf("\n\nPassword Match!\nLOADING");
for(i=0;i<=6;i++)
{
fordelay(100000000);
printf(".");
}
system("cls");
menu();
}
else
{ printf("\n\nWrong password!!\a\a\a");
login_try:
printf("\nEnter 1 to try again and 0 to exit:");
scanf("%d",&main_exit);
if (main_exit==1)
{
system("cls");
main();
}
else if (main_exit==0)
{
system("cls");
close();}
else
{printf("\nInvalid!");
fordelay(1000000000);
system("cls");
gotologin_try;}
}
return 0;
}
63
SYSTEM HOTEL MANAGEMENT
AIM:
To design and develop a hotel management software for managing the hotel using C-Program
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
void add(); //FUNCTIONS
void list();
void edit(); //GLOBALLY DECLARED FUNCTIONS N VARIABLE
void delete1();
void search();
if(GetConsoleScreenBufferInfo(hStdOut,&csbi))
{
wColor=(csbi.wAttributes& 0xB0)+(ForgC& 0x0B);
// SetConsoleTextAttributes(hStdOut,wColor);
SetConsoleTextAttribute(hStdOut,wColor);
}
}
void login()
{
int a=0,i=0;
char uname[10],c=' ';
char pword[10],code[10];
char user[10]="user";
char pass[10]="pass";
do
{
system("cls");
break;
65
}
else
{
printf("\n SORRY !!!! LOGIN IS UNSUCESSFUL");
a++;
getch();
}
}
while(a<=2);
if (a>2)
{
printf("\nSorry you have entered the wrong username and password for four times!!!");
getch();
}
system("cls");
}
time_t t;
time(&t);
char customername;
char choice;
getch();
system("cls");
login();
system("cls");
while (1) // INFINITE LOOP
{
system("cls");
setcolor(10);
for(i=0;i<80;i++)
printf("-");
printf("\n");
printf(" ****************************** |MAIN MENU| *****************************
\n");
for(i=0;i<80;i++)
printf("-");
printf("\n");
setcolor(10);
printf("\t\t *Please enter your choice for menu*:");
printf("\n\n");
printf(" \n Enter 1 -> Book a room");
printf("\n ");
printf(" \n Enter 2 -> View Customer Record");
printf("\n ");
printf(" \n Enter 3 -> Delete Customer Record");
printf("\n ");
printf(" \n Enter 4 -> Search Customer Record");
printf("\n ");
printf(" \n Enter 5 -> Edit Record");
printf("\n ");
printf(" \n Enter 6 -> Exit");
printf("\n ");
printf("\n");
for(i=0;i<80;i++)
printf("-");
printf("\nCurrent date and time : %s",ctime(&t));
67
for(i=0;i<80;i++)
printf("-");
choice=getche();
choice=toupper(choice);
switch(choice) // SWITCH STATEMENT
{
case '1':
add();break;
case '2':
list();break;
case '3':
delete1();break;
case '4':
search();break;
case '5':
edit();break;
case '6':
system("cls");
printf("\n\n\t *****THANK YOU*****");
printf("\n\t FOR TRUSTING OUR SERVICE");
// Sleep(2000);
exit(0);
break;
default:
system("cls");
printf("Incorrect Input");
printf("\n Press any key to continue");
getch();
}
}
}
void add()
{
FILE *f;
char test;
f=fopen("add.txt","a+");
if(f==0)
{ f=fopen("add.txt","w+");
system("cls");
printf("Please hold on while we set our database in your computer!!");
printf("\n Process completed press any key to continue!! ");
getch();
}
while(1)
{
system("cls");
printf("\n Enter Customer Details:");
printf("\n**************************");
printf("\n Enter Room number:\n");
scanf("\n%s",s.roomnumber);
fflush(stdin);
printf("Enter Name:\n");
68
scanf("%s",s.name);
printf("Enter Address:\n");
scanf("%s",s.address);
printf("Enter Phone Number:\n");
scanf("%s",s.phonenumber);
printf("Enter Nationality:\n");
scanf("%s",s.nationality);
printf("Enter Email:\n");
scanf(" %s",s.email);
printf("Enter Period(\'x\'days):\n");
scanf("%s",&s.period);
printf("Enter Arrival date(dd\\mm\\yyyy):\n");
scanf("%s",&s.arrivaldate);
fwrite(&s,sizeof(s),1,f);
fflush(stdin);
printf("\n\n1 Room is successfully booked!!");
printf("\n Press esc key to exit, any other key to add another customer detail:");
test=getche();
if(test==27)
break;
}
fclose(f);
}
void list()
{
FILE *f;
int i;
if((f=fopen("add.txt","r"))==NULL)
exit(0);
system("cls");
printf("ROOM ");
printf("NAME\t ");
printf("\tADDRESS ");
printf("\tPHONENUMBER ");
printf("\tNATIONALITY ");
printf("\tEMAIL ");
printf("\t\t PERIOD ");
printf("\t ARRIVALDATE \n");
for(i=0;i<118;i++)
printf("-");
while(fread(&s,sizeof(s),1,f)==1)
{
/*printf("ROOMNUMBER :\t%s\n",s.roomnumber);
printf("NAME:\t%s\n",,s.name);
printf("ADDRESS:\t%s\n",s.address);
printf("PHONENUMBER:\t%s\n",s.phonenumber);
printf("NATIONALITY \n");*/
printf("\n%s \t%s \t\t%s \t\t%s \t%s \t%s \t %s \t %s",s.roomnumber, s.name , s.address ,
s.phonenumber ,s.nationality ,s.email,s.period, s.arrivaldate);
}
printf("\n");
69
for(i=0;i<118;i++)
printf("-");
fclose(f);
getch();
}
void delete1()
{
FILE *f,*t;
int i=1;
char roomnumber[20];
if((t=fopen("temp.txt","w"))==NULL)
exit(0);
if((f=fopen("add.txt","r"))==NULL)
exit(0);
system("cls");
printf("Enter the Room Number of the hotel to be deleted from the database: \n");
fflush(stdin);
scanf("%s",roomnumber);
while(fread(&s,sizeof(s),1,f)==1)
{
if(strcmp(s.roomnumber,roomnumber)==0)
{ i=0;
continue;
}
else
fwrite(&s,sizeof(s),1,t);
}
if(i==1)
{
printf("\n\n Records of Customer in this Room number is not found!!");
//remove("E:/file.txt");
//rename("E:/temp.txt","E:/file.txt");
getch();
fclose(f);
fclose(t);
main();
}
fclose(f);
fclose(t);
remove("add.txt");
rename("temp.txt","add.txt");
printf("\n\nThe Customer is successfully removed... ");
fclose(f);
fclose(t);
getch();
}
void search()
{
system("cls");
FILE *f;
char roomnumber[20];
70
int flag=1;
f=fopen("add.txt","r+");
if(f==0)
exit(0);
fflush(stdin);
printf("Enter Room number of the customer to search its details: \n");
scanf("%s", roomnumber);
while(fread(&s,sizeof(s),1,f)==1)
{
if(strcmp(s.roomnumber,roomnumber)==0){
flag=0;
printf("\n\tRecord Found\n ");
printf("\nRoom Number:\t%s",s.roomnumber);
printf("\nName:\t%s",s.name);
printf("\nAddress:\t%s",s.address);
printf("\nPhone number:\t%s",s.phonenumber);
printf("\nNationality:\t%s",s.nationality);
printf("\nEmail:\t%s",s.email);
printf("\nPeriod:\t%s",s.period);
printf("\nArrival date:\t%s",s.arrivaldate);
flag=0;
break;
}
}
if(flag==1){
printf("\n\tRequested Customer could not be found!");
}
getch();
fclose(f);
}
void edit()
{
FILE *f;
int k=1;
char roomnumber[20];
long int size=sizeof(s);
if((f=fopen("add.txt","r+"))==NULL)
exit(0);
system("cls");
printf("Enter Room number of the customer to edit:\n\n");
scanf("%[^\n]",roomnumber);
fflush(stdin);
while(fread(&s,sizeof(s),1,f)==1)
{
if(strcmp(s.roomnumber,roomnumber)==0)
{
k=0;
printf("\nEnter Room Number :");
gets(s.roomnumber);
printf("\nEnter Name :");
fflush(stdin);
scanf("%s",&s.name);
printf("\nEnter Address :");
71
scanf("%s",&s.address);
printf("\nEnter Phone number :");
scanf("%f",&s.phonenumber);
printf("\nEnter Nationality :");
scanf("%s",&s.nationality);
printf("\nEnter Email :");
scanf("%s",&s.email);
printf("\nEnter Period :");
scanf("%s",&s.period);
printf("\nEnter Arrival date :");
scanf("%s",&s.arrivaldate);
fseek(f,size,SEEK_CUR); //to go to desired position infile
fwrite(&s,sizeof(s),1,f);
break;
}
}
if(k==1){
printf("\n\nTHE RECORD DOESN'T EXIST!!!!");
fclose(f);
getch();
}else{ fc
lose(f);
printf("\n\n\t\tYOUR RECORD IS SUCCESSFULLY EDITED!!!");
getch();
}
}
72
LIBRARY MANAGEMENT SYSTEM
AIM:
To design and develop a Library Management System using C-Program.
PROGRAM
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#include<string.h> //contains strcmp(),strcpy(),strlen(),etc
#include<ctype.h> //contains toupper(), tolower(),etc
#include<dos.h> //contains _dos_getdate
#include<time.h>
//#include<bios.h>
#define RETURNTIME 15
char catagories[][15]={"Computer","Electronics","Electrical","Civil","Mechnnical","Architecture"};
void returnfunc(void);
73
void mainmenu(void);
void addbooks(void);
void deletebooks(void);
void editbooks(void);
void searchbooks(void);
void issuebooks(void);
void viewbooks(void);
void closeapplication(void);
int getdata();
int checkid(int);
int t(void);
//void show_mouse(void);
void Password();
void issuerecord();
void loaderanim();
struct meroDate
{
int mm,dd,yy;
};
struct books
{
int id;
char stname[20];
char name[20];
char Author[20];
int quantity;
float Price;
int count;
int rackno;
char *cat;
struct meroDate issued;
struct meroDateduedate;
};
struct books a;
int main()
{
Password();
74
getch();
return 0;
}
void mainmenu()
{
//loaderanim();
system("cls");
// textbackground(13);
int i;
gotoxy(20,3);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 MAIN MENU
\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
// show_mouse();
gotoxy(20,5);
printf("\xDB\xDB\xDB\xDB\xB2 1. Add Books ");
gotoxy(20,7);
printf("\xDB\xDB\xDB\xDB\xB2 2. Delete books");
gotoxy(20,9);
printf("\xDB\xDB\xDB\xDB\xB2 3. Search Books");
gotoxy(20,11);
printf("\xDB\xDB\xDB\xDB\xB2 4. Issue Books");
gotoxy(20,13);
printf("\xDB\xDB\xDB\xDB\xB2 5. View Book list");
gotoxy(20,15);
printf("\xDB\xDB\xDB\xDB\xB2 6. Edit Book's Record");
gotoxy(20,17);
printf("\xDB\xDB\xDB\xDB\xB2 7. Close Application");
gotoxy(20,19);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,20);
t();
gotoxy(20,21);
printf("Enter your choice:");
switch(getch())
{
case '1':
addbooks();
break;
case '2':
deletebooks();
break;
case '3':
searchbooks();
break;
case '4':
issuebooks();
break;
case '5':
viewbooks();
break;
case '6':
editbooks();
75
break;
case '7':
{
system("cls");
gotoxy(16,3);
printf("\tLibrary Management System");
gotoxy(16,4);
printf("\tMini Project in C");
gotoxy(16,5);
printf("\tis brought to you by");
gotoxy(16,7);
printf("\tCode with C Team");
gotoxy(16,8);
printf("******************************************");
gotoxy(16,10);
printf("*******************************************");
gotoxy(16,11);
printf("*******************************************");
gotoxy(16,13);
printf("********************************************");
gotoxy(10,17);
printf("Exiting in 3 second...........>");
//flushall();
Sleep(3000);
exit(0);
}
default:
{
gotoxy(10,23);
printf("\aWrong Entry!!Please re-entered correct option");
if(getch())
mainmenu();
}
}
}
void addbooks(void) //funtion that add books
{
system("cls");
int i;
gotoxy(20,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2SELECT
CATEGOIES\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,7);
printf("\xDB\xDB\xDB\xDB\xB2 1. Computer");
gotoxy(20,9);
printf("\xDB\xDB\xDB\xDB\xB2 2. Electronics");
gotoxy(20,11);
printf("\xDB\xDB\xDB\xDB\xB2 3. Electrical");
gotoxy(20,13);
printf("\xDB\xDB\xDB\xDB\xB2 4. Civil");
gotoxy(20,15);
printf("\xDB\xDB\xDB\xDB\xB2 5. Mechanical");
gotoxy(20,17);
76
printf("\xDB\xDB\xDB\xDB\xB2 6. Architecture");
gotoxy(20,19);
printf("\xDB\xDB\xDB\xDB\xB2 7. Back to main menu");
gotoxy(20,21);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,22);
printf("Enter your choice:");
scanf("%d",&s);
if(s==7)
mainmenu() ;
system("cls");
fp=fopen("Bibek.dat","ab+");
if(getdata()==1)
{
a.cat=catagories[s-1];
fseek(fp,0,SEEK_END);
fwrite(&a,sizeof(a),1,fp);
fclose(fp);
gotoxy(21,14);
printf("The record is sucessfully saved");
gotoxy(21,15);
printf("Save any more?(Y / N):");
if(getch()=='n')
mainmenu();
else
system("cls");
addbooks();
}
}
void deletebooks() //function that delete items from file fp
{
system("cls");
int d;
char another='y';
while(another=='y') //infinite loop
{
system("cls");
gotoxy(10,5);
printf("Enter the Book ID to delete:");
scanf("%d",&d);
fp=fopen("Bibek.dat","rb+");
rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(a.id==d)
{
gotoxy(10,7);
printf("The book record is available");
gotoxy(10,8);
printf("Book name is %s",a.name);
gotoxy(10,9);
77
printf("Rack No. is %d",a.rackno);
findbook='t';
}
}
if(findbook!='t')
{
gotoxy(10,10);
printf("No record is found modify the search");
if(getch())
mainmenu();
}
if(findbook=='t' )
{
gotoxy(10,9);
printf("Do you want to delete it?(Y/N):");
if(getch()=='y')
{
ft=fopen("test.dat","wb+"); //temporary file for delete
rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(a.id!=d)
{
fseek(ft,0,SEEK_CUR);
fwrite(&a,sizeof(a),1,ft); //write all in tempory file except that
} //we want to delete
}
fclose(ft);
fclose(fp);
remove("Bibek.dat");
rename("test.dat","Bibek.dat"); //copy all item from temporary file to fp except that
fp=fopen("Bibek.dat","rb+"); //we want to delete
if(findbook=='t')
{
gotoxy(10,10);
printf("The record is sucessfully deleted");
gotoxy(10,11);
printf("Delete another record?(Y/N)");
}
}
else
mainmenu();
fflush(stdin);
another=getch();
}
}
gotoxy(10,15);
mainmenu();
}
void searchbooks()
{
system("cls");
int d;
printf("*****************************Search Books*********************************");
78
gotoxy(20,10);
printf("\xDB\xDB\xDB\xB2 1. Search By ID");
gotoxy(20,14);
printf("\xDB\xDB\xDB\xB2 2. Search By Name");
gotoxy( 15,20);
printf("Enter Your Choice");
fp=fopen("Bibek.dat","rb+"); //open file for reading propose
rewind(fp); //move pointer at the begining of file
switch(getch())
{
case '1':
{
system("cls");
gotoxy(25,4);
printf("****Search Books By Id****");
gotoxy(20,5);
printf("Enter the book id:");
scanf("%d",&d);
gotoxy(20,7);
printf("Searching....... ");
while(fread(&a,sizeof(a),1,fp)==1)
{
if(a.id==d)
{
Sleep(2);
gotoxy(20,7);
printf("The Book is available");
gotoxy(20,8);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2");
gotoxy(20,9);
printf("\xB2 ID:%d",a.id);gotoxy(47,9);printf("\xB2");
gotoxy(20,10);
printf("\xB2 Name:%s",a.name);gotoxy(47,10);printf("\xB2");
gotoxy(20,11);
printf("\xB2 Author:%s ",a.Author);gotoxy(47,11);printf("\xB2");
gotoxy(20,12);
printf("\xB2 Qantity:%d ",a.quantity);gotoxy(47,12);printf("\xB2"); gotoxy(47,11);printf("\xB2");
gotoxy(20,13);
printf("\xB2 Price:Rs.%.2f",a.Price);gotoxy(47,13);printf("\xB2");
gotoxy(20,14);
printf("\xB2 Rack No:%d ",a.rackno);gotoxy(47,14);printf("\xB2");
gotoxy(20,15);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2");
findbook='t';
}
}
if(findbook!='t') //checks whether conditiion enters inside loop or not
{
gotoxy(20,8);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,9);printf("\xB2"); gotoxy(38,9);printf("\xB2");
79
gotoxy(20,10);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,9);printf("\aNo Record Found");
}
gotoxy(20,17);
printf("Try another search?(Y/N)");
if(getch()=='y')
searchbooks();
else
mainmenu();
break;
}
case '2':
{
char s[15];
system("cls");
gotoxy(25,4);
printf("****Search Books By Name****");
gotoxy(20,5);
printf("Enter Book Name:");
scanf("%s",s);
int d=0;
while(fread(&a,sizeof(a),1,fp)==1)
{
if(strcmp(a.name,(s))==0) //checks whether a.name is equal to s or not
{
gotoxy(20,7);
printf("The Book is available");
gotoxy(20,8);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2");
gotoxy(20,9);
printf("\xB2 ID:%d",a.id);gotoxy(47,9);printf("\xB2");
gotoxy(20,10);
printf("\xB2 Name:%s",a.name);gotoxy(47,10);printf("\xB2");
gotoxy(20,11);
printf("\xB2 Author:%s",a.Author);gotoxy(47,11);printf("\xB2");
gotoxy(20,12);
printf("\xB2 Qantity:%d",a.quantity);gotoxy(47,12);printf("\xB2");
gotoxy(20,13);
printf("\xB2 Price:Rs.%.2f",a.Price);gotoxy(47,13);printf("\xB2");
gotoxy(20,14);
printf("\xB2 Rack No:%d ",a.rackno);gotoxy(47,14);printf("\xB2");
gotoxy(20,15);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2");
d++;
}
}
if(d==0)
{
gotoxy(20,8);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
80
gotoxy(20,9);printf("\xB2"); gotoxy(38,9);printf("\xB2");
gotoxy(20,10);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,9);printf("\aNo Record Found");
}
gotoxy(20,17);
printf("Try another search?(Y/N)");
if(getch()=='y')
searchbooks();
else
mainmenu();
break;
}
default :
getch();
searchbooks();
}
fclose(fp);
}
void issuebooks(void) //function that issue books from library
{
int t;
system("cls");
printf("********************************ISSUE SECTION**************************");
gotoxy(10,5);
printf("\xDB\xDB\xDB\xDb\xB2 1. Issue a Book");
gotoxy(10,7);
printf("\xDB\xDB\xDB\xDb\xB2 2. View Issued Book");
gotoxy(10,9);
printf("\xDB\xDB\xDB\xDb\xB2 3. Search Issued Book");
gotoxy(10,11);
printf("\xDB\xDB\xDB\xDb\xB2 4. Remove Issued Book");
gotoxy(10,14);
printf("Enter a Choice:");
switch(getch())
{
case '1': //issue book
{
system("cls");
int c=0;
char another='y';
while(another=='y')
{
system("cls");
gotoxy(15,4);
printf("***Issue Book section***");
gotoxy(10,6);
printf("Enter the Book Id:");
scanf("%d",&t);
fp=fopen("Bibek.dat","rb");
fs=fopen("Issue.dat","ab+");
if(checkid(t)==0) //issues those which are present in library
{
81
gotoxy(10,8);
printf("The book record is available");
gotoxy(10,9);
printf("There are %d unissued books in library ",a.quantity);
gotoxy(10,10);
printf("The name of book is %s",a.name);
gotoxy(10,11);
printf("Enter student name:");
scanf("%s",a.stname);
//struct dosdate_t d; //for current date
//_dos_getdate(&d);
//a.issued.dd=d.day;
//a.issued.mm=d.month;
//a.issued.yy=d.year;
gotoxy(10,12);
printf("Issued date=%d-%d-%d",a.issued.dd,a.issued.mm,a.issued.yy);
gotoxy(10,13);
printf("The BOOK of ID %d is issued",a.id);
a.duedate.dd=a.issued.dd+RETURNTIME; //for return date
a.duedate.mm=a.issued.mm;
a.duedate.yy=a.issued.yy;
if(a.duedate.dd>30)
{
a.duedate.mm+=a.duedate.dd/30;
a.duedate.dd-=30;
}
if(a.duedate.mm>12)
{
a.duedate.yy+=a.duedate.mm/12;
a.duedate.mm-=12;
}
gotoxy(10,14);
printf("To be return:%d-%d-%d",a.duedate.dd,a.duedate.mm,a.duedate.yy);
fseek(fs,sizeof(a),SEEK_END);
fwrite(&a,sizeof(a),1,fs);
fclose(fs);
c=1;
}
if(c==0)
{
gotoxy(10,11);
printf("No record found");
}
gotoxy(10,15);
printf("Issue any more(Y/N):");
fflush(stdin);
another=getche();
fclose(fp);
}
break;
}
82
case '2': //show issued book list
{
system("cls");
int j=4;
printf("*******************************Issued book list*******************************\n");
gotoxy(2,2);
printf("STUDENT NAME CATEGORY ID BOOK NAME ISSUED DATE RETURN DATE");
fs=fopen("Issue.dat","rb");
while(fread(&a,sizeof(a),1,fs)==1)
{
gotoxy(2,j);
printf("%s",a.stname);
gotoxy(18,j);
printf("%s",a.cat);
gotoxy(30,j);
printf("%d",a.id);
gotoxy(36,j);
printf("%s",a.name);
gotoxy(51,j);
printf("%d-%d-%d",a.issued.dd,a.issued.mm,a.issued.yy );
gotoxy(65,j);
printf("%d-%d-%d",a.duedate.dd,a.duedate.mm,a.duedate.yy);
//struct dosdate_t d;
//_dos_getdate(&d);
gotoxy(50,25);
// printf("Current date=%d-%d-%d",d.day,d.month,d.year);
j++;
}
fclose(fs);
gotoxy(1,25);
returnfunc();
}
break;
case '3': //search issued books by id
{
system("cls");
gotoxy(10,6);
printf("Enter Book ID:");
int p,c=0;
char another='y';
while(another=='y')
{
scanf("%d",&p);
fs=fopen("Issue.dat","rb");
while(fread(&a,sizeof(a),1,fs)==1)
{
if(a.id==p)
{
issuerecord();
gotoxy(10,12);
printf("Press any key.......");
83
getch();
issuerecord();
c=1;
}
}
fflush(stdin);
fclose(fs);
if(c==0)
{
gotoxy(10,8);
printf("No Record Found");
}
gotoxy(10,13);
printf("Try Another Search?(Y/N)");
another=getch();
}
}
break;
case '4': //remove issued books from list
{
system("cls");
int b;
FILE *fg; //declaration of temporary file for delete
char another='y';
while(another=='y')
{
gotoxy(10,5);
printf("Enter book id to remove:");
scanf("%d",&b);
fs=fopen("Issue.dat","rb+");
while(fread(&a,sizeof(a),1,fs)==1)
{
if(a.id==b)
{
issuerecord();
findbook='t';
}
if(findbook=='t')
{
gotoxy(10,12);
printf("Do You Want to Remove it?(Y/N)");
if(getch()=='y')
{
fg=fopen("record.dat","wb+");
rewind(fs);
while(fread(&a,sizeof(a),1,fs)==1)
{
if(a.id!=b)
{
fseek(fs,0,SEEK_CUR);
fwrite(&a,sizeof(a),1,fg);
}
}
84
fclose(fs); fclose(fg);
remove("Issue.dat");
rename("record.dat","Issue.dat");
gotoxy(10,14);
printf("The issued book is removed from list");
}
if(findbook!='t')
{
gotoxy(10,15);
printf("No Record Found");
}
}
gotoxy(10,16);
printf("Delete any more?(Y/N)");
another=getch();
}
}
default:
gotoxy(10,18);
printf("\aWrong Entry!!");
getch();
issuebooks();
break;
}
gotoxy(1,30);
returnfunc();
}
void viewbooks(void) //show the list of book persists in library
{
int i=0,j;
system("cls");
gotoxy(1,1);
printf("*********************************Book List*****************************");
gotoxy(2,2);
printf(" CATEGORY ID BOOK NAME AUTHOR QTY PRICE RackNo ");
j=4;
fp=fopen("Bibek.dat","rb");
while(fread(&a,sizeof(a),1,fp)==1)
{
gotoxy(3,j);
printf("%s",a.cat);
gotoxy(16,j);
printf("%d",a.id);
gotoxy(22,j);
printf("%s",a.name);
gotoxy(36,j);
printf("%s",a.Author);
gotoxy(50,j);
printf("%d",a.quantity);
gotoxy(57,j);
85
printf("%.2f",a.Price);
gotoxy(69,j);
printf("%d",a.rackno);
printf("\n\n");
j++;
i=i+a.quantity;
}
gotoxy(3,25);
printf("Total Books =%d",i);
fclose(fp);
gotoxy(35,25);
returnfunc();
}
void editbooks(void) //edit information about book
{
system("cls");
int c=0;
int d,e;
gotoxy(20,4);
printf("****Edit Books Section****");
char another='y';
while(another=='y')
{
system("cls");
gotoxy(15,6);
printf("Enter Book Id to be edited:");
scanf("%d",&d);
fp=fopen("Bibek.dat","rb+");
while(fread(&a,sizeof(a),1,fp)==1)
{
if(checkid(d)==0)
{
gotoxy(15,7);
printf("The book is availble");
gotoxy(15,8);
printf("The Book ID:%d",a.id);
gotoxy(15,9);
printf("Enter new name:");scanf("%s",a.name);
gotoxy(15,10);
printf("Enter new Author:");scanf("%s",a.Author);
gotoxy(15,11);
printf("Enter new quantity:");scanf("%d",&a.quantity);
gotoxy(15,12);
printf("Enter new price:");scanf("%f",&a.Price);
gotoxy(15,13);
printf("Enter new rackno:");scanf("%d",&a.rackno);
gotoxy(15,14);
printf("The record is modified");
fseek(fp,ftell(fp)-sizeof(a),0);
fwrite(&a,sizeof(a),1,fp);
fclose(fp);
c=1;
}
if(c==0)
86
{
gotoxy(15,9);
printf("No record found");
}
}
gotoxy(15,16);
printf("Modify another Record?(Y/N)");
fflush(stdin);
another=getch() ;
}
returnfunc();
}
void returnfunc(void)
{
{
printf(" Press ENTER to return to main menu");
}
a:
if(getch()==13) //allow only use of enter
mainmenu();
else
goto a;
}
int getdata()
{
int t;
gotoxy(20,3);printf("Enter the Information Below");
gotoxy(20,4);printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(20,5);
printf("\xB2");gotoxy(46,5);printf("\xB2");
gotoxy(20,6);
printf("\xB2");gotoxy(46,6);printf("\xB2");
gotoxy(20,7);
printf("\xB2");gotoxy(46,7);printf("\xB2");
gotoxy(20,8);
printf("\xB2");gotoxy(46,8);printf("\xB2");
gotoxy(20,9);
printf("\xB2");gotoxy(46,9);printf("\xB2");
gotoxy(20,10);
printf("\xB2");gotoxy(46,10);printf("\xB2");
gotoxy(20,11);
printf("\xB2");gotoxy(46,11);printf("\xB2");
gotoxy(20,12);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\
xB2\xB2\xB2\xB2");
gotoxy(21,5);
printf("Category:");
gotoxy(31,5);
printf("%s",catagories[s-1]);
gotoxy(21,6);
printf("Book ID:\t");
gotoxy(30,6);
scanf("%d",&t);
87
if(checkid(t) == 0)
{
gotoxy(21,13);
printf("\aThe book id already exists\a");
getch();
mainmenu();
return 0;
}
a.id=t;
gotoxy(21,7);
printf("Book Name:");gotoxy(33,7);
scanf("%s",a.name);
gotoxy(21,8);
printf("Author:");gotoxy(30,8);
scanf("%s",a.Author);
gotoxy(21,9);
printf("Quantity:");gotoxy(31,9);
scanf("%d",&a.quantity);
gotoxy(21,10);
printf("Price:");gotoxy(28,10);
scanf("%f",&a.Price);
gotoxy(21,11);
printf("Rack No:");gotoxy(30,11);
scanf("%d",&a.rackno);
return 1;
}
int checkid(int t) //check whether the book is exist in library or not
{
rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
if(a.id==t)
return 0; //returns 0 if book exits
return 1; //return 1 if it not
}
int t(void) //for time
{
time_t t;
time(&t);
printf("Date and time:%s\n",ctime(&t));
return 0 ;
}
/*void show_mouse(void) //show inactive mouse pointer in programme
{
union REGS in,out;
in.x.ax = 0x1;
int86(0x33,&in,&out);
}*/
void Password(void) //for password option
{
system("cls");
char d[25]="Password Protected";
char ch,pass[10];
88
int i=0,j;
//textbackground(WHITE);
//textcolor(RED);
gotoxy(10,4);
for(j=0;j<20;j++)
{
Sleep(50);
printf("*");
}
for(j=0;j<20;j++)
{
Sleep(50);
printf("%c",d[j]);
}
for(j=0;j<20;j++)
{
Sleep(50);
printf("*");
}
gotoxy(10,10);
gotoxy(15,7);
printf("Enter Password:");
while(ch!=13)
{
ch=getch();
if(ch!=13
&&ch!=8){putch('*');
pass[i] = ch;
i++;
}
}
pass[i] = '\0';
if(strcmp(pass,password)==0)
{
gotoxy(15,9);
//textcolor(BLINK);
printf("Password match");
gotoxy(17,10);
printf("Press any key to countinue.....");
getch();
mainmenu();
}
else
{
gotoxy(15,16);
printf("\aWarning!! Incorrect Password");
getch();
Password();
}
}
void issuerecord() //display issued book's information
89
{
system("cls");
gotoxy(10,8);
printf("The Book has taken by Mr. %s",a.stname);
gotoxy(10,9);
printf("Issued Date:%d-%d-%d",a.issued.dd,a.issued.mm,a.issued.yy);
gotoxy(10,10);
printf("Returning Date:%d-%d-%d",a.duedate.dd,a.duedate.mm,a.duedate.yy);
}
/*void loaderanim()
{
int loader;
system("cls");
gotoxy(20,10);
printf("LOADING....... ");
printf("\n\n");
gotoxy(22,11);
for(loader=1;loader<20;loader++)
{
Sleep(100);printf("%c",219);}
}*/
//End of program
90
Additional C Programs for exercise
1. Program to find ther owsum and column sum of a given matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
intmat[10][10];
inti,j;
intm,n;
int sumrow,sumcol;
printf("\nTOFINDTHEROWSUMANDCOLUMNSUMOFAGIVENMATRIX:");
printf("\n ");
printf("\nEnter the order of matrix:");
scanf("%d%d",&m,&n);
printf("\nEnter elementsofa matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
}
printf("\n\nOUTPUT:");
printf("\n ");
for(i=0;i<m;i++)
{
sumrow=0;
for(j=0;j<n;j++)
sumrow=sumrow+mat[i][j];
printf("\nTHESUMOF%dROWIS%d",i+1,sumrow);
}
printf("\n ");
for(j=0;j<n;j++)
{
sumcol=0;
for(i=0;i<m;i++)
sumcol=sumcol+mat[i][j];
printf("\nTHESUMOF%dCOLUMNIS%d",j+1,sumcol);
}
printf("\n ");
getch();
}
91
2. Program to read a string and print the first two characters of each word in thestring.
#include<stdio.h>
#include<conio.h>
void main()
{
chars[100];
int i, l;
printf(“Enterastring”);
gets(s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]!=””&&s[i]=””)
{
printf(“%c%c”,s[i],s[i+1]);
I =i+2;
while(s[i]!=” “)
i++;
}
}
getch();
}
3. Programtoprintcurrentsystemdate.
#include<stdio.h>
#include<conio.h>
#include <dos.h>
intmain()
{
structdated;
getdate(&d);
printf("Currentsystemdateis%d/%d/%d",d.da_day,d.da_mon,d.da_year);
getch();
return0;
}.
4. ProgramtocalculateStandardDeviation.
#include<stdio.h>
#include<math.h>
floatstandard_deviation(floatdata[],intn);
intmain()
{
intn,i;
floatdata[100];
printf("Enternumberof datas(shouldbelessthan100):");
92
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; ++i)
scanf("%f",&data[i]);
printf("\n");
printf("StandardDeviation=%.2f",standard_deviation(data,n));return0;
}
floatstandard_deviation(floatdata[],intn)
{
floatmean=0.0,
sum_deviation=0.0;
inti;
for(i=0;i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0;i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);returnsqrt(sum_deviation/n);
}
#include<stdio.h>
intpower(int n1,intn2);
intmain()
{
intbase, exp;
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&exp);
printf("%d^%d = %d", base, exp, power(base, exp));
return0;
}
intpower(intbase,intexp)
{
if ( exp!=1 )
return(base*power(base,exp-1));
}
5. ProgramtofindtheASCIIvalueofaCharacter.
#include<stdio.h>
intmain()
{
charc;
printf("Enteracharacter:");
93
scanf("%c",&c);
/* Takes a character from user */
printf("ASCIIvalueof%c= %d",c,c);
return0;
}
6. Programtofindbiggestoffournumbers byusingternary operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,big; printf(“enter valuea”);
scanf(“%d”,&a);
printf(“enterthevalueofb”);
scanf(“%d”,&b);
printf(“enterthevalueofc”);
scanf(“%d”,&c);
printf(“enterthevalueofd”);
scanf(“%d”,&d);
big=(a>b)?(a>c)?(a>d)?a:d:(c>d)?c:d:(b>c)?(b>d)?b:d:(c>d)?c:d;
printf(“Biggestofthegiven4numbersis%d”,big);
getch();
}
94
Viva Questions and Answers
1- What is C language?
C is a mid-level and procedural programming language. The Procedural programming language is also known
as the structured programming language is a technique in which large programs are broken down into smaller modules,
and each module uses structured code. This technique minimizes error and misinterpretation.
Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts
Portable: C is highly portable means that once the program is written can be run on any machine with little or
no modifications.
Mid Level: C is a mid-level programming language as it combines the low- level language with the features of
the high-level language.
Structured: C is a structured language as the C program is broken into parts.
Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
Memory Management: C provides an inbuilt memory function that saves the memory and improves the
efficiency of our program.
Extensible: C is an extensible language as it can adopt new features in the future.
scanf(): The scanf() function is used to take input from the user.
95
A variable which is declared as static is known as a static variable. The static variable retains its value between
multiple function calls.
Static variables are used because the scope of the static variable is available in the entire program. So, we can
access a static variable anywhere in the program.
The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is
assigned.
The static variable is used as a common value which is shared by all the methods.
The static variable is initialized only once in the memory heap to reduce the memory usage.
C functions are used to avoid the rewriting the same code again and again in our program.
C functions can be called any number of times from any place of our program.
When a program is divided into functions, then any part of our program can easily be tracked.
C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C
program more understandable.
Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string
is an array of characters which is terminated by a null character ‘\0’.
Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution
of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data
structures like tree, graph, linked list, etc.
If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by
the first pointer while the first pointer still points to that memory location, the first pointer will be known as a
dangling pointer. This problem is known as a dangling pointer problem.
Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer
points to the deallocated memory.
96
20- What is static memory allocation?
In case of static memory allocation, memory is allocated at compile time, and memory can’t be increased while
executing the program. It is used in the array.
The lifetime of a variable in static memory is the lifetime of a program.
The static memory is allocated using static keyword.
The static memory is implemented using stacks or heap.
The pointer is required to access the variable present in the static memory.
The static memory is faster than dynamic memory.
In static memory, more memory space is required to store the variable.
In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while
executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.
An allocation or deallocation of memory is done at the execution time of a program.
No dynamic pointers are required to access the memory.
The dynamic memory is implemented using data segments.
Less memory space is required to store the variable.
97