[go: up one dir, main page]

0% found this document useful (0 votes)
140 views55 pages

CP Lab Manual-Program

The document describes a C program to sort numbers based on their weights according to certain conditions. The program takes numbers as input, calculates their weights (5 if a perfect cube, 4 if divisible by 4 and 6, 3 if prime), sorts the numbers based on increasing weight and displays the output. It uses nested for loops, if-else statements and functions like pow() and modulus operator to determine weights and sort the numbers accordingly.

Uploaded by

srividhya
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)
140 views55 pages

CP Lab Manual-Program

The document describes a C program to sort numbers based on their weights according to certain conditions. The program takes numbers as input, calculates their weights (5 if a perfect cube, 4 if divisible by 4 and 6, 3 if prime), sorts the numbers based on increasing weight and displays the output. It uses nested for loops, if-else statements and functions like pow() and modulus operator to determine weights and sort the numbers accordingly.

Uploaded by

srividhya
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/ 55

EX. No.

: 1a PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

DATE:

Aim:
To write a C Program to find area and circumference of the circle using I/O
statements and Expressions

Algorithm:
Step 1: Start
Step 2: Declare the variables Area, circum, pi, r

Step 3: Read the radius of the circle, r


Step 3: Calculate the area of the circle, Area=pi*r*r
Step 4: Calculate the circumference of the circle, circum=2*pi*r
Step 5: Display the results, Area, circum
Step 6: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float Area, circum, pi=3.14, r;

clrscr();
printf(“Enter the radius of the circle:”);
scanf(“%f”,&r);
Area=pi*r*r; circum=2*pi*r;
printf(“\nArea=%f\ncircumference=%f”,Area,circum);
getch();
}

Output:
Enter the radius of the
circle: 10 Area=314.20
Circumference=62.8

Result:
Thus a C Program to find area and circumference of the circle using I/O
statements and Expressions was executed and the output was verified.
EX.No.: 1b PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

DATE :

Aim:
To write a C Program to find simple interest using I/O statements and Expressions

Algorithm:
Step 1: Start
Step 2: Declare pamt, r, n, si=0;
Step 3: Calculate the simple interest using the expression si=((pamt*r*n)/100)
Step 4: Print si
Step 5: Stop

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float pamt,n,r,si;
clrscr();
printf(“Enter the value of pamt,n,r:”);
scanf(“\n%f\n%f\n%f”,&pamt,&r,&n);
si=((pamt*r*n)/100);
printf(“\nSimple interest=%f”,si);
getch();
}
Output:
Enter the value of
pamt,n,r: 2500
10
2
Simple interest: 500

Result:
Thus a C Program to find simple Interest using I/O statements and Expressions was
executed successfully and output was verified.
EX.No.: 2a PROGRAM USING DECISION-MAKING CONSTRUCTS

DATE:

Aim:
To write a C program to find greatest of three numbers using decision making
constructs.

Algorithm:
Step 1: Start
Step 2: Declare the variable a, b and c
Step 3: Read the variable a,b and c
Step 4: Check the condition
Step 4.1:if (a>b) and (a>c)
Display a is greater number
Step 4.2: elseif (b>c) and (b>a)
Display b is greater number
Step 4.3: else
Display c is greater number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n Enter the first number: ”);

scanf(“%d”,&a);
printf(“\n Enter the second number:”);
scanf(“%d”,&b);
printf(“\nEnter the third number: ”);
scanf(“%d”,&c);
if (a>b && a>c)
printf(“\n %d is greater number”,a);

else if (b>c && b>a)


printf(“\n %d is greater number”,b);
else
printf(“\n %d is greater number”,c);
getch();
}
Output:
Enter the first number :6

Enter the second number :8

Enter the third number: 10

10 is greater number

Result:
Thus a C Program to find greatest among three numbers using decision making
constructs was executed successfully and output was verified.
EX.No.:2b PROGRAM USING DECISION-MAKING CONSTRUCTS

DATE:

Aim:
To write a C program to find odd or even number using decision making constructs.

