0% found this document useful (0 votes)
73 views

20ESPL101 Programming in C Lab Manual

The document contains the code for several C programs involving arrays and input/output operations. The first program stores personal details like name, address, date of birth, mobile number and age in variables and displays them. The second program takes these details as input from the user and displays them. The third program checks if a given year is a leap year or not. The fourth program finds the factorial of a given number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

20ESPL101 Programming in C Lab Manual

The document contains the code for several C programs involving arrays and input/output operations. The first program stores personal details like name, address, date of birth, mobile number and age in variables and displays them. The second program takes these details as input from the user and displays them. The third program checks if a given year is a leap year or not. The fourth program finds the factorial of a given number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 91

EX N O : 3 a

PROGRAM USING I/O STATEMENTS AND EXPRESSIONS


DATE:

AIM:

i] Program to display your personal details

ALGORITHM:

Step1: Start
Step2:Declare and initialize the variables for name, address, date of birth, mobile
number and age
Step3:Display name, address, date of birth, mobile number and age
Step4:End
PROGRAM:

#include<stdio.h>
void main()
{
charname[20]="SAIRAM";
char address[80]= "west tambharam,chennai";
int date=20;
int month=10;
int year=1990;
int mobile=987456321;
intage=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\nADDRESS:%s",address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");
}
OUTPUT:

NAME:SAIRAM
ADDRESS:westtambaram,chennai
DOB:20:10:1990
MOBILE
NUMBER:987456321AGE:25
RESULT

Thus the C Program to display the personal details has been executed and the output
wasverified.
EX N O : 3 b
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE:

AIM:

ii] Program to get the user details and display it.

ALGORITHM:

Step1: Start
Step2:Declare the variables for name, address, date, month, year, mobile number,
age.
Step3:Read values of name, address, date, month, year, mobile number, age from
the user.
Step4:Displayname, address, date, month, year, mobile number, age.
Step5:End
PROGRAM:

#include<stdio.h>
#include<conio.h
>#include<string.
h>int main()
{
Char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\nENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/
%d",&date,&month,&year);printf("\n
ENTER YOUR AGE=");
scanf("%d",&age);
printf("\nENTER YOUR GENDER (MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld", &mobile);
printf("\n=====================");
printf("\n NAME: %s", name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
return0;
}
OUTPUT:

ENTER YOUR NAME:=karthikeyan

ENTER YOUR ADDRESS=westtambharam,chennai.

ENTER YOUR date/month/year=03/12/1992

ENTER YOUR AGE=28

ENTERYOURGENDER(MALE/FEMALE)=MALE

ENTER YOUR MOBILENUMBER=987654321

=====================
NAME:karthikeyan
ADDRESS:westtambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILENUMBER:987654321
========================
RESULT:

Thus the C Program to read and display the userdetails has been executed and the
output was verified.
EX N O : 2 a
PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS

DATE:

AIM:

ii]Program to check biggest of three numbers

ALGORITHM:

Step1:Start

Step 2:Read three numbers A,B & C

Step3:IfA>B,thengotostep6

Step 4:If B>C, then print B & go to step

8Step5:printC is greatest & goto step8

Step6:IfA>C,then printA is greatest & gotostep8

Step7:PrintC is greatest

Step8:end
PROGRAM:

#include <stdio.h>
Void main()
{
Int A,B,C;
Printf ("Enter 3 integer number\
n");
scanf ("%d", &A);
scanf ("%d", &B);
scanf("%d",&C);
if(A>B)
{
if(A>C)
{
printf("%d istheGreatestNumber\n",A);
}
else
{
printf("%disthegreatestNumber\n",C);
}
}
else
{
if(B>C){

printf("%disthegreatestNumber\n",B);
}
else{
printf("%disthegreatestNumber\n",C);
}
}
}
OUTPUT:

Enter three numbers:


-4.5
3.9
5.6
5.60 is the largest number.
RESULT:

Thus the C Program to display biggest of two numbers has been executed and the
output was verified .
EXN O : 2b PROGRAM TO CHECK WHETHER THE ENTERED
CHARACTER IS VOWEL OR NOT (USESWITCHCASE)
DATE:

AIM:

Program to check whether the entered character is vowel or not (Useswitchcase)

ALGORITHM:

Step1: Start
Step2:Declare and initialize the variables
Step3:Get the input from the user and compare with each cases

Step 4: if match found, print vowel otherwise print consonant

Step5:End
PROGRAM:

