Ex 7
Ex 7
AIM:
ALGORITHM:
1. Start
5. Compare the elements in each pass till all the elements are sorted.
7. Stop
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
int n,a[100],i;
void sortarray(int*,int);
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
{ temp=arr[i];
OUTPUT:
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.
ALGORITHM:
1. Start
2. Declare variables
6. Stop
PROGRAM:
#include <stdio.h>
void main()
int num;
clrscr();
scanf("%d", &num);
getch();
{
if (num == 1)
return;
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
OUTPUT:
RESULT:
Thus a C Program tower of Hanoi using recursion was executed and the output was obtained.
ALGORITHM:
1. Start
2. Declare variables
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] ;
} e[10] ;
void main()
int i, n ;
clrscr();
scanf("%d", &n) ;
scanf("%d", &e[i].empno) ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
getch();
OUTPUT:
8000
4000
6000
3500
RESULT:
Thus a C Program Salary slip of employees was executed and the output was obtained.
AIM:
To write a C Program to Compute internal marks of students for five different subjects
ALGORITHM:
1. Start
2. Declare variables
5. Calculate internal mark by i=total of three test marks / 3 for each subject per student
7. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct mark_sheet
{
char name[20];
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();
scanf("%d",&n);
for(a=1;a<=n;++a)
clrscr();
scanf("%s", students[a].name);
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("\n ");
for(b=1;b<=5;b++)
printf("\n\n \n");
ch =getche();
if((ch=="y")||(ch=="Y"))
continue;
return(0);
OUTPUT:
Mark Sheet
Subject 1 : 89
Subject 2 : 90
Subject 3 : 97
Subject 4 : 68
Subject 5 : 90
Totl Marks : 434 Average Marks : 86.80
Mark Sheet
Subject 1 : 78
Subject 2 : 90
Subject 3 : 93
Subject 4 : 94
Subject 5 : 93
Thus a C Program for Internal marks of students was executed and the output was obtained
AIM:
To write a C Program to add, delete ,display ,Search and exit options for telephone details
ALGORITHM:
Step1: Start.
Step6: Call the procedure (Add, delete ,display ,Search and exit)for user chosen option.
Step8: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
phone *phonebook;
int iSelection = 0;
if (phonebook == NULL)
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(5)\tExit Phonebook");
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)
return 0;
if ( pWrite == NULL )
{
perror("The following error occurred ");
exit(EXIT_FAILURE);
else
counter++;
realloc(phonebook, sizeof(phone));
scanf("%s", phonebook[counter-1].FirstName);
scanf("%s", phonebook[counter-1].LastName);
scanf("%s", phonebook[counter-1].PhoneNumber);
fclose(pWrite);
{
M
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
--counter;
return;
int x = 0;
exit(EXIT_FAILURE);
else
printf("\n(%d)\n", x+1);
fclose(pRead);
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
scanf("%s", TempFirstName);
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
phonebook[x].LastName, phonebook[x].PhoneNumber);
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Bank_Account
char no[10];
char name[20];
char balance[15];
};
void main()
{
FILE *fp;
char *ano,*amt;
char choice;
int type,flag=0;
float bal;
do
fflush(stdin);
printf("2. Display\n");
printf("4. Number of Account Holder Whose Balance is less than the Minimum Balance\n");
printf("5. Stop\n");
choice=getchar();
switch(choice)
case '1' :
fflush(stdin);
fp=fopen("acc.dat","a");
gets(acc.no);
gets(acc.name);
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
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+");
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);
fflush(stdin);
gets(amt);
if(type==1)
else
if(bal<0)
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)
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);
} while (choice!='5');
}