ex. no. 1 i/o statements and...

59
CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan College of Engineering Page 1 of 59 Ex. No. 1 I/O STATEMENTS AND EXPRESSIONS PROBLEM STATEMENT: Programs using I/O statements and expressions. AIM: To write C programs to demonstrate the uses of various formatted and unformatted input and output functions, expressions and functional blocks. ALGORITHM: STEP 1:Start the program. STEP 2:Declare all required variables and initialize them. STEP 3:Get input values from the user for arithmetic operation. STEP 4:Use input function scanf() and output function printf()to display the output after the calculation. STEP 5:Stop the program. PROGRAM: #include<stdio.h> voidmain() { intage, bir_year, cur_year; clrscr(); printf("Enter the year of birth:\n"); scanf("%d",&bir_year); printf("Enter the current year:\n"); scanf("%d",&cur_year); age=cur_year-bir_year; printf("The age is:%d years",age); getch(); }

Upload: others

Post on 18-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 1 of 59

Ex. No. 1 I/O STATEMENTS AND EXPRESSIONS

PROBLEM STATEMENT:

Programs using I/O statements and expressions.

AIM:

To write C programs to demonstrate the uses of various formatted and unformatted

input and output functions, expressions and functional blocks.

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare all required variables and initialize them.

STEP 3:Get input values from the user for arithmetic operation.

STEP 4:Use input function scanf() and output function printf()to display the output after

the calculation.

STEP 5:Stop the program.

PROGRAM:

#include<stdio.h>

voidmain()

{

intage, bir_year, cur_year;

clrscr();

printf("Enter the year of birth:\n");

scanf("%d",&bir_year);

printf("Enter the current year:\n");

scanf("%d",&cur_year);

age=cur_year-bir_year;

printf("The age is:%d years",age);

getch();

}

Page 2: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 2 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the C program to perform input and output operations using built-in printf()

and scanf() function has been done and verified successfully.

Page 3: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 3 of 59

Ex. No. 2 DECISION-MAKING CONSTRUCTS

PROBLEM STATEMENT:

Programs using decision-making constructs.

AIM:

To write a C program to check whether the given number is positive number or

negative number

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare all required variables.

STEP 3:Get an input from the user and store it in a variable.

STEP 4:Check the input value whether it is a positive number or negative number.

STEP 5:If the number is less than ZERO, then print the result as “NEGATIVE”.

Otherwise display the result as “POSITIVE”.

STEP 6:Stop the program.

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

Page 4: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 4 of 59

printf("The given number %d is neither positive nor

negative",n);

getch();

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the C program to check for positive and negative numbers using decision

making construct has been completed and verified successfully.

Page 5: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 5 of 59

Ex. No. 3 FIND WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT

PROBLEM STATEMENT:

Write a program to find whether the given year is leap year or Not? (Hint: not every

centurion year is a leap. For example 1700, 1800 and 1900 is not a leap year)

AIM:

T write a C program to check whether the given year is a LEAP year or not.

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare the required variables.

STEP 3:Read the input from the user and store it in a variable.

STEP 4:Using if..else statement,

i. Check whether the given input is divisible by 400

ii. Check whether the given input is divisible by 100

iii. Check whether the given input is divisible by 4

If all conditions are stratified, then go to step 5 otherwise go to step 6.

STEP 5:Print the result as “It is a leap year” and goto step 7.

STEP 6:Print the result as “It is not a leap year”

STEP 7:Stop the program.

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

Page 6: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 6 of 59

if(n%100==0)

n%400==0 ? printf("The given year %d is a leap year",n):

printf("The given year %d is a non-leap year",n);

else if(n%4==0)

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

else

printf("The given year %d is non leap year",n);

getch();

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program to check for leap year has been written in C successfully.

Page 7: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 7 of 59

Ex. No. 4 MENU DRIVEN CALCULATOR

PROBLEM STATEMENT:

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

multiplication, division and square of a number.

AIM:

To write a C program to design a menu-based calculator to perform various basic

arithmetic operations like addition, subtraction, multiplication, division and modulo.

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare all required variables

STEP 3:Get two inputs from the user using scanf() function and store them in “a” and “b”

respectively.

STEP 4:Get the user option and based on the user options, perform the corresponding

arithmetic operations on the user data.

STEP 5:Store the result in a variable called “c” and display the value of “c” using printf().

STEP 6:Stop the program.

PROGRAM:

#include<stdio.h>

void main()

{

int ch,a,b,c;

clrscr();

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

printf("Menu driven program\n");

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

printf("Enter the choice\n");

printf(" 1) Addition\n 2) Subraction\n 3) Multiplication\n

4) Division\n

5) Square\n");

scanf("%d",&ch);

Page 8: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 8 of 59

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;

Page 9: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 9 of 59

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

break;

default:

printf("Invalid option");

}

