CS8261 C Programming Lab Record Manual
CS8261 C Programming Lab Record Manual
Ex. No. 1
I/O STATEMENTS AND EXPRESSIONS
PROBLEM STATEMENT:
Programs 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>
voidmain()
{
intage, 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();
}
TEST CASES:
RESULT:
Thus the C program to perform input and output operations using built-in printf()
and scanf() function has been done and verified successfully.
Ex. No. 2
DECISION-MAKING CONSTRUCTS
PROBLEM STATEMENT:
Programs using decision-making constructs.
AIM:
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>
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
TEST CASES:
RESULT:
Thus the C program to check for positive and negative numbers using decision
making construct has been completed and verified successfully.
Ex. No. 3
FIND WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT
PROBLEM STATEMENT:
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)
AIM:
T write a C program to check whether the given year is a LEAP year or not.
ALGORITHM:
STEP 3:Read the input from the user and store it in a variable.
STEP 5:Print the result as “It is a leap year” and goto step 7.
PROGRAM:
#include<stdio.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%100==0)
n%400==0 ? printf("The given year %d is a leap year",n):
printf("The given year %d is a non-leap year",n);
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();
}
TEST CASES:
RESULT:
Thus the program to check for leap year has been written in C successfully.
Ex. No. 4
MENU DRIVEN CALCULATOR
PROBLEM STATEMENT:
Design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.
AIM:
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 program\n");
printf("**********************\n");
printf("Enter the choice\n");
printf(" 1) Addition\n 2) Subraction\n 3) 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("The difference of two numbers %d and %d
is:%d",a,b,c);
break;
case 3:
printf("Enter two numbers to be multiplied:");
scanf("%d %d",&a,&b);
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;
default:
printf("Invalid option");
}
getch();
}
TEST CASES:
RESULT:
Thus the menu-drive arithmetic calculator has been implemented in C and verified
successfully.
ALGORITHM:
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 four digit 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);
getch();
}
TEST CASES:
RESULT:
Thus the program to check for an Armstrong number has been written in C and the
output was verified successfully.
Ex. No. 6
SORT THE NUMBERS BASED ON THE WEIGHT
PROBLEM STATEMENT:
Given a set of numbers like <10, 36, 54, 89, 12, 27>, find 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>
AIM:
ALGORITHM:
STEP 2:Declare all required global and local variables and initialize them either inside or
outside the functions.
int getweight(int n) =>to assign weight for each input value based the
given condition.
STEP 4:Sort the numbers and their weights in ascending order based on weight.
PROGRAM:
#include<stdio.h>
#include<math.h>
int prime(int n)
{
int flag = 1;
for(int i=2; i<(n/2); i++)
{
if(n%i == 0)
flag = 0;
}
if(flag)
return 1;
else
return 0;
}
void main()
{
int nums[15], ws[15];
int i,j,t,n;
clrscr();
printf("Enter the number of numbers");
scanf("%d",&n);
printf("\nEnter the numbers");
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
for(i=0;i<n;i++)
ws[i]=getWeight(nums[i]);
printf("\nBefore sorting:\n");
for(i=0;i<n;i++)
printf("<%d,%d>\t",nums[i],ws[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(ws[j]>ws[j+1])
{
t=ws[j+1];
ws[j+1]=ws[j];
ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
}
printf("\nSorted:\n");
for(i=0;i<n;i++)
printf("<%d,%d>\t",nums[i],ws[i]);
getch();
}
TEST CASES:
RESULT:
Thus the C program to calculate weight for a set numbers based on a series of
conditions and sort them based on the assigned weights has been implemented and the
result was tested.
Ex. No. 7
AVERAGE HEIGHTS OF PERSONS
PROBLEM STATEMENT:
Populate an array with height of persons and find how many persons are above the
average height.
AIM:
To write a C program to populate a 1D array with height of persons and find how
many persons are above the average height value.
ALGORITHM:
STEP 2:Include all required header files and declare all required variables.
intaboveavg(…) => to find no. of persons are above the average height
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>
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 no of persons\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("The no of persons above the avg height,%d
are:%d\n",avg,count);
getch();
}
int aboveavg(int a[],int n,int avg)
{
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;
}
TEST CASES:
RESULT:
Thus the program to count how many persons are above the average height value
has been implemented in C and verified.
Ex. No. 8
BODY MASS INDEX OF THE INDIVIDUALS
PROBLEM STATEMENT:
Populate a two dimensional array with height and weight of persons and compute
the Body Mass Index of the individuals.
AIM:
To write a C program to compute the Body Mass Index (BMI) value for the given
number of persons with their height and weight values.
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;
clrscr();
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",&h);
height[i] = h/100;
printf("Weight (in kg): ");
scanf("%f",&weight[i]);
}
for(i=0;i<n;i++)
{
bmi[i]=weight[i]/(height[i] * height[i]);
}
printf("Overweight");
else if(bmi[i]>=18.5)
printf("Normal");
else if(bmi[i]>=16)
printf("Underweight");
else
printf("Severely Underweight");
}
getch();
}
TEST CASES:
RESULT:
Thus the C program to calculate Body Mass Index (BMI) has been implemented
and tested sucessfully.
To write a C program to reverse the given string without changing the position of
special characters.
ALGORITHM:
STEP 2: Include all required header files and Declare all required variables.
STEP 3: Define an user-defined function isAlphabet(), to check whether the given symbol
is an alphabet or not. If it is an alphabet, then return TRUE otherwise return
FALSE>
STEP 4: Define an another user-defined function reverse(), to retrieve the characters from
the input string one-by-one and reverse them based on the result of above
mentioned function.
STEP 5: Get a String input from the user and pass it to reverse() function.
PROGRAM:
#include <stdio.h>
#include <string.h>
int isAlphabet(char x)
{
return ( (x >= 'A' && x <= 'Z') || (x >= 'a' && x <=
'z') );
}
void swap(char *a, char *b)
{
char *t;
t = a;
a = b;
b = t;
}
void reverse(char str[])
{
int r = strlen(str) - 1, l = 0;
while (l < r)
{
if (!isAlphabet(str[l]))
l++;
else if(!isAlphabet(str[r]))
r--;
else
{
char temp;
temp = str[l];
str[l] = str[r];
str[r] = temp;
swap(&str[l], &str[r]);
l++;
r--;
}
}
}
void main()
{
char str[15];
printf("Input string: ");
scanf("%s", str);
reverse(str);
printf("Output string: %s", str);;
getch();
}
TEST CASES:
RESULT:
Thus the program for reversing a string without changing the special characters'
position has been implemented and tested.
Ex. No. 10
NUMBER SYSTEM CONVERSION
PROBLEM STATEMENT:
Convert the given decimal number into binary, octal and hexadecimal numbers
using user defined functions.
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 bin(int n);
void hex(int n);
void oct(int n);
void main()
{
int n;
clrscr();
printf("******************************\n");
printf("Number system - Conversion\n");
printf("******************************\n");
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 equivalent 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;
}
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("\nTheocatal equivalent of the given number %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]);
}
}
TEST CASES:
RESULT:
Thus the C program to convert the given number from one number system to
another number system has been implemented and tested successfully.
Ex. No. 11
STRING OPERATIONS USING BUILT-IN FUNCTIONS
PROBLEM STATEMENT:
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.
AIM:
ALGORITHM:
STEP 3: Display the menu list and get the user's option and based on the input, perform
the string operations.
STEP 6: Count the number of words in the given sentence and display the result.
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 <string.h>
#include <stdlib.h>
void main()
{
char s1[30],s2[30];
printf("\nString Operations using built-in functions\n");
printf("Enter the string : ");
gets(s1);
word_count(s1);
capitalize(s1);
printf("Enter the substring\n");
fflush(stdin);
gets(s2);
printf("Enter the string to replace the substring\n");
fflush(stdin);
gets(s3);
replace(s1,s2,s3);
getch();
}
{
wc++;
}
i++;
}
printf("Total number of words in the string is : %d\n", wc);
}
void capitalize(char s[])
{
int i=0;
char new[20];
new[i] = toupper(s[0]);
i++;
while(s[i] != '\0')
{
if(s[i]==' ' || s[i]=='\n' || s[i]=='\t')
{
new[i] = s[i];
new[i+1] = toupper(s[i+1]);
i+=2;
}
else
{
new[i] = s[i];
i++;
}
}
printf("After capitalizing: %s", new);
}
p = strstr(s1, s2);
if (p)
{
strncpy(buf, s1, p-s1);
buf[p-s1] = 0;
sprintf(buf+(p-s1), "%s%s", s3, p + strlen(s2));
s1[0] = 0;
strcpy(s1, buf);
printf("%s", s1);
}
else
printf("String not found\n");
}
TEST CASES:
RESULT:
Thus the program to perform string operations has been implemented in C using
built-in functions.
Ex. No. 12
TOWER OF HANOI USING RECURSION
PROBLEM STATEMENT:
. Solve 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:
#include <stdio.h>
void main()
{
int num;
clrscr();
printf("*******************\n");
printf("Towers of Hanoi\n");
printf("*******************\n");
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();
}
TEST CASES:
RESULT:
Thus the program for Tower of Hanoi problem has been implemented in C using
recursion technique.
PROBLEM STATEMENT:
. Sort the list of numbers using pass by reference.
AIM:
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>
void main()
{
int A[50],n,i=0;
clrscr();
printf("***************************\n");
printf("Sorting an array elements\n");
printf("***************************\n");
printf("Enter the no of elements in the array\n");
scanf("%d",&n);
printf("Enter the element in the array:\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("The sorted elements in the array are:\n");
sort(A,n);
getch();
}
TEST CASES:
RESULT:
Thus the program for sorting numbers in ascending order using function in pass-by-
reference method has been written and the output was verified.
Ex. No. 14
EMPLOYEES SALARY SLIP GENERATION
PROBLEM STATEMENT:
Generate salary slip of employees using structures and pointers.
AIM:
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>
struct emp
{
int empno ;
char name[10];
int bpay, allow, ded, npay ;
struct emp *next;
} ;
void main()
{
int i,n=0;
char answer;
int more_data = 1;
struct emp *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, allowances & deductions : ")
;
scanf("%d %d %d", & current_ptr ->bpay, & current_ptr -
>allow,
& 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 eme *) 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() ;
}
TEST CASES:
RESULT:
Thus the Employee's salary slip generation program has been implemented using
structures and verified.
Ex. No. 15
INTERNAL MARKS OF STUDENTS
PROBLEM STATEMENT:
Compute internal marks of students for 5 subjects using structures and functions.
AIM:
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();
}
}
return(0);
}
void calcinternal(int n)
{
int a,b,j,total;
for(a=1;a<=n;++a)
{
for(b=0;b<5;b++)
{
total=0;
for(j=0;j<=2;++j)
{
total += students[a].marks[b][j];
}
students[a].i[b]=total/3;
}
}
}
TEST CASES:
RESULT:
Thus the program for computing the internal marks of students has been written in
C using structures and functions and the output was tested.
Ex. No. 16
CREATION OF TELEPHONE DIRECTORY
PROBLEM STATEMENT:
Insert, update, delete and append telephone details of an individual or a company
into a telephone directory using random access file.
AIM:
ALGORITHM:
STEP 2: Declare all required variables, file pointers and phone book structure.
STEP 3: Display the menu options and get an input option from the user.
STEP 4: Based on the user input, call the corresponding function to add, delete, search to
display the contact details.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];
FILE *pRead;
FILE *pWrite;
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
exit(1);
}
}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);
}
}
}
printf("That contact was not found, please try again.");
}
}
}
fclose(pRead);
}
TEST CASES:
RESULT:
Thus the implementation of telephone directory system has been implemented and
verified successfully.
Ex. No. 17
IMPLEMENTATION OF BANKING APPLICATION
PROBLEM STATEMENT:
Count the number of account holders whose balance is less than the minimum
balance using sequential access file.
AIM:
STEP 2: Include necessary header files and declare all required variables.
STEP 3: Display the list of options to the user as a menu and get the user input.
STEP 4: Based the user input, validate the values given by the user and do the calculation.
STEP 6: Repeat the steps from 3 to 5 until user exits from the program.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct Bank_Account
{
char no[10];
char name[20];
char balance[15];
};
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=ftel
l(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');
}
TEST CASES:
RESULT:
Thus the implementation of Banking System has been implemented and verified
successfully.
Ex. No. 18
IMPLEMENTAION OF RAILWAY RESERVATION SYSTEM
PROBLEM STATEMENT:
Create a Railway reservation system with the following modules:
Booking
Availability checking
Cancellation
Prepare chart
AIM:
ALGORITHM:
STEP 2: Include necessary header files and declare all required variables.
i. Booking
ii. Availability checking
iii. Cancellation
iv. Prepare chart
STEP 4: Get an input from the user.
STEP 5: Based the user input, validate the values given by the user and do the calculation.
STEP 7: Repeat the steps from 3 to 5 until user exits from the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int first=5,second=5,third=5;
struct node
{
int ticketno;
int phoneno;
char name[100];
char address[100];
}s[15];
int i=0;
void booking()
{
printf("enter your details");
printf("\nname:");
scanf("%s",s[i].name);
printf("\nphonenumber:");
scanf("%d",&s[i].phoneno);
printf("\naddress:");
scanf("%s",s[i].address);
void availability()
{
int c;
printf("availability cheking");
printf("\n1.first class\n2.second class\n3.third
class\n");
printf("enter the option");
scanf("%d",&c);
switch(c)
{
case 1:
if(first>0)
{
printf("seat available\n");
first--;
}
else
printf("seat not available");
break;
case 2:
if(second>0)
{
printf("seat available\n");
second--;
}
else
printf("seat not available");
break;
case 3:
if(third>0)
{
printf("seat available\n");
third--;
}
else
printf("seat not available");
break;
default:
break;
}
}
void cancel()
{
int c;
printf("cancel\n");
printf("which class you want to cancel");
printf("\n1.first class\n2.second class\n3.third
class\n");
cheking\n3.cancel\n4.Chart \n
5. Exit\nenter your option:");
scanf("%d",&n);
switch(n)
{
case 1:
booking();
break;
case 2:
availability();
break;
case 3:
cancel();
break;
case 4:
chart();
break;
case 5:
printf(“\n Thank you visit again!”);
getch();
exit(0);
default:
break;
}
}
getch();
}
TEST CASES:
RESULT: