0% found this document useful (0 votes)
4 views25 pages

Ex 7

The document outlines several C programming exercises, including sorting numbers using pass by reference, solving the Towers of Hanoi problem using recursion, generating salary slips for employees using structures and pointers, computing internal marks for students, managing a telephone directory, and creating a banking application. Each exercise includes a clear aim, algorithm, program code, and output results demonstrating the functionality of the programs. The examples illustrate various programming concepts such as arrays, structures, functions, and file handling.

Uploaded by

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

Ex 7

The document outlines several C programming exercises, including sorting numbers using pass by reference, solving the Towers of Hanoi problem using recursion, generating salary slips for employees using structures and pointers, computing internal marks for students, managing a telephone directory, and creating a banking application. Each exercise includes a clear aim, algorithm, program code, and output results demonstrating the functionality of the programs. The examples illustrate various programming concepts such as arrays, structures, functions, and file handling.

Uploaded by

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

EX:6 SORTING USING PASS BY REFERENCE

AIM:

To write a C Program to Sort the list of numbers using pass by reference.

ALGORITHM:

1. Start

2. Declare variables and create an array

3. Read the Input for number of elements and each element.

4. Develop a function to sort the array by passing reference

5. Compare the elements in each pass till all the elements are sorted.

6. Display the output of the sorted elements

7. Stop

PROGRAM:

#include <stdio.h>

#include <conio.h>

void main()

int n,a[100],i;

void sortarray(int*,int);

clrscr();

printf("\nEnter the Number of Elements in an array : ");

scanf("%d",&n);

printf("\nEnter the Array elements\n");

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

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

sortarray(a,n);

printf("\nAfter Sorting... \n");

for(i=0;i<n;i++)
printf("%d\n",a[i]);

getch();

void sortarray(int* arr,int num)

int i,j,temp;

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

for(j=i+1;j<num;j++)

if(arr[i] > arr[j])

{ temp=arr[i];

arr[i] = arr[j];arr[j] = temp;

OUTPUT:

Enter the Number of Elements in an array : 5

Enter the Array elements

56

43

90

12

89

After Sorting...

12

43

56

89

90
RESULT:

Thus a C Program Sorting using pass by reference was executed and the output was obtained.

EX:7 TOWERS OF HANOI USING RECURSION

ALGORITHM:

1. Start

2. Declare variables

3. Read the Input for number of discs.

4. Check the condition for each transfer of discs using recursion.

5. Display the output of the each move

6. Stop

PROGRAM:

#include <stdio.h>

void towers(int, char, char, char);

void main()

int num;

clrscr();

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');

getch();

void towers(int num, char frompeg, char topeg, char auxpeg)

{
if (num == 1)

printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

return;

towers(num - 1, frompeg, auxpeg, topeg);

printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

towers(num - 1, auxpeg, topeg, frompeg);

OUTPUT:

Enter the number of disks : 3

The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C

Move disk 2 from peg A to peg B

Move disk 1 from peg C to peg B

Move disk 3 from peg A to peg C

Move disk 1 from peg B to peg A

Move disk 2 from peg B to peg C

Move disk 1 from peg A to peg C

RESULT:

Thus a C Program tower of Hanoi using recursion was executed and the output was obtained.

EX:8A GENERATE SALARY SLIP OF EMPLOYEES USING STRUCTURES AND


POINTERS

ALGORITHM:

1. Start
2. Declare variables

3. Read the number of employees .

4. Read allowances, deductions and basic for each employee.

5. Calculate net pay= (basic+ allowances)-deductions

6. Display the output of the Pay slip calculations for each employee.

7. Stop

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct emp

int empno ;

char name[10] ;

int bpay, allow, ded, npay ;

} e[10] ;

void main()

int i, n ;

clrscr();

printf("Enter the number of employees : ") ;

scanf("%d", &n) ;

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

printf("\nEnter the employee number : ") ;

scanf("%d", &e[i].empno) ;

printf("\nEnter the name : ") ;

scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;

scanf("%d %d %d", &e[i].bpay,&e[i].allow, &e[i].ded) ;

e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;

printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;

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

printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].allow,


e[i].ded,e[i].npay) ;

getch();

OUTPUT:

Enter the number of employees : 2

Enter the employee number : 101

Enter the name : WILSON

Enter the basic pay, allowances & deductions : 36000

8000

4000

Enter the employee number : 102

Enter the name : VINITH


Enter the basic pay, allowances & deductions : 35000

6000

3500

