You are on page 1of 81

SARANATHAN COLLEGE OF ENGINEERING

VENKATESWARA NAGAR,
TRICHY-MADURAI MAIN ROAD (NH 45 B),
EDAMALAIPATTI PUDHUR(POST), PANJAPPUR,
THIRUCHIRAPPALLI � 620012

CS 8261

PROGRAMMING IN C LABORATORY

LAB MANUAL

BATCHELOR OF ENGINEERING

COMPUTER SCIENCE AND ENGINEERING / INFORMATION


TECHNOLOGY

ACADEMIC YEAR 2017-2018


Syllabus: CS8261 C PROGRAMMING LABORATORY L T P C

0 0 4 2

OBJECTIVES:

. To develop programs in C using basic constructs.


. To develop applications in C using strings, pointers, functions, structures.
. To develop applications in C using file processing.

LIST OF EXPERIMENTS:

1. Programs using I/O statements and expressions.

2. Programs using decision-making constructs.

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)

4. Design a calculator to perform the operations, namely, addition, subtraction,


multiplication, division and square of a number.

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

6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based
on the
following conditions

A. 5 if it is a perfect cube
B. 4 if it is a multiple of 4 and divisible by 6
C. 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>

7. Populate an array with height of persons and find how many persons are above the

average height.

8. Populate a two dimensional array with height and weight of persons and compute
the
Body Mass Index of the individuals.

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)

10. Convert the given decimal number into binary, octal and hexadecimal numbers
using user defined functions.

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.


12. Solve towers of Hanoi using recursion.

13. Sort the list of numbers using pass by reference.

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

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

16. Insert, update, delete and append telephone details of an individual or a


company
into a telephone directory using random access file.

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

Mini Project 18. Create a .Railway reservation system. with the following modules

. Booking
. Availability checking
. Cancellation
. Prepare chart

TOTAL: 60 PERIODS OUTCOMES: Upon completion of the course, the students will
be able to:.

. Develop C programs for simple applications making use of basic constructs,


arrays and strings..
. Develop C programs involving functions, recursion, pointers, and structures.
Design applications using sequential and random access file processing
TABLE OF CONTENTS

Ex. No

Title of the Exercise

Page No

Programs Using I/O Statements and Expressions

Programs Using Decision-Making Constructs

Program To Find Whether the Given Year Is Leap


Year or Not

Menu Driven Calculator

11

Armstrong Number

15

Sort the Numbers Based on the Weight

17

Average Heights of Persons

21

8
Body Mass Index of the Individuals

24

Reverse of a Given String

27

10

Conversion of Decimal Number into Other Bases

31

11

String Operations

36

12

Towers of Hanoi Using Recursion

42

13

Sorting Using Pass by Reference

45

14

Salary Slip of Employees

48

15

Internal Marks of Students

51

16

Telephone Directory

55

17
Banking Application

65

18

Railway Reservation System

69
Ex: No: 01 Programs Using I/O Statements

And Expressions

Aim: To write a C Program to perform I/O statements and expressions.

Algorithm:

1. Start the program.

2. Declare variables and initializations

3. Read the Input variable.

4. Using I/O statements and expressions for computational processing.

5. Display the output of the calculations.

6. Stop the program execution.

Program:

#include<stdio.h>

void main()

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",ag


Test case:

Input

Expected output

Actual output

Status
Ex: No: 02 Programs Using Decision-Making

Constructs

Aim: To write a C Program to perform decision-making constructs.

Algorithm:

1. Start the program.


2. Input a number from the user
3. If number is less than zero, then it is a negative integer
4. Else If number is greater than zero, then it is a positive integer
5. Else, the number is equal to zero
6. Stop the program execution

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

printf("The given number %d is neither positive nor negative",n);

getch();

Test case:

Input

Expected output

Actual output

Status
Ex: No: 03 Program To Find Whether the

Given Year Is Leap Year or Not

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

Algorithm:

1. Start the program

2. Declare variables

3. Read the Input.

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

5. Using if,else statements to,

a) Check whether a given year is divisible by 400.

b) Check whether a given year is divisible by 100.

c) Check whether a given year is divisible by 4.

6. If the condition at step 5.a becomes true, then print the ouput as �It is a leap
year�.

7. If the condition at step 5.b becomes true, then print the ouput as �It is not a
leap year�.

8. If the condition at step 5.c becomes true, then print the ouput as �It is a leap
year�.

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

10. Display the output of the calculations.

11. Stop the program execution

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 case:

Input

Expected output

Actual output

Status
Ex: No: 04 Menu Driven Calculator

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:

1. Start the program.

2. Declare variables .

3. Read the Inputs .

4. Calculate Arithmetic operations(+,-,*,/,pow) for the input of two numbers.

5. Display the output of the calculations .

6. Stop the program execution.

Program:

#include<stdio.h>