Algorithm:
Step 1: Start
Step 2: Declare the variable a
Step 3: Read the variable a
Step 4: Check if (a%2==0)
Step 4.1: if it is true
Step 4.2: Display a is even number
Step 4.3: else
Step 4.4: Display a is odd number
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“\n Enter the number a :”);
scanf(“%d”,&a);
if (a%2==0)
{
printf(“\n %d is even number”,a);

else

printf(“\n %d is odd number”,a);


}
}

Output:

Enter the number a :6


6 is even number

Result:
Thus the C program to find odd or even number using decision making constructs
program was successfully executed and output was verified.
EX. No.: 3 LEAP YEAR CHECKING

DATE:

Aim:

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

Algorithm:

Step 1: Start

Step 2: Declare variables

Step 3: Read the Input.

Step 4: Take a year as input and store it in the variable year.

Step 5: Using if, else statements ,

Step 5.1: Check whether a given year is divisible by 400.

Step 5.2: Check whether a given year is divisible by 100.

Step 5.3: Check whether a given year is divisible by 4.

Step 6: If the condition at step 5.1 becomes true, then print the output as “It is a leap
year”.

Step 7: If the condition at step 5.2 becomes true, then print the ouput as “It is not a
leap year”.

Step 8: If the condition at step 5.3 becomes true, then print the ouput as “It is a leap
year”.

Step 9: If neither of the condition becomes true, then the year is not a leap year and
print the same.

Step 10: Stop


Program:

#include<stdio.h>
#include<conio.h>
void main()

{
int year;

printf("Enter a year \n");

scanf("%d", &year);

if ((year % 400) == 0)

printf("%d is a leap year \n", year);

else if ((year % 100) == 0)

printf("%d is a not leap year \n", year);

else if ((year % 4) == 0)

printf("%d is a leap year \n", year);

else

printf("%d is not a leap year \n", year);

Output:

Enter a year 2012


2012 is a leap year

Result:

Thus a C Program for Leap year checking was executed successfully and the
output was verified.
EX. No.: 4 ARITHMETIC OPERATIONS

DATE:

Aim:
To write a C Program to Design a calculator to perform the operations, namely,
addition, subtraction, multiplication, division and square of a number.

Algorithm:
Step 1: Start
Step 2: Initialize n1 ,n2 , op, variables
Step 3: Display the menu with option add, sub, mul and div, square
Step 4: Use switch () statement.
Step 5: Display the respective result.
Step 6: Stop.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int
a,b,op;
printf(“\n Enter two number:”);
scanf(“%d%d”, &a,&b);
printf(“\n menu”);
printf(“\n 1.Addtion”);
printf(“\n 2.Subraction”);
printf(”\n 3.Multiplication”);
printf(“\n 4.Division”);
printf(“\n 5. Square”);
printf(“\nPlease select the operation”) ;
scanf(“%d”,&op);
switch(op)
{
case 1:
{
printf(“\n Addition is :%d”,a+b); brea

}
case 2::
{
printf(“\Subtraction is:%d”,a-b);
break;
}
case 3:
{
printf(“\n Multiplication is:%d,a*b);
break;
}
case 4:
{
printf(“\n Division is:%d”,a/b);
break;
}
case 5:
{
printf(“\nSquare of %d is:%d”, a,a*a);
printf(“\nSquare of %d is:%d”, b, b*b);
break;
}
default:
{
printf(“\n Invalid operation”);
}
getch();
}
Output:
Enter two number: 20,10
menu 1.Addtion
2.Subraction
3.Multiplication
4.Division
5. Square
Please select the operation 1
Addition is :30
Please select the operation 2
Subtraction is:10
Please select the operation 3
Multiplication is:200 Please
select the operation 4
Division is: 2
Please select the operation 5
Square of 20 is:400
Square of 10 is:100
Please select the operation 10
Invalid operation

Result:
Thus a C Program for Arithmetic operations was executed successfully and the
output was verified.
EX. No.: 5 ARMSTRONG NUMBER
DATE:

Aim:
To write a C program to check whether a given number is Armstrong or not.

Algorithm:

Step 1: Start
Step 2: Declare the variables and assign a=n
Step 3: Using while loop
Step 3.1: remainder =number%10
Step 3.2: sum=sum+(remainder*remainder*remainder)
Step 3.3: number=number/10
Step 4: Repeat step 3 until number>0
Step 5: if sum==a
Step 6: Display number is Armstrong
Step 7: else display number is not Armstrong
Step 8: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int n,r,a,s=0;
printf (“\n Enter a number :”);
scanf(“%d”,&n);
a=n;
while (n>0)
{
r=n%10;
s=s+(r*r*r); n=n/10;
}
if (a==s)
printf(“\n %d is an Armstrong number “,a)

else
printf(“\n %d is not an Armstrong number”, a);
}

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

Result:
Thus the C program to check whether a given number is Armstrong or not
was executed successfully and output was verified.
EX. No.: 6 SORT THE NUMBERS BASED ON THE WEIGHT

DATE:

Aim:
To write a C Program to perform the following:
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>

Algorithm:

Step1: Start
Step2: Declare variables
Step3: Read the number of elements.

Step4: Get the individual elements.


Step5: Calculate the weight for each element by the conditions
 5 if it is a perfect cube (pow)
 4 if it is a multiple of 4 and divisible by 6 (modulus operator)
 3 if it is a prime number(modulus operator)
Step 6: Display the output of the weight calculations after sorting.

Step7: Stop
Program:

#include<stdio.h>
#include<math.h>
void main()
{

int nArray[50],wArray[50],nelem,i,j,t;
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]);
for(i=0; i<nelem; i++)
{
wArray[i] = 0;

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++)
for(j=i+1;j<nelem;j++)
if(wArray[i] > wArray[j])
{
t = wArray[i]; wArray[i] = wArray[j]; wArray[j] = t;
}

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


printf("<%d,%d>\n", nArray[i],wArray[i]);
getch();

int prime(int num)


{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;

break;
}
return flag;

}
int percube(int num)
{
int i,flag=0;

for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{

flag=1; break;
}
return flag;
}
Output:
Enter the number of elements in an
array: 5 Enter 5 elements:
8
11
216
24
34

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

 8 is a perfect cube of 2, not a prime number and not a multiple of 4 &


divisible of 6 so the answer is 5.
 11 is a prime number so the answer is 3.
 216 is a perfect cube and multiple of 4 & divisible by 6 so the answer is 5+4 = 9.
 24 is not a perfect cube and not a prime number and multiple of 4 &
divisible by 6 so the answer is 4.
 34 not satisfied all the conditions so the answer is 0.

Result:
Thus a C Program for Sort the numbers based on the weight was executed
successfully and the output was verified.
EX. No. : 7 AVERAGE HEIGHT OF PERSONS

DATE:

Aim:
To write a C Program to populate an array with height of persons and find how many
persons are above the average height.

Algorithm:

Step1:Start
Step2: Declare variables
Step3: Read the total number of persons and their height.
Step4: Calculate avg=sum/n and find number of persons their h>avg.
Step5: Display the output of the calculations.
Step6: Stop

Program:
#include<stdio.h>
#include <conio.h>
void main()
{

int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
printf("Enter the Number of Persons : ");
scanf("%d",&n);
printf("\nEnter the Height of each person incentimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);

sum = sum + height[i];


}
avg = (float)sum/n;
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
printf("\nAverage Height of %d persons is : %.2f\n",n,avg);
printf("\nThe number of persons above average : %d ",count); getch();
}

Output:
Enter the Number of Persons: 5
Enter the Height of each person in
centimeter 150
155
162
158
154

Average Height of 5 persons is:


155.8 The number of persons
above average: 2

Result:

Thus a C Program average height of persons was executed successfully and the
output was verified.
EX. No.: 8 BODY MASS INDEX OF THE INDIVIDUALS

DATE:

