Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Programming in C Lab Manual

Uploaded by

Mariyam Hilmiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Programming in C Lab Manual

Uploaded by

Mariyam Hilmiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

MOHAMED SATHAK ENGINEERING COLLEGE

KILAKARAI – 623806
Approved by AICTE, Accredited by NAAC

DEPARTMENT OF INFORMATION TECHNOLOGY

ANNA UNIVERSITY – CHENNAI – 600 025

UG PRACTICAL EXAMINATIONS

APR/MAY-2024

RECORD NOTE BOOK

CS CS3271 - ProgrammingINinCCLABORATORY
3271 - PROGRAMMING Laboratory

LAB MANUAL
Student Name :
(R - 2021)
Register Number :
Prepared by
Class & Semester :
Sangeetha S, AP/IT
Month & Year :
MOHAMED SATHAK ENGINEERING COLLEGE
LIST OF EXPERIMENTS
KILAKARAI – 623806
1. I/O statements,(Approved
operators, expressions
by AICTE, Accredited by NAAC)
2. Decision-making constructs: if-else, goto, switch-case, break-continue
DEPARTMENT
3. Loops: OF COMPUTER SCIENCE AND ENGINEERING
for, while, do-while

4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal

5. Strings: operations
Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Class: . . . . . . . . . . . . . . . . . .
6. Functions: call, return, passing parameters by (value, reference), passing arrays to function.
.
7. Recursion

8. Pointers: Register
Pointers to No:
functions, Arrays, Strings, Pointers to Pointers, Array of Pointers

9. Structures: Nested Structures,


Pointers to Structures, Arrays of Structures and Unions.

10. Files: reading and writing, File pointers, file operations, random access, processor directives.
Certified that this is the bonafide record of work done by above student in the
CS3271 - Programming in C Laboratory during the year 2023-24.
TOTAL: 60 PERIODS

.................... ..... ......... .............. ..... .........

Signature of Lab-in-charge Signature of H.O.D.

Submitted for the university practical examination held on . . . . . . . . . . . . .

.................... ..... ........ .................... ..... ........

Internal Examiner External Examiner


TABLE OF CONTENTS

Exp.No Date Name of the Experiment Page No. Signature

1 Programs using I/O statements and expressions.

2 Programs using decision-making constructs.

3 To find whether the given year is leap year or Not

4 Arithmetic operations

5 Armstrong number

6 Sort the numbers based on the weight

7 Average height of persons

8 Body Mass Index of the individuals

9 Reverse of a given string

10 Conversion of Decimal number into other bases

11 String operations

12 Towers of Hanoi using Recursion

13 Sorting using pass by reference

14 Salary slip of employees

15 Internal marks of students

16 Telephone directory

17 Banking Application

18 Railway Reservation System


EX.No. : 01 PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE :

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

ALGORITHM

1. Start
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

PROGRAM

/*Sum the odd and even numbers, respectively, from 1 to a given upper bound. Also compute the absolute
difference. (SumOddEven.c)*/

#include <stdio.h>
int main()
{
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int upperbound; // Sum from 1 to this upperbound
int absDiff; // The absolute difference between the two sums
printf("Enter the upper bound: "); // Prompt user for an upperbound
scanf("%d", &upperbound); // Use %d to read an int
int number = 1; //Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
while (number <= upperbound)
{
if (number % 2 == 0) // Even number
{
sumEven += number; // Add number into sumEven
}
else
{ // Odd number
sumOdd += number; // Add number into sumOdd
}
++number; // increment number by 1
}
if (sumOdd > sumEven) // Compute the absolute difference between the two sums
{
absDiff = sumOdd - sumEven;
}
else
{
absDiff = sumEven - sumOdd;
} // Print the results
printf("The sum of odd numbers is %d.\n", sumOdd);
printf("The sum of even numbers is %d.\n", sumEve );
printf("The absolute difference is %d.\n", absDiff);
return 0;
}

RESULT:

Thus a C Program using i/o statements and expressions was executed and the output was obtained.
EX.No. : 02 PROGRAM USING DECISION-MAKING CONSTRUCTS
DATE :