getch();

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the menu-drive arithmetic calculator has been implemented in C and verified

successfully.

Page 10: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 10 of 59

Ex. No. 5 FIND WHETHER GIVEN NUMBER IS AN ARMSTRONG

NUMBER OR NOT

PROBLEM STATEMENT:

Check whether a given number is Armstrong number or not?

AIM:

To write a C program to check whether the given number is an Armstrong number

or Not.

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare all required variables and initialize the result variable “sum” to 0.

STEP 3:Get an input from the user and store it in “n” and “n_copy” variables.

STEP 4:Using the user input, perform the following operation until it becomes ZERO

r = n%10;

sum = sum + (r*r*r);

n = n/10;

STEP 5:compare the value of “sum” and n_copy”. If they are equal then go to step no 6

otherwise go to step no 7.

STEP 6:print the result as “It is an Armstrong Number” and go to step 8.

STEP 7:print the result as “It is not an Armstrong Number”.

STEP 8:Stop the program.

PROGRAM:

#include <stdio.h>

void main()

{

int n, sum=0;

int r, n_copy;

printf("Enter a four digit number\t");

scanf("%d", &n);

n_copy = n;

while(n>0)

{

Page 11: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 11 of 59

r = n%10;

sum = sum + (r*r*r);

n = n/10;

}

if(sum == n_copy)

printf("%d is an armstrong number", n_copy);

else

printf("%d is not an armstrong number", n_copy);

getch();

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program to check for an Armstrong number has been written in C and the

output was verified successfully.

Page 12: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 12 of 59

Ex. No. 6 SORT THE NUMBERS BASED ON THE WEIGHT

PROBLEM STATEMENT:

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

following conditions.

5 if it is a perfect cube.

4 if it is a multiple of 4 and divisible by 6.

3 if it is a prime number.

Sort the numbers based on the weight in the increasing order as shown below

<10,its weight>,<36,its weight>, <89,its weight>

AIM:

ALGORITHM:

STEP 1:Start the program.

STEP 2:Declare all required global and local variables and initialize them either inside or

outside the functions.

STEP 3:Define a separate function to check the following:

int prime(int n) => to check for prime numbers

int cube(int n) => to check whether it is perfect cube

int getweight(int n) => to assign weight for each input value based the

given condition.

STEP 4:Sort the numbers and their weights in ascending order based on weight.

STEP 5:Display the list of numbers and their weights as follows:

<10,its weight>, <36,its weight>, <89,its weight>

STEP 6:Stop the program.

PROGRAM:

#include<stdio.h>

#include<math.h>

int prime(int n)

{

int flag = 1;

for(int i=2; i<(n/2); i++)

Page 13: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 13 of 59

{

if(n%i == 0)

flag = 0;

}

if(flag)

return 1;

else

return 0;

}

int cube(int num)

{

int i,flag=0;

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

{

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

{

flag=1;

break;

}

}

return flag;

}

int getWeight(int n)

{

int w=0;

if(cube(n))

w+=5;

if(n%4==0 && n%6==0)

w+=4;

if(prime(n))

w+=3;

return w;

}

Page 14: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 14 of 59

void main()

{

int nums[15], ws[15];

int i,j,t,n;

clrscr();

printf("Enter the number of numbers");

scanf("%d",&n);

printf("\nEnter the numbers");

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

scanf("%d",&nums[i]);

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

ws[i]=getWeight(nums[i]);

printf("\nBefore sorting:\n");

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

printf("<%d,%d>\t",nums[i],ws[i]);

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

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

if(ws[j]>ws[j+1])

{

t=ws[j+1];

ws[j+1]=ws[j];

ws[j]=t;

t=nums[j+1];

nums[j+1]=nums[j];

nums[j]=t;

}

printf("\nSorted:\n");

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

printf("<%d,%d>\t",nums[i],ws[i]);

getch();

}

Page 15: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 15 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the C program to calculate weight for a set numbers based on a series of

conditions and sort them based on the assigned weights has been implemented and the

result was tested.

Page 16: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 16 of 59

Ex. No. 7 AVERAGE HEIGHTS OF PERSONS

PROBLEM STATEMENT:

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

average height.

AIM:

To write a C program to populate a 1D array with height of persons and find how

many persons are above the average height value.

ALGORITHM:

STEP 1:Start the program.

STEP 2:Include all required header files and declare all required variables.

STEP 3:Write functions to perform the following:

int avgheight(…) => to calculate the average height value

intaboveavg(…) => to find no. of persons are above the average height

STEP 4:Get the number of persons value from the user and store it in “n”.

STEP 5:Get the height value of “n” persons from the user and store it in an array.

STEP 6:Call the user-defined functions and display the result.

STEP 7:Stop the program.

PROGRAM:

#include<stdio.h>

int aboveavg(int a[],int n,int avg);

int avgheight(int a[],int n);

void main()

{

int a[5],avg,count,n,i=0;

clrscr();

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

printf("Height of the person\n");

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

printf("Enter the no of persons\n");

Page 17: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 17 of 59

scanf("%d",&n);

printf("Enter the height of the %d persons in cm\n",n);

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

scanf("%d",&a[i]);

avg=avgheight(a,n);

printf("The average height of %d persons is:%d\n",n,avg);

count=aboveavg(a,n,avg);

printf("The no of persons above the avg height,%d

are:%d\n",avg,count);

getch();

}

int aboveavg(int a[],int n,int avg)

{

int i=0,c=0;

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

{

if(a[i]>avg)

c++;

}

return c;

}

int avgheight(int a[],int n)

{

int total=0,i=0,mean;

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

{

total=total+a[i];

}

printf("The sum of the heights are:%d\n",total);

mean=total/n;

return mean;

}

Page 18: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 18 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program to count how many persons are above the average height value

has been implemented in C and verified.

Page 19: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 19 of 59

Ex. No. 8 BODY MASS INDEX OF THE INDIVIDUALS

PROBLEM STATEMENT:

Populate a two dimensional array with height and weight of persons and compute

the Body Mass Index of the individuals.

AIM:

To write a C program to compute the Body Mass Index (BMI) value for the given

number of persons with their height and weight values.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include all required header files and declare all necessary variables.

STEP 3: Get the value of "n" from the user.

STEP 4: Get 'n' persons Height and Weight values from the user.

STEP 5: Convert the height value from centimetres to meters before calculating the BMI

value.

STEP 6: Calculate the Body Mass Index (BMI) as follows,

bmi = weight / (height * height)

Store the bmi result in an array.

STEP 7: Display the BMI value and BMI remarks based on the BMI value.

STEP 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

float h, height[10],weight[10],bmi[10];

int i=0,k=0,j=0,n;

clrscr();

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

Page 20: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 20 of 59

printf("BMI CALCULATION\n");

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

printf("Enter the number of individuals:\n");

scanf("%d",&n);

printf("\nEnter the height & weight of individuals:\n");

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

{

printf("\nIndividual %d :\n",i+1);

printf("Height (in cm): ");

scanf("%f",&h);

height[i] = h/100;

printf("Weight (in kg): ");

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

}

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

{

bmi[i]=weight[i]/(height[i] * height[i]);

}

printf("\nThe BMI of the given individuals are as

follows:\n");

printf("\n================================================")

;

printf("\nPerson\tHeight\tWeight\tBMI\tBMI Category");

printf("\n================================================")

;

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

{

printf("\n

%d\t%.1f\t%.1f\t%.2f\t",i+1,height[i]*100,weight[i],bmi[i]);

if(bmi[i]>=35)

printf("Severely obese");

else if(bmi[i]>=30)

printf("Moderately obese");

else if(bmi[i]>=25)

Page 21: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 21 of 59

printf("Overweight");

else if(bmi[i]>=18.5)

printf("Normal");

else if(bmi[i]>=16)

printf("Underweight");

else

printf("Severely Underweight");

}

getch();

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the C program to calculate Body Mass Index (BMI) has been implemented

and tested sucessfully.

Page 22: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 22 of 59

Ex. No. 9 REVERSING THE GIVEN STRING WITHOUT USING

FUNCTIONS

PROBLEM STATEMENT:

Given a string ―a$bcd./fg, Find its reverse without changing the position of special

characters. For example,

input: a@gh%;j

output: j@hg%;a

AIM:

To write a C program to reverse the given string without changing the position of

special characters.

ALGORITHM:

STEP 1:Start the program.

STEP 2: Include all required header files and Declare all required variables.

STEP 3: Define an user-defined function isAlphabet(), to check whether the given symbol

is an alphabet or not. If it is an alphabet, then return TRUE otherwise return

FALSE>

STEP 4: Define an another user-defined function reverse(), to retrieve the characters from

the input string one-by-one and reverse them based on the result of above

mentioned function.

STEP 5: Get a String input from the user and pass it to reverse() function.

STEP 6: Display the reversed string using printf() function.

STEP 7:Stop the program.

PROGRAM:

#include <stdio.h>

#include <string.h>

int isAlphabet(char x)

{

return ( (x >= 'A' && x <= 'Z') || (x >= 'a' && x <=

'z') );

}

void swap(char *a, char *b)

Page 23: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 23 of 59

{

char *t;

t = a;

a = b;

b = t;

}

void reverse(char str[])

{

int r = strlen(str) - 1, l = 0;

while (l < r)

{

if (!isAlphabet(str[l]))

l++;

else if(!isAlphabet(str[r]))

r--;

else

{

char temp;

temp = str[l];

str[l] = str[r];

str[r] = temp;

swap(&str[l], &str[r]);

l++;

r--;

}

}

}

void main()

{

char str[15];

printf("Input string: ");

scanf("%s", str);

reverse(str);

printf("Output string: %s", str);;

getch();

}

Page 24: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 24 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program for reversing a string without changing the special characters'

position has been implemented and tested.

Page 25: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 25 of 59

Ex. No. 10 NUMBER SYSTEM CONVERSION

PROBLEM STATEMENT:

Convert the given decimal number into binary, octal and hexadecimal numbers

using user defined functions.

AIM:

To write a C program to convert the given number from one number system to

another number system.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include all necessary header files and declare all required variables.

STEP 3: Declare separate user-defined function for following conversions,

bin() => Decimal to Binary

hex() => Decimal to Hexadecimal

oct() => Decimal to Octal

STEP 4: Define all user-defined functions for corresponding number system conversion.

STEP 4: Get an input number from the user

STEP 5: Call the user-defined functions and pass the input value to it.

STEP 6: Display the result after each conversion.

STEP 7: Stop the program.

PROGRAM:

#include<stdio.h>

void bin(int n);

void hex(int n);

void oct(int n);

void main()

{

int n;

Page 26: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 26 of 59

clrscr();

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

printf("Number system - Conversion\n");

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

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

scanf("%d",&n);

bin(n);

hex(n);

oct(n);

getch();

}

void bin(int n)

{

int B[50],i=0,len;

printf("The binary equivalent of given number %d is:\n",n);

while(n>0)

{

B[i]=n%2;

n=n/2;

i++;

}

len=i;

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

{

printf("%d",B[len-i-1]);

}

}

void hex(int n)

{

int i=0,len,rem;

char H[50];

printf("\nThe hexadecimal equivalent of given number %d

is:\n",n);

while(n>0)

Page 27: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 27 of 59

{

rem=n%16;

if(rem>9)

{

H[i]=55+rem;

}

else

{

H[i]=48+rem;

}

n=n/16;

i++;

}

len=i;

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

printf("%c",H[len-1-i]);

}

void oct(int n)

{

int O[50],i=0,len;

printf("\nTheocatal equivalent of the given number %d

is:\n",n);

while(n>0)

{

O[i]=n%8;

n=n/8;

i++;

}

len=i;

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

{

printf("%d",O[len-1-i]);

}

}

Page 28: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 28 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the C program to convert the given number from one number system to

another number system has been implemented and tested successfully.

Page 29: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 29 of 59

Ex. No. 11 STRING OPERATIONS USING BUILT-IN FUNCTIONS

PROBLEM STATEMENT:

From a given paragraph perform the following using built-in functions:

a) Find the total number of words.

b) Capitalize the first word of each sentence.

c) Replace a given word with another word.

AIM:

To write a C program to perform various string operations using built-in functions

in string.h and stdlib.h.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare the necessary variables.

STEP 3: Display the menu list and get the user's option and based on the input, perform

the string operations.

STEP 4: Define user-defined functions to perform the following,

i. Count total number of words in the given sentence.

ii. Capitalize the first word of each sentence.

iii. Find a word in the given sentence and replace them with another word.

STEP 5: Get an input string from the user and perform the requested operation.

STEP 6: Count the number of words in the given sentence and display the result.

STEP 7: Take the characters in the input sentence and capitalize the starting character of

each sentence and display the result.

STEP 8: Find the occurrences of the given string in the sentence and replace them with

new string value.

STEP 9: Stop the program.

Page 30: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 30 of 59

PROGRAM:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

void word_count(char s[]);

void capitalize(char s[]);

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

void main()

{

char s1[30],s2[30];

printf("\nString Operations using built-in functions\n");

printf("Enter the string : ");

gets(s1);

word_count(s1);

capitalize(s1);

printf("Enter the substring\n");

fflush(stdin);

gets(s2);

printf("Enter the string to replace the substring\n");

fflush(stdin);

gets(s3);

replace(s1,s2,s3);

getch();

}

void word_count(char s[])

{

int i = 0;

int wc = 0;

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

{

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

Page 31: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 31 of 59

{

wc++;

}

i++;

}

printf("Total number of words in the string is : %d\n", wc);

}

void capitalize(char s[])

{

int i=0;

char new[20];

new[i] = toupper(s[0]);

i++;

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

{

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

{

new[i] = s[i];

new[i+1] = toupper(s[i+1]);

i+=2;

}

else

{

new[i] = s[i];

i++;

}

}

printf("After capitalizing: %s", new);

}

void replace(char *s1, char *s2, char *s3)

{

char *p;

char buf[20];

Page 32: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 32 of 59

p = strstr(s1, s2);

if (p)

{

strncpy(buf, s1, p-s1);

buf[p-s1] = 0;

sprintf(buf+(p-s1), "%s%s", s3, p + strlen(s2));

s1[0] = 0;

strcpy(s1, buf);

printf("%s", s1);

}

else

printf("String not found\n");

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program to perform string operations has been implemented in C using

built-in functions.

Page 33: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 33 of 59

Ex. No. 12 TOWER OF HANOI USING RECURSION

PROBLEM STATEMENT:

. Solve towers of Hanoi using recursion.

AIM:

To write a C program to implement the Tower of Hanoi problem using recursion

technique.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include all necessary header files.

STEP 3: Declare a user-defined function to swap the disk from one poll to another in

Tower of Hanoi problem.

STEP 4: Read input values from the user and pass it to the user-defined function

STEP 5: Check the condition for disk movement from one poll to another and transfer the

disk when the condition is satisfied.

STEP 6: Repeat the above step using recursion until all disks are moved from source poll

to destination poll.

STEP 7: 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 : ");

Page 34: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 34 of 59

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

S. No Input Output Signature

RESULT:

Thus the program for Tower of Hanoi problem has been implemented in C using

recursion technique.

Page 35: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 35 of 59

Ex. No. 13 SORT THE LIST OF NUMBERS USING FUNCTION IN PASS BY

REFERENCE

PROBLEM STATEMENT:

. Sort the list of numbers using pass by reference.

AIM:

To write a C program to sort the list of numbers using function in pass-by-reference

method.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare variables and one dimensional array for storing a list of numbers.

STEP 3: Define a function to sort the array in ascending order.

STEP 4: Read inputs from the user and store it in the array in main() function.

STEP 5: Call the sorting function by pass the input array elements as reference.

STEP 6: Display the list of elements of an array after the sorting operation.

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

}

Page 36: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 36 of 59

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

S. No Input Output Signature

RESULT:

Thus the program for sorting numbers in ascending order using function in pass-by-

reference method has been written and the output was verified.

Page 37: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 37 of 59

Ex. No. 14 EMPLOYEES SALARY SLIP GENERATION

PROBLEM STATEMENT:

Generate salary slip of employees using structures and pointers.

AIM:

To write a C program to generate salary slip of employees using structures.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include all necessary header files and declare all required variables.

STEP 3: Read the number of employees value from the user and store it in "n".

STEP 4: Read basic, allowance and deductions from each employee to calculate the net

salary amount.

STEP 5: Calculate the net salary value as follows,

net_salary = (basic + allowance ) - deduction

STEP 6: Display the calculated result for each employee.

STEP 7: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include <stdlib.h>

struct emp

{

int empno ;

char name[10];

int bpay, allow, ded, npay ;

struct emp *next;

} ;

void main()

Page 38: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 38 of 59

{

int i,n=0;

char answer;

int more_data = 1;

struct emp *current_ptr, *head_ptr;

clrscr() ;

head_ptr = (struct emp *)malloc (sizeof(struct emp));

current_ptr = head_ptr;

while (more_data)

{

printf("\nEnter the employee number : ") ;

scanf("%d", & current_ptr->empno) ;

printf("\nEnter the name : ") ;

scanf("%s",& current_ptr->name) ;

printf("\nEnter the basic pay, allowances & deductions : ")

;

scanf("%d %d %d", & current_ptr ->bpay, & current_ptr -

>allow,

& current_ptr ->ded) ;

e[i].npay = e[i].bpay + e[i].allow - e[i].ded ; n++;

printf("Would you like to add another employee? (y/n): ");

scanf("%s", answer);

if (answer!= 'Y')

{

current_ptr->next = (struct eme *) NULL;

more_data = 0;

}

else

{

current_ptr->next = (struct emp *) malloc

(sizeof(struct emp));

current_ptr = current_ptr->next;

}

}

printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay

\n\n") ;

Page 39: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 39 of 59

current_ptr = head_ptr;

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

{

printf("%d \t %s \t %d \t %d \t %d \t %d \n", current_ptr-

>empno,

current_ptr->name, current_ptr->bpay, current_ptr-

>allow, current_ptr->ded,

current_ptr->npay) ;

current_ptr=current_ptr->next;

}

getch() ;

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the Employee's salary slip generation program has been implemented using

structures and verified.

Page 40: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 40 of 59

Ex. No. 15 INTERNAL MARKS OF STUDENTS

PROBLEM STATEMENT:

Compute internal marks of students for 5 subjects using structures and functions.

AIM:

To write a C program to compute internal marks of students for 5 subjects using

structures and functions.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Read the number of students and store it in "n".

STEP 3: Get "n" students test marks of 5 subjects.

STEP 4: Calculate the internal mark for students as follows,

internal = sum of all subject marks / total no. of subjects

STEP 5: display the value of internal marks for each student.

STEP 6: 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, total;

clrscr();

Page 41: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 41 of 59

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

Page 42: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 42 of 59

}

}

return(0);

}

void calcinternal(int n)

{

int a,b,j,total;

for(a=1;a<=n;++a)

{

for(b=0;b<5;b++)

{

total=0;

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

{

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

}

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

}

}

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the program for computing the internal marks of students has been written in

C using structures and functions and the output was tested.

Page 43: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 43 of 59

Ex. No. 16 CREATION OF TELEPHONE DIRECTORY

PROBLEM STATEMENT:

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

into a telephone directory using random access file.

AIM:

To write a C program to implement the telephone directory application to update,

delete and append telephone details of an individual or a company using file concept.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Declare all required variables, file pointers and phone book structure.

STEP 3: Display the menu options and get an input option from the user.

STEP 4: Based on the user input, call the corresponding function to add, delete, search to

display the contact details.

STEP 5: Display the status & output the requested operation.

STEP 6: 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 * );

Page 44: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 44 of 59

void SearchForNumber(phone * );

int counter = 0;

char FileName[256];

FILE *pRead;

FILE *pWrite;

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

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)

