0% found this document useful (0 votes)
15 views45 pages

Record Printing

The document contains several C programming examples demonstrating various functionalities such as calculating the area and circumference of a circle, finding the largest number among three, adding two integers, converting Celsius to Fahrenheit, and checking if a number is odd or even. It also includes programs for determining leap years, performing basic calculator operations, checking for Armstrong numbers, computing body mass index, reversing strings while preserving special characters, and converting decimal numbers to binary, octal, and hexadecimal. Each program is accompanied by its output and a note confirming successful execution.

Uploaded by

Yoganandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views45 pages

Record Printing

The document contains several C programming examples demonstrating various functionalities such as calculating the area and circumference of a circle, finding the largest number among three, adding two integers, converting Celsius to Fahrenheit, and checking if a number is odd or even. It also includes programs for determining leap years, performing basic calculator operations, checking for Armstrong numbers, computing body mass index, reversing strings while preserving special characters, and converting decimal numbers to binary, octal, and hexadecimal. Each program is accompanied by its output and a note confirming successful execution.

Uploaded by

Yoganandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 45

1. a. To find area and circumference of circle.

Program:

#include <stdio.h>
void main()
{
float r,area,circum;
clrscr();
printf("\n Enter the radius of circle:");
scanf(“%f”,&r);
area=3.14*r*r
circum=2*3.14*r
printf(" Area of Circle: %f \n Circumference of a Circle: %f\n”,area,circum);
return 0;

Output:

Enter the radius of circle: 2.34

Area of Circle: 17.193384

Circumference of a Circle: 14.695200

Result:

Thus the above c program was executed successfully.


1.b. To find Largest Number among three numbers

Program:

#include <stdio.h>
void main()
{
int a,b,c,big;
clrscr();
printf("\n Enter three numbers:");
scanf(“%d %d %d”,&a, &b, &c);
if(a>b && a>c)
big=a;
elseif(b>a && b>c)
big=b;
else
big=c;
printf(" Largest number is = %d”,big);
return 0;
}

Output:

Enter three numbers: 10 20 30


Largest number is = 30

Result:
Thus the above c program was executed successfully.
1.c. To add two integers

Program:

#include <stdio.h>
int main()
{
int a,b,c;
printf(“ Enter two numbers:”);
scanf(“ %d %d ”,&a,&b);
c=a+b;
printf(“ %d+%d=%d”,a,b,c);
return 0;
}

Output:

Enter two numbers: 15 10


15+10= 25

Result:
Thus the above c program was executed successfully.
1.d. To convert the temperature Celsius into Fahrenheit

Program:

#include <stdio.h>
void main()
{
float temp_c,temp_f;
clrscr();
printf("\n Enter the value of Temperature in Celsious:");
scanf(“%f”,&temp_c);
temp_f=(1.8*temp_c)+32;
printf(" The value of Temperature in Fahrenheit is: %f",temp_f);
getch();
}

Output:
Enter the value of Temperature in Celsious:30
The value of Temperature in Fahrenheit is: 86.00000

Result:
Thus the above c program was executed successfully
2.a)To find whether a given number is odd or even

Program:

#include <stdio.h>
int main()
{
int number;
printf("Enter the number: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
}
Output
Enter the number : 7

7 is odd.

Enter the number : 8

8 is even.

Result:
Thus the above c program was executed successfully
2.b)To find whether a given number is positive or negative or zero

Program:
#include<stdio.h>
int main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n<0)
printf("Number is negative");
else if(n>0)
printf("Number is positive");
else
printf("Number is equal to zero");
return 0;
}
Output
Enter a number: 8
Number is positive
Enter a number: -11
Number is negative
Enter a number: 0
Number is equal to zero

Result:
Thus the above c program was executed successfully
3. 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)

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

Enter the Year : 1900


The Given year 1900 is Not a Leap Year