AIM
To write a C Program to perform decision-making constructs.
ALGORITHM
1. Start
2. Declare variables and initializations
3. Read the Input variable.
4. Codes are given to different categories and da s calculated as follows: For code 1,10% of basic salary.
For code 2, 15% of basic salary. For code 3, 20% of basic salary. For code >3 da is not given.
5. Display the output of the calculations .
6. Stop
PROGRAM

#include <stdio.h>
#include<conio.h>
void main ()
{
float basic , da , salary ;
int code ;
char name[25];
da=0.0;
printf("Enter employee name\n");
scanf("%[^\n]",name);
printf("Enter basic salary\n");
scanf("%f",&basic);
printf("Enter code of the Employee\n");
scanf("%d",&code);
switch (code)
{
case 1:
da = basic * 0.10;
break;
case 2:
da = basic * 0.15;
break;
case 3:
da = basic * 0.20;
break;
default :
da = 0;
}
salary = basic + da;
printf("Employee name is\n");
printf("%s\n",name);
printf ("DA is %f and Total salary is =%f\n",da, salary);
getch();
}

RESULT

Thus a C Program using decision-making constructs was executed and the output was obtained.
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
1. Start
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
PROGRAM
#include<stdio.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);
}

RESULT
Thus a C Program for Leap year checking was executed and the output was obtained.
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
1. Start
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
PROGRAM
/*C Program for Addition, Subtractio , Multiplication, Division and square of two numbers*/
#include <stdio.h>
#include <conio.h>
int main()
{
int firstNumber, secondNumber;
int sum, difference, product;
long square;
float quotient;
printf("Enter First Number: ");
scanf("%d", &amp;firstNumber);
printf("Enter Second Number: ");
scanf("%d", &amp;secondNumber);
sum = firstNumber + secondNumber;
difference = firstNumber - secondNumber;
product = firstNumber * secondNumber;
quotient = (float)firstNumber / secondNumber;
square = firstNumber *firstNumber;
printf("\nSum = %d", sum);
printf("\nDifference = %d", difference);
printf("\nMultiplication = %d", product);
printf("\nDivision = %.3f", quotient);
printf("\n Square= %ld", square);
getch();
return 0;
}

RESULT
Thus a C Program for Arithmetic operations was executed and the output was obtained.
EX.No. : 5 Armstrong number
DATE :

AIM
To write a C Program to Check whether a given number is Armstrong number or not .
ALGORITHM
1. Start
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
PROGRAM
/** C Program to Check whether a given Number is Armstrong */
#include <stdio.h>
#include <math.h>
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
}

RESULT

Thus a C Program for Armstrong number checking was executed and the output was obtained.
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
1. Start
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
PROGRAM
#include <stdio.h>
#include <math.h>
void main()
{
int nArray[50],wArray[50],nelem,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]);
//Calculate the weight
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", nArr y[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;
}

RESULT
Thus a C Program for Sort the numbers based on the weight was executed and the output was obtained.
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
1. Start
2. Declare variables
3. Read the total number of persons and their h ight.
4. Calculate avg=sum/n and find number of p rsons their h>avg.
5. Display the output of the calculations .
6. Stop
PROGRAM
/* Get a Height of Different Pe sons and find how many of them are are above average */
#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 in centimeter\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();
}

RESULT
Thus a C Program average height of persons was executed and the output was obtained.
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
1. Start
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 p rson.
6. Stop
PROGRAM
#include<stdio.h>
#include<math.h>
int main(void)
{
int n,i,j;
printf("How many people's BMI do you want to calculate?\n"); scanf("%d",&n);
float massheight[n][2];
float bmi[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]);
} return 0;}

RESULT
Thus a C Program Body Mass Index of the individuals was executed and the output was obtained.
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
1. Start
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
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];
void 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') );
}

RESULT

Thus a C Program for reverse of a given String was executed and the output was obtained.
EX.No. : 10 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
1. Start
2. Declare variables.
3. Read a decimal number.
4. Develop the procedure for conversion of diff r nt base by modulus and divide operator.
5. Display the output of the conversion value.
6. 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; clrscr();
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\ ",convert(n,str,16)); getch();
}

RESULT