void main()

intch,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;

printf("The square of a number %d is:%d",a,c);

break;

default:

printf("Invalid option");

getch();

}
Test case:

Input

Expected output

Actual output

Status
Ex: No: 05 Armstrong Number

Aim: To write a C Program to Check whether a given number is Armstrong number or


not.

Algorithm:

1. Start the program.

2. Declare variables.

3. Read the Input number.

4. Calculate sum of cubic of individual digits of the input.

5. Match the result with input number.

6. If match, Display the given number is Armstrong otherwise not.

7. Stop the program execution.

Program:

#include<stdio.h>

void main()

intsum,rem,dup,n;

sum=0;

clrscr();

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

printf("Armstrong number\n");

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

printf("Enter a 3 digit number:");

scanf("%d",&n);

dup=n;
while(n>0)

rem=n%10;

sum=sum+(rem*rem*rem);

n=n/10;

if(dup==sum)

printf("The given number %d is an armstrongnumber",sum);

else

printf("The given number %d is not an armstrongnumber",sum);

getch();

Test case:

Input

Expected output

Actual output

Status
Ex: No: 06 Sort the Numbers Based on the Weight

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:

1. Start the program.

2. Declare variables .

3. Read the number of elements .

4. Get the individual elements.

5. 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)

6. Display the output of the weight calculations after sorting .

7. Stop the program execution.

Program:

#include <stdio.h>
void main()

intnArray[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<=num/2;i++)

if(num%i==0)

flag=0;

break;
}

return flag;

intpercube(intnum)

inti,flag=0;

for(i=2;i<=num/2;i++)

if((i*i*i)==num)

flag=1;

break;

return flag;

Test case:

Input

Expected output

Actual output

Status
Ex: No: 07 Average Heights of Persons

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:

1. Start the program.

2. Declare variables

3. Read the total number of persons and their height.

4. Calculate avg=sum/n and find number of persons their h>avg.

5. Display the output of the calculations .

6. Stop the program execution.

Program:

#include<stdio.h>

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 avgheight,%d are:%d\n",avg,count);

getch();

intaboveavg(int A[],intn,intavg)

inti=0,c=0;

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

if(A[i]>avg)

c++;

return c;

intavgheight(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 case:

Input

Expected output

Actual output

Status
Ex: No: 08 Body Mass Index of the

Individuals

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:

1. Start the program.

2. Declare variables

3. Read the number of persons and their height and weight.

4. Calculate BMI=W/H2for each person

5. Display the output of the BMI for each person.

6. Stop the program execution.

Program:

#include<stdio.h>

void main()

float A[2][10],H[10],W[10],B[10];

inti=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);
for(i=0;i<2;i++)

if(i==0)

printf("Enter the height of the individuals in meters:\n");

else

printf("Enter the weight of the individuals in kgs:\n");

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

scanf("%f",&A[i][j]);

i=0;

while(i<1)

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

H[k]=A[i][j]*A[i][j];

k++;

i++;

k=0;
for(j=0;j<n;j++)

W[k]=A[i][j];

k++;

printf("The BMI of the given individuals are as follows:\n");

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

B[i]=W[i]/H[i];

printf("%f\n",B[i]);

getch();

Test case:

Input

Expected output

Actual output

Status
Ex: No: 09 Reverse of a Given String

Aim: To write a C Program to perform reverse without changing the position of


special
characters for the given string.

Algorithm:

1. Start the program.

2. Declare variables .

3. Read a String.

4. Check each character of string for alphabets or a special character by using


isAlpha() .

5. Change the position of a character vice versa if it is alphabet otherwise


remains same.

6. Repeat step 4 until reach to the mid of the position of a string.

7. Display the output of the reverse string without changing the position of
special characters.

8. Stop the program execution.

Program:

#include<stdio.h>

#include<string.h>

void main()

char C[50],R[50];

intlen,i=0,temp;

clrscr();

printf("Enter the string:\n");

scanf("%s",&C);

len=strlen(C);
printf("The length of the string is:%d\n",len);

strcpy(R,C);

for(i=0;i<len/2;i++)

temp=R[len-i-1];

R[len-i-1]=R[i];

R[i]=temp;

swap(R,C,len);

getch();

int swap(char R[],char C[],intlen)

inti=0;

char *C1;

char *C2;

C1=C;

C2=R;

while(i<len)

if(!isalpha(C[i]))

C1++;
}

else if(!isalpha(*C2))

C2++;

i--;

else

C[i]=*C2;

C2++;

i++;

printf("The reversed string with special characters at same position is:%s",C);

return 0;

}
Test case:

Input

Expected output

Actual output

Status
Ex: No: 10 Conversion of Decimal Number into Other Bases

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

Algorithm:

1. Start the program.

2. Declare variables.

3. Read a decimal number.