{

Page 45: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 45 of 59

PrintEntry(phonebook);

}

if (iSelection == 4)

{

SearchForNumber(phonebook);

}

if (iSelection == 5)

{

printf("\nYou have chosen to exit the Phonebook.\n");

exit(1);

}

}while (iSelection <= 4);

}

}

void AddEntry (phone * phonebook)

{

pWrite = fopen("phonebook_contacts.dat", "a");

if ( pWrite == NULL )

{

perror("The following error occurred ");

exit(EXIT_FAILURE);

}

else

{

counter++;

realloc(phonebook, sizeof(phone));

printf("\nFirst Name: ");

scanf("%s", phonebook[counter-1].FirstName); printf("Last

Name: ");

scanf("%s", phonebook[counter-1].LastName);

printf("Phone Number (XXX-XXX-XXXX): ");

scanf("%s", phonebook[counter-1].PhoneNumber);

printf("\n\tFriend successfully added to Phonebook\n");

fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-

1].FirstName,

Page 46: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 46 of 59

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;

}

}

Page 47: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 47 of 59

}

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

Page 48: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 48 of 59

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

S. No Input Output Signature

RESULT:

Thus the implementation of telephone directory system has been implemented and

verified successfully.

Page 49: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 49 of 59

Ex. No. 17 IMPLEMENTATION OF BANKING APPLICATION