Aim:

To write a C Program to Populate a two dimensional array with height and weight of
persons and compute the Body Mass Index of the individuals.

Algorithm:

Step1: Start
Step2: Declare variables
Step3: Read the number of persons and their height and weight.

Step4: Calculate BMI=W/H2for each person.


Step5: Display the output of the BMI for each person.
Step6: Stop

Program:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
float
massheight[5][5];
float bmi[n];
printf("How many people's BMI do you want to calculate?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
{
switch(j)
{
case 0:
printf("\nPlease enter the mass of the person %d in kg: ",i+1);
scanf("%f",&massheight[i][0]);
break;

case 1:
printf("\nPlease enter the height of the person %d in meter: ",i+1);
scanf("%f",&massheight[i][1]);
break;
}
}
}
for(i=0;i<n;i++)
{
bmi[i]=massheight[i][0]/pow(massheight[i][1],2.0);
printf("Person %d's BMI is %f\n",i+1,bmi[i]);

getch();

}
Output:

How many people's BMI do you want to calculate? 2


Please enter the mass of the person 1 in kg: 88

Please enter the height of the person 1 in meter: 1.8288

Please enter the mass of the person 2 in kg: 58


Please enter the height of the person 2 in meter: 2.2
Person 1's BMI is26.31178
Person 2's BMI is11.98347

Result:

Thus a C Program Body Mass Index of the individuals was executed successfully
and the output was verified.
EX. No.: 9 REVERSE OF A GIVEN STRING

DATE:

Aim:
To write a C Program to perform reverse without changing the position of special
characters for the given string.

Algorithm:

Step 1: Start
Step 2: Declare variables.
Step 3: Read a String.
Step 4: Check each character of string for alphabets or a special character by
using is Alpha ().
Step 5: Change the position of a character vice versa if it is alphabet otherwise remains
same.
Step 6: Display the output of the reverse string without changing the position of special
characters.
Step 7: Stop
Program:
#include <stdio.h>

#include <string.h>

#include <conio.h>
void swap(char *a, char *b)
{
char t;

t = *a;
*a = *b;
*b = t;
}