Result:
Thus the above c program was executed successfully
4. Design a calculator to perform the operations, namely, addition, subtraction, multiplication,
division and square of a number.

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,result,square1, square 2,ch;
float divide;
printf(“Enter two integers:”);
scanf(“%d%d”,&a,&b);
printf(“1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square:”);
printf(“\n Enter the Choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
{
result=a+b;
printf(“Sum= %d\n”,result);
break;
}
case 2:
{
result=a-b;
printf(“Difference= %d\n”,result);
break;
}
case 3:
{
result=a*b;
printf(“Multiplication= %d\n”,result);
break;
}
case 4:
{
result=a/ (float)b;
printf(“Division= %2f\n”,result);
break;
}
case 5:
{
square1=a*a;
printf(“Square= %d\n”, square1);
square2=b*b;
printf(“Second Square number= %d\n”, square2);
break;
}
OUTPUT
Enter two integers : 2 3
1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square
Enter the Choice: 1
Sum=5
Enter two integers : 3 2
1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square
Enter the Choice: 2
Difference=1
Enter two integers : 2 3
1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square
Enter the Choice: 3
Multiplication= 6
Enter two integers : 10 2
1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square
Enter the Choice: 4
Division =5.00
Enter two integers : 2 3
1.Add, 2.Subtract ,3. Multiply, 4.Divide 5. Square
Enter the Choice: 5
Square= 4
Second Square number= 9

Result: Thus the above c program was executed successfully


5. Check whether a given number is Armstrong number or not?

Program:

#include <stdio.h>
#include <conio.h>
void main()
{
int n, originalnumber, r, result=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
originalnumber=n;
while(originalnumber!=0)
{
r= originalnumber %10;
result=result+(r*r*r);
originalnumber = originalnumber /10;
}
if(result==n)
printf("%d is an Armstrong number",n);
else
printf("%d is not an Armstrong number",n);
getch();
}

Output:
Enter a positive integer:153
153 is an Armstrong number

Result: Thus the above c program was executed successfully


6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, find the sum of weights based on the
following conditions.
5 if it is a perfect cube.
4 if it is a multiple of 4 and divisible by 6.
3 if it is a prime number.
Sort the numbers based on the weight in the increasing order as shown below <10, its weight>,<36,
its weight><89, its weight>

Program:

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

Output:

<8,5><11,3><216,9><24,4><34,0>

Result:
Thus the above c program was executed successfully
7. /* Get a Height of Different Persons and find how many of them are are above average */

Program:
#include <stdio.h>
#include <conio.h>
void main()
{
float height;
printf("Enter the Height)in centimeters):”);
scanf("%f",&height);
if (height<150.0)
printf("Dwarf \n”);
else if ((height>=150.0) && (height<=165.0))
printf("Average Height \n”);
else if ((height>165.0) && (height<=195.0))
printf("Taller \n”);
else
printf("Abnormal Height \n”)
}
Output:
Enter the Height (in centimeters): 170
Taller
Enter the Height (in centimeters): 143
Dwarf
Enter the Height (in centimeters): 165
Average Height

Result:
Thus the above c program was executed successfully
8. Populate a two dimensional array with height and weight of persons and compute the
body mass index of the individuals.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float person_hw[100][2];
float bmi;
printf("\nEnter the number of person");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the person's height and weight[%d]=",i+1);
scanf("%f%f",&person_hw[i][0],&person_hw[i][1]);
bmi=person_hw[i][1]/(person_hw[i][0]*person_hw[i][0]);
printf("\n body mass index of person %d = %f", i+1, bmi);
}
getch();
}

Output:
Enter the number of person3
Enter the person's height and weight[1]=1.50 55
body mass index of person 1 = 24.444445
Enter the person's height and weight[2]=1.75 61
body mass index of person 2 = 19.918367
Enter the person's height and weight[3]=1.61 80
body mass index of person 3 = 30.863007

Result: Thus the above c program was executed successfully


9. Given a string “a$bcd./fg” find its reverse without changing the position of special
characters. (Example input: a@gh%;j and output: j@hg%;a)

Program:
#include<stdio.h>
#include<math.h>
Bool isalphabet(char x)
{
return ((x>=’A’&& x<=’Z’) | |(x>=’a’&& x<=’z’));
}
void reversestring(char a[ ])
{
int r = strlen(s)-1,l=0;
while (l<r)
{
if(!isAlphabet(s[l]))
l++;
else if (!isAlphabet(s[r]))
r--;
else
{
swap(s[l],s[r]);
l++;
r--;
}
}
}
int main()
{
char s[100];
printf(“Enter the input string and special character:”);
scanf(“%s”,&s);
printf(“Input string:”,s);
reversestring(s);
printf(“Output string:”,s);
return 0;
}

Output:

Enter the input string and special character: a@gh%;j

Input string: a@gh%;j

Output string: j@hg%;a

Result:
Thus the above c program was executed successfully
10. Convert the given decimal number into binary, octal and hexadecimal numbers using
user defined functions.

Program:

#include<stdio.h>
#include<conio.h>
long decimaltobinary(long n);
long decimaltooctal(long n);
long decimaltohexadecimal(long n);
void main()
{
long decimal;
printf("Enter a decimal number");
scanf("%ld",&decimal);
printf("\n Binary number of %ld is %ld", decimal,decimaltobinary(decimal));
printf("\n octal number of %ld is %ld",decimal,decimaltooctal(decimal));
printf("\n hexadecimal number is %ld is %ld ",decimal, decimaltohexadecimal(decimal));
getch();
}
long decimaltobinary(long n)
{
int remainder;
long binary = 0, i=1;
while(n!=0)
{
remainder=n%2;
n=n/2;
binary=binary+(remainder*i);
i=i*10;
}
return binary;
}
long decimaltooctal(long n)
{
int remainder;
long octal=0,i=1;
while(n!=0)
{
remainder=n%8;
n=n/8;
octal=octal+(remainder*i);
i=i*10;
}
return octal;
}
long decimaltohexadecimal(long n)
{
int remainder;
long hexadecimal=0,i=1;
while(n!=0)
{
remainder=n%16;
n=n/16;
hexadecimal=hexadecimal+(remainder*i);
i=i*10;
}
return hexadecimal;
}
Output:
Enter a decimal number102
Binary number of 102 is 1100110
octal number of 102 is 146
hexadecimal number is 102 is 66

Result: Thus the above c program was executed successfully


11. From a given paragraph perform the following using built-in functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word

Program:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void replace (char *, char *, char *);
int main()
{
char choice,str[200];
int i, words;
char s_string[200], r_string[200];
clrscr();
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;
while(str[i]!= '\0')
{
if(str[i]==' '||str[i]=='\n'||str[i]=='\t')
{
words++;
}
i++;
}
printf("\nTotal number of words = %d", words);
break;
case '2' :
i = 0;
while(str[i] != '\0')
{
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':
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();
}
while(choice!='4');
return 0;
}
void replace(char * str, char * s_string, char * r_string)
{
char buffer[200];
char * ch;
if(!(ch=strstr(str,s_string)))
return;
strncpy(buffer, str, ch-str);
buffer[ch-str] = 0;
sprintf(buffer+(ch -str), "%s%s", r_string, ch + strlen(s_string));
str[0] = 0;
strcpy(str, buffer);
// return replace(str, s_string, r_string);
}
Output:

Enter a paragraph: Hello students. How are you? Let us study.


Capitalize paragraph is : HELLO students, HOW are you? LET us study.
Number of words in given paragraph are: 8
Enter the WORD to be replaces : students
Enter the WORD the students to be replaces : boys
New paragraph is: HELLO boys. HOW are you? LET us study.

Result:
Thus the above c program was executed successfully
12. C program for Tower of Hanoi using Recursion

Program:

#include <stdio.h>
void towers(int, char, char, char);
int main()
{
intnum;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char source, char destination, char intermediate)
{
if (num == 1)
{
printf("\n Move disk 1 source %c destination %c", source, destination);
return;
}
towers(num - 1, source, intermediate, destination);
printf("\n Move disk %d source %c destination %c", num, source, destination);
towers(num - 1, intermediate, destination, source);
}
Output:
Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :
Move disk 1 source A destination C
Move disk 2 source A destination B
Move disk 1 source C destination B
Move disk 3 source A destination C
Move disk 1 source B destination A
Move disk 2 source B destination C
Move disk 1 source A destination C

Result: Thus the above c program was executed successfully


13. Sort the list of numbers using pass by reference.
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];
arr[j] = temp;
}
}
Output:
Sorted List is : 12,12,33,35,43,45,67,78,88,90

Result: Thus the above c program was executed successfully


14. Generate salary slip of employees using structures and pointers.
Program:

#include<stdio.h>
#include<conio.h>
#include"stdlib.h"
struct emp
{
int empno ;
char name[10],answer;
int bpay,allow,ded,npay ;
struct emp *next;
};
void main()
{
int i,n=0;
int more_data = 1;
char answer;
struct emp e[20],*current_ptr, *head_ptr;
clrscr() ;
head_ptr = (struct emp *) malloc (sizeof(struct emp));
current_ptr = head_ptr;
while (more_data)
{
{
printf("\nEnter the employee number : ") ;
scanf("%d",&current_ptr->empno) ;
printf("\nEnter the name : ") ;
scanf("%s",&current_ptr->name) ;
printf("\nEnter the basic pay: ") ;
scanf("%d",&current_ptr ->bpay);
printf("\nenter the hra:");
scanf("%d",&current_ptr ->allow);
printf("\nenter the deduction: ");
scanf("%d",&current_ptr->ded);
e[i].npay = e[i].bpay + e[i].allow - e[i].ded;
n++;
printf("Would you like to add another employee? (y/n): ");
scanf("%s",answer);
if (answer!= 'Y')
{
current_ptr->next=(struct emp *) NULL;
more_data=0;
}
else
{
current_ptr->next = (struct emp *) malloc (sizeof(struct emp));
current_ptr = current_ptr->next;
}
}
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
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() ;
}
Output:

Result: Thus the above c program was executed successfully


15. Compute internal marks of students for five different subjects using structures and
functions.
Program:

#include<stdio.h>
#include<conio.h>
struct stud
{
char name[20];
long int rollno;
int marks[5,3];
int i[5];
}students[10];

void calcinternal(int);
int main()
{
int a,b,j,n,total;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=0;a<n;++a)
{
clrscr();
printf("\n\nEnter the details of %d student : ", a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
total=0;
for(b=0;b<=4;++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=0;a<n;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=0;b<5;b++){
printf("\n\n\t Subject %d internal \t\t :\t %d", b+1, students[a].i[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
getch();
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a){
for(b=0;b<5;b++){

for(j=0;j<=2;++j){
total+= students[a].marks[b,j];
}
students[a].i[b]=total/3;
}
}
}
OUTPUT:

Result: Thus the above program was executed successfully.


16. Insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
}phone;
void AddEntry(phone*);
void DeleteEntry(phone*);
void PrintEntry(phone* );
void SearchForNumber(phone* );
int counter = 0;
int iSelection=0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
int main (void)
{
phone*phonebook;
phonebook=((phone*)malloc(sizeof(phone)*100));
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
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);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
return 0;
}
} while (iSelection <= 4);
return 0;
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName,
phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[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("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if ( pRead == NULL)
{
perror("The following error occurred: ");
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);
}
}
fclose(pRead);
}
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
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:

Result: Thus the above program was executed successfully.


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

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MINBAL 500
struct Bank_Account
{
char no[10];
char name[20];
char balance[15];
};
struct Bank_Account acc;
void main()
{
long int pos1,pos2,pos;
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
{
clrscr();
fflush(stdin);
printf("1. Add a New Account Holder\n");
printf("2. Display\n");
printf("3. Deposit or Withdraw\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum Balance\
n");
printf("5. Stop\n");
printf("Enter your choice : ");
choice=getchar();
switch(choice)
{
case '1' :
fflush(stdin);
fp=fopen("acc.dat","a");
printf("\nEnter the Account Number : ");
gets(acc.no);
printf("\nEnter the Account Holder Name : ");
gets(acc.name);
printf("\nEnter the Initial Amount to deposit : ");
gets(acc.balance);
fseek(fp,0,2);
fwrite(&acc,sizeof(acc),1,fp);
fclose(fp);
break;
case '2' :
fp=fopen("acc.dat","r");
if(fp==NULL)
printf("\nFile is Empty");
else
{
printf("\nA/c Number\tA/c Holder Name Balance\n");
while(fread(&acc,sizeof(acc),1,fp)==1)
printf("%-10s\t\t%-20s\t%s\n",acc.no,acc.name,acc.balance);
fclose(fp);
}
break;
case '3' :
fflush(stdin);
flag=0;
fp=fopen("acc.dat","r+");
printf("\nEnter the Account Number : ");
gets(ano);
for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftell(fp))
{
if(strcmp(acc.no,ano)==0)
{
printf("\nEnter the Type 1 for deposit & 2 for withdraw : ");
scanf("%d",&type);
printf("\nYour Current Balance is : %s",acc.balance);
printf("\nEnter the Amount to transact : ");
fflush(stdin);
gets(amt);
if(type==1)
bal = atof(acc.balance) + atof(amt);
else
{
bal = atof(acc.balance) - atof(amt);
if(bal<0)
{
printf("\nRs.%s Not available in your A/c\n",amt);
flag=2;
break;
}
}
flag++;
break;
}
}
if(flag==1)
{
pos2=ftell(fp);
pos = pos2-pos1;
fseek(fp,-pos,1);
sprintf(amt,"%.2f",bal);
strcpy(acc.balance,amt);
fwrite(&acc,sizeof(acc),1,fp);
}
else if(flag==0)
printf("\nA/c Number Not exits... Check it again");
fclose(fp);
break;
case '4' :
fp=fopen("acc.dat","r");
flag=0;
while(fread(&acc,sizeof(acc),1,fp)==1)
{
bal = atof(acc.balance);
if(bal<MINBAL)
flag++;
}
printf("\nThe Number of Account Holder whose Balance less than the Minimum
Balance :%d",flag);
fclose(fp);
break;
case '5' :
fclose(fp);
exit(0);
}
printf("\nPress any key to continue....");
getch();
} while (choice!='5');
}

OUTPUT:

Result: Thus the above program was executed successfully.

You might also like