PROBLEM STATEMENT:

Count the number of account holders whose balance is less than the minimum

balance using sequential access file.

AIM:

To write a C program to implement the banking system.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include necessary header files and declare all required variables.

STEP 3: Display the list of options to the user as a menu and get the user input.

STEP 4: Based the user input, validate the values given by the user and do the calculation.

STEP 5: Display the status and result of the requested operation.

STEP 6: Repeat the steps from 3 to 5 until user exits from the program.

STEP 7: 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];

};

Page 50: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 50 of 59

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

Page 51: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 51 of 59

fwrite(&acc,sizeof(acc),1,fp);

fclose(fp);

break;

case '2' :

fp=fopen("acc.dat","r");

if(fp==NULL)

printf("\nFile is Empty");

else

{

printf("\nA/c Number\tA/c Holder Name

Balance\n");

while(fread(&acc,sizeof(acc),1,fp)==1)

printf("%-10s\t\t%-

20s\t%s\n",acc.no,acc.name,acc.balance);

fclose(fp);

}

break;

case '3' :

fflush(stdin);

flag=0;

fp=fopen("acc.dat","r+");

printf("\nEnter the Account Number :");

gets(ano);

for(pos1=ftell(fp);fread(&acc,sizeof(acc),1,fp)==1;pos1=ftel

l(fp))

{

if(strcmp(acc.no,ano)==0)

{

printf("\nEnter the Type 1 for deposit & 2 for

withdraw : ");

scanf("%d",&type);

printf("\nYour Current Balance is :

%s",acc.balance);

printf("\nEnter the Amount to transact : ");

fflush(stdin);

Page 52: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 52 of 59

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)

Page 53: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 53 of 59

{

bal = atof(acc.balance);

if(bal<MINBAL)

flag++;

}

printf("\nThe Number of Account Holder whose Balance

less than

the Minimum Balance : %d",flag);

fclose(fp);

break;

case '5' :

fclose(fp);

exit(0);

}

printf("\nPress any key to continue....");

getch();

}while (choice!='5');

}

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the implementation of Banking System has been implemented and verified

successfully.

Page 54: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 54 of 59

Ex. No. 18 IMPLEMENTAION OF RAILWAY RESERVATION SYSTEM

PROBLEM STATEMENT:

Create a Railway reservation system with the following modules:

Booking

Availability checking

Cancellation

Prepare chart

AIM:

To develop an application in C for railway ticket reservation system.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include necessary header files and declare all required variables.

STEP 3: Display the list of options to the user as a menu as follows,

i. Booking

ii. Availability checking

iii. Cancellation

iv. Prepare chart

STEP 4: Get an input from the user.

STEP 5: Based the user input, validate the values given by the user and do the calculation.

STEP 6: Display the status and result of the requested operation.

STEP 7: Repeat the steps from 3 to 5 until user exits from the program.

STEP 8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

int first=5,second=5,third=5;

struct node

{

int ticketno;

Page 55: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 55 of 59

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.third

class\n");

printf("enter the option");

scanf("%d",&c);

switch(c)

{

case 1:

if(first>0)

{

Page 56: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 56 of 59

printf("seat available\n");

first--;

}

else

printf("seat not available");

break;

case 2:

if(second>0)

{

printf("seat available\n");

second--;

}

else

printf("seat not available");

break;

case 3:

if(third>0)

{

printf("seat available\n");

third--;

}

else

printf("seat not available");

break;

default:

break;

}

}

void cancel()

{

int c;

printf("cancel\n");

printf("which class you want to cancel");

printf("\n1.first class\n2.second class\n3.third

class\n");

Page 57: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 57 of 59

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

Page 58: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 58 of 59

cheking\n3.cancel\n4.Chart \n

5. Exit\nenter your option:");

scanf("%d",&n);

switch(n)

{

case 1:

booking();

break;

case 2:

availability();

break;

case 3:

cancel();

break;

case 4:

chart();

break;

case 5:

printf(“\n Thank you visit again!”);

getch();

exit(0);

default:

break;

}

}

getch();

}

Page 59: Ex. No. 1 I/O STATEMENTS AND EXPRESSIONSsaranathan.ac.in/attachments/CS8261_C_Programming_Lab_Record_… · CS8261 - C Programming Laboratory / II Sem CSE / R2017 Dept. of CSE / Saranathan

CS8261 - C Programming Laboratory / II Sem CSE / R2017

Dept. of CSE / Saranathan College of Engineering Page 59 of 59

TEST CASES:

S. No Input Output Signature

RESULT:

Thus the implementation of Railway ticket reservation system has been

implemented and verified successfully.