Emp. No. Name Bpay Allow Ded Npay

101 WILSON 36000 8000 4000 40000

102 VINITH 35000 6000 3500 37500

RESULT:

Thus a C Program Salary slip of employees was executed and the output was obtained.

EX:8B INTERNAL MARKS OF STUDENTS

AIM:

To write a C Program to Compute internal marks of students for five different subjects

using structures and functions.

ALGORITHM:

1. Start

2. Declare variables

3. Read the number of students

4. Read the student mark details

5. Calculate internal mark by i=total of three test marks / 3 for each subject per student

6. Display the output of the calculations for all the students

7. Stop

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct mark_sheet
{

char name[20];

long int rollno;

int marks[10];

int total;

float average;

char rem[10];

char cl[20];

}students[100];

int main()

int a,b,n,flag=1;

char ch;

clrscr();

printf("How many students : \n");

scanf("%d",&n);

for(a=1;a<=n;++a)

clrscr();

printf("\n\nEnter the details of %d students : ", n-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);

students[a].total=0;

for(b=1;b<=5;++b)

{
printf("\n\nEnter the mark of subject-%d : ", b);

scanf("%d", &students[a].marks[b]);

students[a].total += students[a].marks[b];

if(students[a].marks[b]<40)

flag=0;

students[a].average =(float)(students[a].total)/5.0;

if((students[a].average>=75)&&(flag==1))

strcpy(students[a].cl,"Distinction");

else if((students[a].average>=60)&&(flag==1))

strcpy(students[a].cl,"First Class");

else if((students[a].average>=50)&&(flag==1))

strcpy(students[a].cl,"Second Class");

else if((students[a].average>=40)&&(flag==1))

strcpy(students[a].cl,"Third Class");

if(flag==1)

strcpy(students[a].rem,"Pass");

else strcpy(students[a].rem,"Fail");

flag=1;

for(a=1;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=1;b<=5;b++)

printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);

printf("\n\n \n");

printf("\n\n Totl Marks : %d", students[a].total);

printf("\t\t\t\t Average Marks : %5.2f", students[a].average);

printf("\n\n Class : %s", students[a].cl);

printf("\t\t\t\t\t Status : %s", students[a].rem);

printf("\n\n\n\t\t\t\t Press Y for continue . . . ");

ch =getche();

if((ch=="y")||(ch=="Y"))

continue;

return(0);

OUTPUT:

How many students :

Enter the details of 2 students :

Enter student 1 Name : joan

Enter student 1 Roll Number : 34

Enter the mark of subject-1 : 89

Enter the mark of subject-2 : 90

Enter the mark of subject-3 : 97


Enter the mark of subject-4 : 68

Enter the mark of subject-5 : 90

Enter the details of 1 students :

Enter student 2 Name : kavi

Enter student 2 Roll Number : 35

Enter the mark of subject-1 : 78

Enter the mark of subject-2 : 90

Enter the mark of subject-3 : 93

Enter the mark of subject-4 : 94

Enter the mark of subject-5 : 93

Mark Sheet

Name of Student joan Roll No : 34

Subject 1 : 89

Subject 2 : 90

Subject 3 : 97

Subject 4 : 68

Subject 5 : 90
Totl Marks : 434 Average Marks : 86.80

Class : Distinction Status : Pass

Mark Sheet

Name of Student kavi Roll No : 35

Subject 1 : 78

Subject 2 : 90

Subject 3 : 93

Subject 4 : 94

Subject 5 : 93

Totl Marks : 448 Average Marks : 89.60

Class : Distinction Status : Pass


RESULT:

Thus a C Program for Internal marks of students was executed and the output was obtained

Ex.NO:9 TELEPHONE DIRECTORY

AIM:
To write a C Program to add, delete ,display ,Search and exit options for telephone details

of an individual into a telephone directory using random access file.

ALGORITHM:

Step1: Start.

Step 2: Declare variables, File pointer and phonebook structures.

Step3: create menu options.

step4: Read the option .

Step5: Develop procedures for each option.

Step6: Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.

Step7: Display the message for operations performed.

Step8: Stop

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;

char FileName[256];

FILE *pRead;

FILE *pWrite;

int main (void)

phone *phonebook;

phonebook = (phone*) malloc(sizeof(phone)*100);

int iSelection = 0;

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);

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)

{
M

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);

EX.NO:10 BANKING APPLICATION

PROGRAM:

#include <stdio.h>

#include <stdlib.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

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....");

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

You might also like