Thus a C Program for conversion of decimal number into other bases was executed and the output was
obtained.
EX.No. : 11 String operations
DATE :

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
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 se te ce 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
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];
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(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("\nTotal number of words = %d", words); break;
case '2' :
i = 0;
while(str[i] != '\0')
{
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' :
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];
char * ch;
if(!(ch = strstr(str, s_string)))
return;
strncpy(buffer, str, ch-str);
buffer[ch-str] = 0;
sprintf(buffer+(ch -str), "%s%s", r_string, ch + strlen(s_string));
str[0] = 0;
strcpy(str, buffer);
replace(str, s_string, r_string);
}

RESULT

Thus a C Program String operations was executed and the output was obtained.
EX.No. : 12 Towers of Hanoi using Recursion
DATE :

AIM
To write a C Program to solve Towers of Hanoi using recursion.
ALGORITHM
1. Start
2. Declare variables
3. Read the Input for number of discs.
4. Check the condition for each transfer of discs us ng recursion.
5. Display the output of the each move .
6. 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 peg %c to peg %c", from, to);
return;
}
towerofhanoi(n-1, from, aux, to);
printf("\n Move disk %d from peg %c to peg %c", n, from, to); towerofhanoi(n-1, aux, to, from); }
int main()
{
int n;
clrscr();
printf("Enter the number of disks : ");
scanf("%d",&n); // Number of disks
towerofhanoi(n, 'A', 'C', 'B'); // A, B and C are names of peg
getch();
return 0;
}

RESULT
Thus a C Program Towers of Hanoi using Recursion was executed and the output was obtained.
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
1. Start
2. Declare variables and create an array
3. Read the Input for number of elements and each element.
4. Develop a function to sort the array by passing reference
5. Compare the elements in each pass till all the elements are sorted.
6. Display the output of the sorted elements .
7. Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int n,a[100],i;
void sortarray(int*,int);
clrscr();
printf("\nEnter the Number of Elements in an array : "); scanf("%d",&n);
printf("\nEnter the Array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sortarray(a,n);
printf("\nAfter Sorting....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void sortarray(int* arr,int num)
{
int i,j,temp;
for(i=0;i<num;i++)
for(j=i+1;j<num;j++)
if(arr[i] > arr[j])
{
temp=arr[i]; arr[i] = arr[j]; arr[j] = temp;
}}

RESULT
Thus a C Program Sorting using pass by reference was executed and the output was obtained.
EX.No. : 14 Salary slip of employees
DATE :

AIM
To write a C Program to Generate salary slip of employees using structures and pointers.
ALGORITHM
1. Start
2. Declare variables
3. Read the number of employees.
4. Read allowances, deductions and basic for each employee.
5. Calculate net pay= (basic+ allowances)-deductions
6. Display the output of the Pay slip calculations for each employee.
7. Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#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 D d \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() ;
}

RESULT

Thus a C Program Salary slip of employees was executed and the output was obtained.
EX.No. : 15 Internal marks of students
DATE :

AIM
To write a C Program to Compute internal marks of students for five different subjects using structures
and functions.
ALGORITHM
1. Start
2. Declare variables
3. Read the number of students.
4. Read the student mark details
5. Calculate internal mark by i=total of three test marks / 3 for each subject per student.
6. Display the output of the calculations for all the students.
7. Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
struct 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].roll o);
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;
}}}

RESULT
Thus a C Program for Internal marks of students was executed and the output was obtained.
EX.No. : 16 Telephone directory
DATE :

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.
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
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 Pho
ebook"); 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 Nam : ");
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[
+1].LastName); strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phon book.\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\ ", phonebook[x].FirstName, phonebook[x].LastName,
phonebook[x].PhoneNumber);
}}}}

RESULT

Thus a C Program was executed and the output was obtained.


EX.No. : 17 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
1. Start
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 chooses to stop.
8. Stop
PROGRAM
/* Count the number of account holders whose balance is less than the minimum balance using sequential acc ss
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 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');
}
RESULT

Thus a C Program for Banking Application was executed and the output was obtained.
EX.No. : 18 Railway reservation system
DATE :

AIM
To Create a Railway reservation system in C with the following modules
• Booking
• Availability checking
• Cancellation
• Prepare chart
ALGORITHM
1. Start
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
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].tick tno,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();
}

RESULT

Thus a C Program for Railway reservation system was executed and the output was obtained.

You might also like