#include<stdio.h>
#include<conio.h
>intmain()
{
charch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
switch(ch)
{
case'A':
case'E':
case'I':
case'O':
case'U':
case'a':
case'e':
case'i':
case'o':
case'u':
printf("%c is a VOWEL.\n",ch);

break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}

return0;
}
OUTPUT:

Enter a character

Eis avowel

Enter a character

X is a consonant

Enter a character

+ is not an alphabet
RESULT:

Thus the C Program check whether the entered character is vowel or not (Use
switchcase) has been executed and the output was verified.

EX NO:1 a PROGRAM TO FIND WHETHER THE GIVEN YEAR


IS LEAP YEAR OR NOT
DATE:

AIM:

To write a C Program to find whether the given year is leapyear or not.

ALGORITHM:

Step1:Start

Step2:Take integer variable year

Step3:Check if year is divisible by 400 then DISPLAY" is a leapyear"

Step4:Check if year is not divisible by100 and divisible by4 then DISPLAY"is a leap
year"

Step5:Otherwise,DISPLAY"is not a leap year"

Step6:Stop
PROGRAM:

#include<stdio.h>
#include <conio.h>
voidmain()
{
intyear;
printf("Enter a year :\n");
scanf("%d",&year);
if((year% 400)==0)
printf("%d is a leapyear \n",year);
else
if((year%100)!=0&&(year%4)==0)
printf("%d is a leapyear\n",year);
else
printf("%d is not a leapyear \n",year);
}
OUTPUT:

Enter a year:
2000
2000 is a leapyear

Enterayear:
1900
1900 is not a leapyear
RESULT:
Thus the C Program to find whether the given year is leap year or not has been
executed successfully and the output was verified.
EX NO:1 b
PROGRAM TO FIND FACTORIAL OF A GIVEN
NUMBER
DATE:

AIM:

To find factorial of a given number

ALGORITHM:

Step 1. Start the program

Step2.Get the number

Step 3. If the number < 0 print “Error for finding a factorial”

Step4. Else Initialize variables factorial←1i←1

Step 5. Readvalueofn

Step6. Repeat the steps until i=n

6.1:factorial←factorial,

i←i+1

Step 7. Display factorial

Step 8. Stop the program


PROGRAM:
#include<stdio.h>
int main()