void main()
{
char str[100];
reverse(char *);
int isAlpha(char);
void swap(char *a ,char *b);
clrscr();

printf("Enter the Given String : ");


scanf("%[^\n]s",str);
gets(str);
reverse(str);
printf("\nReverse String : %s",str);
getch();
}
void reverse(char str[100])
{
int r = strlen(str) - 1, l = 0;
while (l < r)
{
if (!isAlpha(str[l]))
l++;
else if(!isAlpha(str[r]))

r--;
else
{
swap(&str[l], &str[r]);

l++;
r--;
}

}
int isAlpha(char x)
{
return ( (x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z') );
}
Output:

Enter the Given String :a@gh%;j


Reverse String :j@hg%;a

Result:

Thus a C Program for reverse of a given String was executed successfully and the
output was verified.
EX. No.: 10 STRING OPERATIONS
DATE:

Aim:
To write a C program to perform string operations on a given paragraph for the following
using built-in functions:
 Find the total number of words.

 Capitalize the first word of each sentence.

 Replace a given word with another word.

Algorithm:

Step1: Start

Step2: Declare variables

Step3: Read the text paragraph..

Step4: Perform the functions such as capitalize (),wordCount() and replace().

Step5: Compute do while loops and for loop until the given conditions are
satisfied for function.

Step6: Find the first word of each sentence to capitalize by checks to see if a character
is a punctuation mark used to denote the end of a sentence. ( . ? ‘ ‘)

Step7: Count the number of words in the text paragraph.

Step7: Replace the word in the text by user specific word if match.

Step8: Display the output of the computation of new paragraph.

Step 9: Stop
Program:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX 100
void capitalize(char *);
int wordCount(char *);
void replace(char *);
void main()
{
char para[MAX]={0};
printf("Enter a paragraph:");
scanf("%[^\n]s",para);
capitalize(para);
printf("\n Capitalize paragraph is :%s",para);
printf("\n Number of words in given paragraph are : %d",wordCount(para));
replace(para);
printf("\n New paragraph is :%s",para);
getch();
}
void capitalize(char *p)
{
int i=0;

do
{
if(i==0||(p[i-1]==' '&& (p[i-2]=='.'|| p[i-2]=='?')))
{
do
{
p[i]=toupper(p[i]);

i++;
}while(p[i]!=' ');
}
i++;
}while(p[i]!='\0');
}

int wordCount(char *p)


{
int i,count=0;
for(i=0;p[i]!='\0';i++)
{
if(p[i]==' ')

count++;
}
return count+1;
}

void replace(char *p)


{
char word[20],rpword[20];

char str[10],rp[100],pp[100];

int j=0,k,len,length;
printf("\nEnter the word to be replaced:");
scanf("%s",word);
printf("\n Enter the word the %s to be replaced:",word);
scanf("%s",rpword);
length=strlen(p);
for(k=0;k<length;k++)
{
j=0;
do
{
if(p[k+j]!=' '||p[k+j]!='.'||p[k+j]!='?')
{

else
{
str[j]=p[k+j];

j++;

}
}while((p[k+j]!=' ') && (p[k+j]!='.') && (p[k+j]!='?') &&(p[k+j]!='\0'));

str[j]='\0';
if(strcmp(str,word)==0)
{
len=strlen(str);

strncpy(pp,p,k);
strncpy(rp,p+k+len,length-k-len);
strcpy(p,pp);
strcat(p,rpword);
strcat(p,rp);

}
}
}
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 replaced Students
Enter the word the students to be replaced: Cse
New Paragraph is: HELLO Cse. HOW are you? LET us Study

Result:
Thus a C Program for String operations using built in functions was executed
successfully and the output was verified.
EX.No. : 11 CONVERSION OF DECIMAL NUMBER INTO OTHER BASES
DATE :
Aim:

To write a C Program to convert the given decimal number into binary, octal and
hexadecimal numbers using user defined functions.

Algorithm:

Step1: Start
Step2: Declare variables.
Step3: Read a decimal number.
Step4: Develop the procedure for conversion of different base by modulus and divide
operator.
Step5: Display the output of the conversion value.
Step6: Stop
Program:

#include <stdio.h>
#include <conio.h>
void swap(char *s1, char *s2)
{
char temp;

temp = *s1;
*s1 = *s2;
*s2 = temp;

}
void reverse(char *str, int length)
{
int start = 0;
int end = length -1;

while (start < end)


{
swap(&str[start], &str[end]);
start++;
end--;

}
}

char* convert(int num, char str[100], int base)


{
int i = 0;

if (num == 0)
{
str[i++] = '0';
str[i] = '\0';

return str;
}
while (num != 0)

{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';

num = num/base;
}

str[i] = '\0';
reverse(str, i);

return str;
}

void main()
{
char str[100];

int n;
printf("Enter the given decimal number : ");
scanf("%d",&n);
printf("\nThe Binary value : %s\n",convert(n,str,2));
printf("\nThe Octal value : %s\n",convert(n,str,8));
printf("\nThe Hexa value : %s\n",convert(n,str,16));
getch();
}
Output:

Enter the given decimal number: 14


The Binary value: 1110
The Octal value: 16

The Hexa value :e

Result:
Thus a C program for conversion of decimal number into other bases was
executed successfully and the output was verified.
EX. No. : 12 TOWERS OF HANOI USING RECURSION

DATE:

Aim:
To write a C Program to solve towers of Hanoi using recursion.

Algorithm:

Step1: Start

Step2: Declare variables

Step3: Read the Input for number of discs.

Step4: Check the condition for each transfer of discs using recursion.

Step5: Display the output of the each move.

Step6: Stop
Program:
#include <stdio.h>

#include <conio.h>
void towerofhanoi(int n, char from, char to, char aux)
{

if (n == 1)
{
printf("\n Move disk 1 from pole %c to pole %c", from, to);
return;
}

towerofhanoi(n-1, from, aux, to);


printf("\n Move disk %d from pole %c to pole %c", n, from, to);
towerofhanoi(n-1, aux, to, from);

int main()

int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
towerofhanoi(n, 'A', 'C', 'B');
return 0;

}
Output:

Enter the number of disks: 3

Move disk 1 from pole A to pole C

Move disk 2 from pole A to pole B

Move disk 1 from pole C to pole B

Move disk 3 from pole A to pole C

Move disk 1 from pole B to pole A

Move disk 2 from pole B to pole C

Move disk 1 from pole A to pole C

Result:

Thus a C Program for Towers of Hanoi using Recursion was executed successfully
and the output was verified.
EX.No. : 13 SORTING USING PASS BY REFERENCE

DATE:

Aim:

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

Algorithm:

Step 1: Start

Step 2: Declare variables and create an array

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

Step 4: Develop a function to sort the array by passing reference

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

Step 6: Display the output of the sorted elements.

Step 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
33
67
21
45
11
After Sorting....
11
21
33
45
67

Result:

Thus a C Program Sorting using pass by reference was executed and the output was
verified.
EX.No. : 14 BANKING APPLICATION
DATE:

Aim:
To write a C Program to Count the number of account holders whose balance is
less than the minimum balance using sequential access file.

Algorithm:

Step 1: Start

Step 2: Declare variables and file pointer.

Step 3: Display the menu options.

Step 4: Read the Input for transaction processing.

Step 5: Check the validation for the input data.

Step 6: Display the output of the calculations.

Step 7: Repeat step 3 until choose to stop.

Step 8: Stop
Program:
/* Count the number of account holders whose balance is less than the minimum
balance using sequential access file.*/

#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
MinimumBalance\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:
1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop

Enter your choice : 1


Enter the Account Number : 547898760
Enter the Account Holder Name : mani
Enter the Initial Amount to deposit : 2000

Press any key to continue....


1. Add a New Account Holder
2. Display
3. Deposit or Withdraw
4. Number of Account Holder Whose Balance is less than the Minimum Balance
5. Stop
Enter your choice : 4

The Number of Account Holder whose Balance less than the Minimum Balance : 0

Result:

Thus a C Program for Banking Application was executed and the output was verified.
EX.No.: 15 RAILWAY RESERVATION SYSTEM
DATE:

Aim:
Create a Railway reservation system in C with the following modules
• Booking
• Availability checking
• Cancellation
• Prepare chart

Algorithm:

Step 1: Start
Step 2: Declare variables
Step 3: Display the menu options
Step 4: Read the option.
Step 5: Develop the code for each option.
Step 6: Display the output of the selected option based on existence.
Step 7: Stop
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);
printf("\nticketnumber only 1-10:");
scanf("%d",&s[i].ticketno);
i++;
}
void availability()
{
int c;
printf("availability checking");
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.thired class\n");
printf("enter the option");
scanf("%d",c);
switch(c)
{
case 1:
first++;
break;
case 2:
second++;
break;
case 3:
third++;
break;
default:
break;
}
printf("ticket is canceled");
}
void chart()
{
int c;
for(c=0;c<I;c++)
{
printf(“\n Ticket No\t Name\n”);
printf(“%d\t%s\n”,s[c].ticketno,s[c].name);
}
}
main()
{
int n;
clrscr();
printf("welcome to railway ticket reservation\n");
while(1)
{
printf("1.booking\n2.availability cheking\n3.cancel\n4.Chart \n5. Exit\n enter
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();
}
Output:

welcome to railway ticket reservation


1.booking
2.availability checking
3.cancel
4.Chart
5. Exit
enter your option: 2

availability checking
1.first class
2.second class
3.third class

enter the option 1


seat available
1.booking
2.availability checking
3.cancel
4.Chart
5. Exit

enter your option: 5

Thank you visit again!

Result:
Thus a C Program for Railway reservation system was executed and the output was
verified.

You might also like