4. Develop the procedure for conversion of different base by modulus and divide
operator.

5. Display the output of the conversion value.

6. Stop the program.

Program:

#include<stdio.h>

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

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

return 0;

int hex(int n)

inti=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]);

return 0;

intoct(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]);

return 0;

}
Test case:

Input

Expected output

Actual output

Status
Ex: No: 11 String Operations

Aim: To write a C Program to perform string operations on a given paragraph for 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.

Algorithm:

1. Start the program


2. Declare variables
3. Read the text.
4. Display the menu options
5. Compare each character with tab char �\t. or space char � � to count no of words

6. 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. (! . ?)
7. Replace the word in the text by user specific word if match.
8. Display the output of the calculations.
9. Repeat the step 4 till choose the option stop.
10. Stop the program

Program:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void replace (char *, char *, char *);

int main()

char choice.str[200];
int i, words;

char s_string[200], r_string[200]; /* Input text from user */

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; /* Runs a loop till end of text */

while(str[i] != '\0')

{ /* If the current character(str[i]) is white space */

if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')

words++;

i++;

}
printf("\nTotal number of words = %d", words); break;

case '2' :

i = 0; /* Runs a loop till end of text */

while(str[i] != '\0')

{ /* Checks to see if a character is a punctuation mark used to denote the end of a

sentence. (! . ?) */

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' :

/*Get the search and replace string from the user.


. Write a user defined function to replace the first occurrence of the search
string with the
replace string. . Recursively call the function until there is no occurrence of the
search
string.*/

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)

//a buffer variable to do all replace things

char buffer[200];

//to store the pointer returned from strstr


char * ch;

//first exit condition

if(!(ch = strstr(str, s_string)))

return;

//copy all the content to buffer before the first occurrence of the search string
strncpy(buffer, str, ch-str);

//prepare the buffer for appending by adding a null to the end of it

buffer[ch-str] = 0;

//append using sprintf function

sprintf(buffer+(ch -str), "%s%s", r_string, ch + strlen(s_string));

//empty str for copying

str[0] = 0;

strcpy(str, buffer); //pass recursively to replace other occurrences

return replace(str, s_string, r_string);


Test case:

Input

Expected output

Actual output

Status
Ex: No: 12 Towers of Hanoi Using Recursion

Aim:

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

Algorithm:

1. Start the program.

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 the program.

Program:

#include <stdio.h>

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

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

void towers(int num, char s_peg, char d_peg, char i_peg)

if (num == 1)

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

return;

else

towers(num - 1, s_peg, i_peg, d_peg);

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

towers(num - 1, i_peg, d_peg, s_peg);

}
Test case:

Input

Expected output

Actual output

Status
Ex: No: 13 Sorting Using Pass by Reference

Aim:

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

Algorithm:

1. Start the program.

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 the program.

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

int sort(int A[], int n)

inti=0,j=1,temp;

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

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

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

temp=A[i];

A[i]=A[j];

A[j]=temp;

printf("%d\n",A[i]);
}

return 0;

Test case:

Input

Expected output

Actual output

Status
Ex: No: 14 Salary Slip of Employees

Aim:

To write a C Program to Generate salary slip of employees using structures and


pointers.

Algorithm:

1. Start the program.

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 the program.

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;

struct emp e *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 case:

Input

Expected output

Actual output

Status
Ex: No: 15 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 the program.

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 the program.

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;

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++)
{

total=0;

for(j=0;j<=2;++j)

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

students[a].i[b]=total/3;

Test case:

Input

Expected output

Actual output

Status
Ex: No: 16 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:

1. Start the program.

2. Declare variables, File pointer and phonebook structures.

3. Create menu options.

4. Read the option.

5. Develop procedures for each option.

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

7. Display the message for operations performed.

8. Stop the program.

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)

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

Test case:

Input

Expected output

Actual output

Status
Ex: No: 17 Banking Application

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:

1. Start the program.

2. Declare variables and file pointer.

3. Display the menu options.

4. Read the Input for transaction processing.

5. Check the validation for the input data.

6. Display the output of the calculations.

7. Repeat step 3 until choose to stop.

8. Stop the program.

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

Test case:

Input

Expected output

Actual output

Status
Ex: No: 18 Railway Reservation System

Aim:

Create a Railway reservation system in C with the following modules . booking .


Availability checking . Cancellation . Prepare chart

Algorithm:

1. Start the program.

2. Declare variables

3. Display the menu options

4. Read the option.

5. Develop the code for each option.

6. Display the output of the selected option based on existence.

7. Stop the program.

Program:

#include<stdio.h>

#include<conio.h>

int first=5,second=5,thired=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 cheking");

printf("\n1.first class\n2.second class\n3.thired 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(thired>0)

printf("seat available\n");
thired--;

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:

thired++;

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\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 case:

Input

Expected output

Actual output

Status

You might also like