int i,fact=1,number;
printf("Enter anumber:”);
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;

printf("Factorial of %d is : %d,number,fact);
return0;
}
OUTPUT:

Enter an number: 5
Factorial of 5 is 120
EX N O : 4 b
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 2D ARRAYS
DATE:

AIM:

To display array elements using 2D arrays

ALGORITHM:

Step1:Starttheprogram

Step2: Gettheelementsofthe array

Step 3 : Display the array elements

Step4:Stoptheprogram
PROGRAM:

#include<stdio.h>

int main(){

/* 2D array declaration*/

int disp[2][3];

/*Counter variables for the loop*/

int i,j;

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

{for(j=0;j<3;j++){

printf("Enter value for disp[%d][%d]:",i,j);

scanf("%d",&disp[i][j]);

//Displaying array elements

Printf("Two Dimensional array elements:\

n");

for(i=0;i<2;i++){

for(j=0;j<3;j++) {

printf("%d",disp[i][j]);

if(j==2){

printf("\n");

}
return0;

OUTPUT:

Enter value for disp[0][0]:1

Enter value for disp[0][1]:2

Enter value for disp[0][2]:3

Enter value for disp[1][0]:4

Enter value for disp[1][1]:5

Enter value for disp[1][2]:6

Two Dimensional array elements:

123

456
EX N O : 4 a
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 1D ARRAY
DATE:

AIM:
To display array elements using 1Darray

ALGORITHM:

Step1:Start the program

Step2: Get the elements of the array

Step 3 : Display the array elements

Step4:Stop the program


PROGRAM:

#include<stdio.h>

int main()

int i=0;

int marks[5]={20,30,40,50,60};

for(i=0; i<5; i++) {

printf("%d\n”,marks[i]);

return0;

}
OUTPUT:

20

30

40

50

60
RESULT:

Thus the C Program to find the factorial of a given number has been successfully
executed and verified.
RESULT:

Thus the C Program to display the array elements of the 2D array has been executed
and the result was verified
RESULT:

Thus the C Program to display the array elements of the 1D array has been executed
and the result was verified
33

EX N O : 5 a
PROGRAM TO FIND THE LENGTH OF STRING
DATE:

AIM:

To find the length of the given string

ALGORITHM:

Step 1 : Start the program

Step2: Get the string

Step 3: Find the length of the string

Step4:Display the length of the string

Step5:Stop the program


34
PROGRAM:

(i)UsingLibraryFunction

#include <stdio.h>
#include <string.h>
int main()
{
char a[100];

int length;
printf("\n Enter a string to calculate its length=");
gets(a);
length=strlen(a);
printf("\nLength of the string = %d\n", length);

return0;
}
(i) WithoutUsingLibraryFunction
#include <stdio.h>

#include<string.h>

int main()

{
char i=0;
a[100];

int length;
printf("\nEnter a string to calculate its length=");
scanf(“%s”,str);
while(string1[i] !='\0')
{i++;
}
length=i;
printf (“\n Length of the string = %d\n",length);

return 0;
}
OUTPUT:

Enter a string to calculate its length = Introduction

Length of the string: 12

RESULT:

Thus the C Program to find the length of the string has been executed and
verified.
EXN O : 6 a
PROGRAM TO PERFORM SWAPPING USING FUNCTIONS
DATE:

AIM:

To perform swapping using functions

ALGORITHM:

Step1. Start the program


Step2. Declare and get the two integer variables a and b.

Step3. call the swap() function in swap definition use the


temporary variable and assign temp =ab=temp
Step4. Print the a and b values

Step5. Stop the program


PROGRAM:

#include<stdio.h>

voidmain()

void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();

void swap(inta,int b)

int temp;
temp=a;
a=b;
b=temp
;
printf("after swapping the value for a&b is:%d%d",a,b);
}
OUTPUT:

Enter the value of a&b:3478

After swapping the value for a&b is78,34


RESULT:
Thus the C Program to swap two numbers using functions has been executed and
verified
EXN O : 6 b
PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN
DATE: TWO INTERVALS USING FUNCTIONS

AIM:

To display all prime numbers between two intervals using functions

ALGORITHM:

Step1: Start the Program

Step2:Get the intervals

Step3:Find and Display the prime numbers ie.,the numbers that are divisible by1and
itself between the intervals

Step4:Stop the Program


PROGRAM:

#include<stdio.h>

/* Function declarations */

int isPrime(intnum);

Void printPrimes(intlowerLimit,intupperLimit);

int main()

int lowerLimit, upperLimit;

printf("Enter the lower and upperlimit to list primes:");

scanf("%d%d",&lowerLimit,&upperLimit);

/* Call function to print all primes between the given range*/

printPrimes(lowerLimit,upperLimit);

return0;

/*Printallprimenumbersbetweenlowerlimitand upperlimit*/

Void printPrimes(int lowerLimit,int upperLimit)

printf("All prime number between %d to %d are: ", lowerLimit, upperLimit);

while(lowerLimit<=upperLimit)

/*Printifcurrentnumber isprime*/

if(isPrime(lowerLimit))

{
printf("%d,",lowerLimit);

lowerLimit++;

/*Checkwhether anumberisprimeornot*/

/*Returns 1 if the number is prime otherwise 0*/

int is Prime(intnum)

inti;

for(i=2;i<=num/2;i++)

/*Ifthenumberisdivisiblebyanynumber*/

/*otherthan1andselfthenitisnotprime*/

if(num%i==0)

return0;

return1;

}
OUTPUT:

Enter the lower and upper limit to list primes:

1100

All prime number between 1 100 are 2,3,5,7,11,13, 17,19, 23,29,31, 37,41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
RESULT:

Thus the C Program to find the prime numbers between two intervals has been
executed and verified.
EX N O : 7
PROGRAM TO REVERSE A SENTENCE USING RECURSION
DATE:

AIM:

To reverse a sentence using recursion

ALGORITHM:

Step1: Start
Step2:Declare the function reverse

Step3: Call the reverse function


Step 4: Get the sentence from the user and reverse it recursively
Step5: Stop the execution.
PROGRAM:

#include<stdio.h>
void reverseSentence();
int main(){
printf("Enterasentence:");
reverseSentence();
return0;
}

Void reverseSentence()
{char c;
scanf("%c",&c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorpawesome
Awesome program
RESULT
Thus the C Program to reverse a sentence has been executed and verified
PROGRAM TO PERFORM ADDITION OPERATION USING
EXN O : 8 a POINTER
DATE:

AIM:
To add two numbers using pointers and display the result
ALGORITHM:
Step1:Start the program
Step2:Declare two variables x , y and pointer variable p and q
Step3:Assign the address of x and y to p and q
Step4:Assign the sum of x and y to variable sum
Step5:Stop the program
PROGRAM:
#include<stdioh>
int main()
{
int first,second,*p,*q,sum;
printf(“Enter two integers to add\n”);
scanf(“%d%d”,&first,&second);
p=&first;
q=&second;
sum=*p + *q;
srintf(“Sum of entered numbers=”%d\n”,sum);
return 0;
}
OUTPUT:
Enter two integers to add
12
13
Sum of entered numbers=25
RESULT:
Thus the c program to add two numbers using pointer concept has been executed and
verified successfully.
EX.NO:8b

PROGRAM TO SET A POINTER VALUES TO AN


DATE: ARRAY OF INTEGERS

AIM:
To set a pointer values to the corresponding array of integers
ALGORITHM:
Step1:Start the program
Step2:Declare and set an array of five integers
Step3:Declare an array of five pointers to integers
Step4:Set a pointer to point a corresponding integer
Step5:Print the values of integers pointed to by the pointers
Step6:Stop the program
PROGRAM:
#include<stdio.h>
const int ARRAY_SIZE=5;
int main()
{
int array_of_integers[]={5,10,20,40,80};
int i,*array_of_pointers[ARRAY_SIZE];
for(i=0;i<ARRAY_SIZE;i++)
{
array_of_pointers[i]=&array_of_integers[i];
}
for(i=0;i<ARRAY_SIZE;i++)
{
printf(“array_of_integers[%d]=%d\n”,i,*array_of_pointers[i]);
}
return 0;
}
OUTPUT
array_of_integers[0]=5
array_of_integers[1]=10
array_of_integers[2]=20
array_of_integers[3]=40
array_of_integers[4]=80
RESULT:
Thus the c program to set a pointer values to the corresponding array of integer has been
executed and verified successfully.
EX.NO:5b

PROGRAM TO CONCATENATE TWO STRINGS


DATE:

AIM:

To concatenate two strings

ALGORITHM:

Step1:Start
Step2:Get the two Strings to be concatenated.
Step3: Declare a new String to store the concatenated String.
Step4: Insert the firststring in the new string.
Step5:Insert the second string in the new string.
Step6:Print the concatenated string.
Step7:Stop
PROGRAM:

#include <stdio.h>
#include <string.h>
Int main()
{
char destination[] = "Hello ";

char source[]="World!";
printf("Concatenated String: %s\n", strcat(destination,source));
return0;
}
OUTPUT:

Concatenated String:HelloWorld!
RESULT:

Thus the C Program to concatenate two strings has been executed and the result was
verified
EXN O : 8 B

PROGRAM TO FIND THE FREQUENCY OF A CHARACTER


IN A STRING
DATE:

AIM:

To find the frequency of a character in a string.

ALGORITHM:

Step 1 : Start the program

Step2:Get the string

Step3 :Get the character for which frequency needs to be found

Step4: Display the frequency

Step5:Stop the program


PROGRAM:

#include <stdio.h>
int main(){
char str[1000], ch;
int count=0;

printf("Enter a string: ");


fgets(str,sizeof(str),stdin);

printf("Enter a character to find its frequency: ");


scanf("%c",&ch);
for(inti=0;str[i]!='\0';++i)
{
if(ch==str[i])
++count;
}

printf("Frequency of %c = %d", ch, count);


return0;
}
OUTPUT:

Enter a string: This website is

awesome.

Enter a character to find its frequency: e

Frequency of e=4
RESULT:

Thus the C Program to find the frequency of a character in a string has been executed
and verified.
EX N O : 9 a
PROGRAM TO STORE STUDENT INFORMATION IN STRUCTURE
DATE: AND DISPLAY IT

AIM:

To store student information in structure and display it

ALGORITHM:

Step1:Start
Step2:Read student details like name,mark1,2,3

Step3: Calculate total,and average


Step 4: Display the grade
Step5:Stop
PROGRAM:
#include<stdio.h>
struct student
{
int roll_no, mark1, mark2, mark3, total;

float average;
char name[10],grade;
};
Void struct_funct_student(struct student
stu);

int main()
{
Struct student stud;

printf("\nRoll No.=");

scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");

scanf("%d",&stud.mark3);
struct_funct_student(stud)
;return0;
}
voidstruct_funct_student(struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;

stu.average=stu.total/3;
if(stu.average>= 90)
stu.grade='S';
else if(stu.average>= 80)
stu.grade='A';
else if(stu.average>= 70)
stu.grade='B';
else if(stu.average>= 60)
stu.grade='C';
else if(stu.average>= 50)
stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \
tGRADE\n");
printf("%d\t%s\t%d\t%f\t
%c",stu.roll_no,stu.name,stu.total,stu.average,stu.gr
ade);
}
OUTPUT:

RollNo.= 1

Name=a

Mark1=95

Mark2=94

Mark3=96

ROLLNO. NAME TOTAL AVG GRADE

1 A 285 95.000000 S
RESULT:

Thus the C Program to store and display student details using structures has been
executed and the result was verified.
EX N O : 9 b
PROGRAM TOREAD THE STUDENT DATA AND CALCULATE
DATE: THE TOTAL MARKS

AIM:

To read the student data and calculate the total marks

ALGORITHM:

Step1:Start the program

Step 2: Get the details of the 10 students in five

subjectsStep3:Calculate the total marks of each student

Step4:Calculate the student who got the highest total marks

Step5:Display the results

Step6:Stop the Program

PROGRAM:
#include<stdio.h>

struct student

int

sub1;

int

sub2;

int

sub3;

int

sub4;

int

sub5;

};

voidmain()
{

struct student s[10];

int i,total=0;
clrscr();

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

printf("\nEnterMarksinFiveSubjects=");

scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);

total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;

printf("\nTotalmarksofs[%d]Student=%d",i,total);

getch();

}
OUTPUT:

Enter Marks in Five Subjects

80 70 90 80 98

Total Marks of 1 student=83.6


RESULT:

Thus the C Program to print the student details has been executed and the result was
verified.
EXNO:10a

TELEPHONE DIRECTORY USING RANDOM ACCESS FILE


DATE:

AIM:
To insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.

ALGORITHM:
Step1:Create a random access file
Step2:call the respective procedure to insert, update, delete or append based on user choice
Step3: Access the random access file to make the necessary changes and save

PROGRAM
#include "stdio.h"
#include "string.h"
#include<stdlib.h>
#include<fcntl.h>
Struct dir
{
char name[20];
char number[10];
};

void insert(FILE *);


void update(FILE *);
void del(FILE *);
void display(FILE *);
voidsearch(FILE*);

int record = 0;
int main(void) {
intchoice=0;
FILE*fp=fopen("telephone.dat","rb+");
if (fp == NULL ) perror ("Error opening file");
while(choice!=6)
{
printf("\n1 insert\t 2 update\n");
printf("3delete\t4display\n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case3: del(fp);break;
case 4: display(fp);
break;
case 5: search(fp); break;
default:
}
}
fclose(fp);
return0;
}

Void insert(FILE*fp)
{

Struct dir contact,blank;


fseek( fp, -sizeof(structdir), SEEK_END );
fread(&blank, sizeof(structdir), 1, fp);
printf("Enter individual/company name: ");
scanf("%s",contact.name);
printf("Enter telephone number: ");
scanf("%s",contact.number);
fwrite(&contact,sizeof(structdir),1,fp);
}

Void update(FILE*fp)
{
char name[20], number[10];
int result;
struct dir contact, blank;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0&&strcmp(name,contact.name) == 0)
{
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number,number);
fseek(fp, -sizeof(structdir),
SEEK_CUR);fwrite(&contact,
sizeof(structdir), 1, fp);
printf("Updatedsuccessfully\n");
return;
}
}
printf("Recordnotfound\n");
}

Void del(FILE*fp)
{
char name[20], number[10];
intresult, record=0;
structdir contact, blank = {"", ""};
printf("Entername:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0&&strcmp(name,contact.name)==0)
{
fseek(fp, record*sizeof(structdir),
SEEK_SET);fwrite(&blank,sizeof(structdir),
1,fp);
printf("%d Deleted successfully\n", record-1);
return;
}
record++;
}
printf("notfoundin %drecords\n",record);
}
Void display(FILE*fp)
{
Struct dir
contact;
int result;
rewind(fp);
printf("\n\n Telephone directory\n");
printf("%20s %10s\n", "Name", "Number");
printf("*******************************\
n");while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result != 0 &&strlen(contact.name) > 0)
printf("%20s%10s\n",contact.name,contact.number);
}
printf("*******************************\n");
}
voidsearch(FILE*fp)
{
Struct dir contact;
int result;
char name[20];
rewind(fp);
printf("\nEnter name:");
scanf("%s",name);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0 &&strcmp(contact.name,name)== 0)
{
printf("\n%20s %10s\n",contact.name, contact.number);
return;
}
}
printf("Recordnotfound\n");
}

OUTPUT:

1insert 2update
3delete 4display
5search 6Exit
Enter choice:
4Telephonedirector
y
Name Number
******************************
*bb 11111
*******************************

1insert 2 update
3delete 4display
5search 6Exit
Enter choice:

5Entername:bb

bb 11111

1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:1
Enter individual/company
name:aaEntertelephonenumber:222
222

1insert 2 update
3delete 4display
5search 6Exit
Enter choice:
2Entername:aa
Enter number:
333333Updated
successfully
1insert 2 update
3delete 4display
5search 6
ExitEnterchoice:
Telephonedirectory
Name Number
******************************
*bb 11111
aa 333333
******************************
*1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:3

Entername:aa
1 Deleted successfully

1insert 2update
3delete 4display
5search 6Exit
Enterchoice:4

Telephonedirectory
Name Number
******************************
*bb 11111
*******************************

1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:6
RESULT:

Thus the C program To insert,update,delete and append telephone details of an


individual or a company into a telephone directory using random access file was successfully
written and executed.
EX N O : 1 0 . b
PROGRAM TO COUNT THE NUMBER OF ACCOUNT HOLDERS
WHOSE BALANCE IS LESS THAN THE MINIMUM BALANCE
DATE:
USING SEQUENTIAL ACCESS FILE

AIM:
To count the number of account holders whose balance is less than the minimum
Balance using sequential access file.

ALGORITHM:

Step1:Start the program


Step2:Read choice to insert records & count minimum balance account
1. If choice is1,then
 Open a datfile in write mode
 Read theNo. Of records
 Write the records into the file using fprintf() function
 Close the file
2. If Choice is 2,then
 Open the file in Read mode
 Read the records one by one using fscanf(0 function until reach the end of file.
 Check the account balance with min bal.
 If account balance is less than min balance, then display the account details
 Close the file
Step3:Stop the program
PROGRAM:

#include <stdio.h>

Void insert();
Void count();

int main(void)
{
int choice=0;
while(choice!=3)
{
printf("\n1insertrecords\n");
printf("2 Count min balance holders\n");
printf("3Exit\n");
printf("Enter choice:");
scanf("%d",&choice);

switch(choice)
{
case 1: insert();
break;
case2:count();
break;
}
}
}

Void insert()
{
Unsigned int
account,i;
Char name[30];
doublebalance;
FILE*cfPtr;

if((cfPtr=fopen("clients.dat", "w"))==NULL)
{
puts("File couldnotbeopened");
}
else{
int records,i=0;
printf("Enter the No. of records ");
scanf("%d",&records);
while(i<records)
{
printf("Entertheaccount, name,andbalance.");
scanf("%d%29s%lf",&account,name,&balance);
fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);
i++;
}
fclose(cfPtr);
}
}

Void count()
{
Unsigned int
account;
char name[30];
doublebalance;
floatminBal = 5000.00;
int count=0;
FILE*cfPtr;
if((cfPtr=fopen("clients.dat","r"))==NULL)
printf("Filecouldnot be opened");
else

{
printf("%-10s%-13s%s\n","Account","Name","Balance");
fscanf(cfPtr,"%d%29s%lf",&account,name,&balance);

while(!feof(cfPtr))
{
if(balance<minBal)
{
printf("%-10d%-13s%7.2f\
n",account,name,balance);count++;
}
fscanf(cfPtr,"%d%29s%lf",&account,name,&balance);
}

fclose(cfPtr);
printf("Thenumberofaccountholderswhosebalanceislessthantheminimumbalance:
%d",count);
}
}
OUTPUT:

1 Insert records
2 Count min balance
holders
3 Exit
Enter choice:1
Enter the No.ofrecords2
Enter the account, name, and balance.1001 A 10000
Entertheaccount,name,andbalance.1002 B 300

1 Insert records
2 Count min balance
holders
3 Exit
Enterchoice:2
AccountName
Balance1002 B
300.00
The number of account holders whose balanceis less than the minimum balance:
1 insertrecords
2 Count min balance holders
3 Exit
Enterchoice:
*
RESULT:

Thus the C Program to count the number of account holders whose balance is less
than the minimum balance using sequential access file

You might also like