0% found this document useful (0 votes)
15 views

C Lab

Uploaded by

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

C Lab

Uploaded by

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

K.L.N.

COLLEGE OF ENGINEERING
(An Autonomous Institution, Affiliated to University, Chennai)
Pottapalayam.P.O, Sivagangai -630611.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

20CS2L1-C PROGRAMMING LABORATORY MANUAL

I YEAR / II SEMESTER
2022-2023 (EVEN)

PREPARED BY:

Mrs.S.Maria Sobana,AP/CSE

Mrs.S.Yoheswari,AP/CSE

Mrs.K.Vanithamani,AP/CSE
SYLLABUS

20CS2L1 C PROGRAMMING LABORATORY LTPC


0042
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 PROGRAMS

1. Programs using I/O statements, expressions and decision-making constructs.


2. Program for finding given year is leap year or not and finding given number is Armstrong
number or not.
3. Design a calculator to perform the operations namely, addition, subtraction, multiplication,division
and square of a number.
4. 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,itsweight>,<36,its
weight><89,its weight>
5. Matrix addition and subtraction
6. Matrix multiplication and transpose of a matrix
7. Program using string with and without using string functions: string copy and Reverse theString.
8. Convert the given decimal number into binary, octal and hexadecimal numbers using userdefined
functions.
9. 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.
10. Program using recursion – factorial and Fibonacci series
11. Sort the list of numbers using pass by reference.
12. Generate salary slip of employees using structures and pointers.
13. Insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.
14. Count the number of account holders whose balance is less than the minimum balanceusing
sequential access file.
15. Mini project (Any one project: Maximum 4 per Team)
 Railway reservation system
 Library Management System
 University Result Publication System
 Hospital Management System
 Student Automation System
 Payroll System
 Banking System
 Inventory System

PLATFORM NEEDED: Turbo C++ Compiler TOTAL: 60 PERIODS

OUTCOMES:
AT THE END OF THE COURSE, LEARNERS WILL BE ABLE TO:
 Develop simple programs using decision making and looping statements.
 Utilize array concepts to perform matrix addition, subtraction and multiplication.
 Utilize string operations and develop programs to show string copy and reverse.
 Develop programs using user defined functions, built-in functions and recursion.
 Design applications using sequential and random access files.
 Design simple real time projects using the concepts of structures and union.

STAFF INCHARGE HOD/CSE


EX.NO:1a PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

DATE:

(a)For a given product with a VAT OF 7%,find the VAT amount and the total value of the
product.

AIM:
To find the VAT amount and the total value of the product.

ALGORITHM:
1.Start the program.
2.Read the item Details.
3.Read discount and tax % details.
4.Calculate amountqty x value.
5.Calculate discount(amount x discount)/100.
6.Calculate taxamt(subtot x tax)100
7.Calculate totamt(subtot + taxamt)
8.Print all Details
9.Stop the Program

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
float totamt, amount,subtot,disamt,taxamt,quantity,value,discount,tax;
clrscr();
printf("\n Enter the quantity of item sold:");
scanf("%f",&quantity);
printf("\n Enter the value of item:");
scanf("%f",&value);
printf("\n Enter the Discount percentage:");
scanf("%f",&discount);
printf("\n Enter the tax:");
scanf("%f",&tax);
amount=quantity * value;
disamt=(amount*discount)/100.0;
subtot=amount-disamt;
taxamt=(subtot*tax)/100.0;
totamt=subtot+taxamt;
printf("\n\n\n ******BILL****** ");
printf("\nQuantitysold: %f", quantity);
printf("\npriceperitem: %f", value);
printf("\nAmount: %f", amount);
printf(" \n Discount: - %f", disamt) ;
printf("\n Discounted Total: %f", subtot) ;
printf("\n Tax:=+ %f", taxamt);
printf("\n Total Amount %f", totamt);
getch();
}
RESULT:
Thus the program for a given product with a VAT OF 7% to find the VAT Amount and
the total value of the product was executed and the output was verified.
EX.NO:1b PROGRAM USING I/O STATEMENTS AND EXPRESSIONS

DATE:

(b)Write a C Program to find area and perimeter of Circle.

AIM:
To write a C Program to find area and perimeter of Circle.

ALGORITHM:
1.Start the program.
2.Read radius.
3.AreaP1 * Radius * Radius , PI=3.14
4.Perm2 * PI * Radius
5.Print area Prem
6.Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
float r,area,perimeter;
clrscr();
printf("\n Enter r value:");
scanf("%f",&r);
area=pi*r*r;
printf(" area%f:",area);
perimeter=2*pi*r;
printf("perimeter%f:",perimeter);
getch();
}

RESULT:
Thus the program to find area and perimeter of Circle was executed and the output was
verified.
EX.NO:1(c) PROGRAMS USING DECISION-MAKING CONSTRUCTS

DATE:

(c)Write a C Program to Find the Eligibility of a Person to Vote or Not?

AIM:
To Write a C Program to find the eligibility of a Person to Vote or not?

ALGORITHM:
1.Start the program.
2.Read name, age.
3.IF age>=18 print “Eligible”.
4.ELSE print “Not Eligible”.
5.ENDIF.
6.Stop the Program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
char name[50]; // or *name
clrscr();
printf("\n Type the Name of the candidate: ");
gets(name);
printf("\n Enter The Age : ");
scanf("%d",&age);
if(age>=18)
printf("\n %s is Eligible for Vote",name);
else
printf("\n %s is Not Eligible for Vote",name);
getch();
}

RESULT:
Thus the program to find the eligibility of a Person to Vote or not was executed and the
output was verified.
EX.NO:1(d) PROGRAMS USING DECISION-MAKING CONSTRUCTS

DATE:

(d)Write a C Program to find the Largest Numbers Among Three Numbers.

AIM:
To Write a C Program to find the Largest Numbers among Three Numbers.

ALGORITHM:

1.Start the program.


2.Read a, b ,c.
3.IF(a>b and a>c) big = a.
4.ELSEIF (b>a and b>c) big =b.
5.ELSE big = c.
5.ENDIF.
6.print big.
6.Stop the Program.

PROGRAM:

#include <stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
int big;
clrscr();
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big=a;
else if(b>a && b>c)
big=b;
else
big=c;
printf("Largest number is = %d",big);
return 0;
getch();
}

RESULT:
Thus the program to find the Largest Numbers among Three Numbers was executed and
the output was verified.
EX.NO:2(a) WRITE A PROGRAM TO FIND WHETHER THE GIVEN YEAR IS LEAP YEAR OR
NOT.
DATE:

AIM:
To Write a program to find whether the given year is leap year or not.

ALGORITHM:
1.Start the program.
2.Read Year.
3.IF Year%4=0.
Step-3.1 IF Year%100=0.
Step-3.1.1 IF Year%400=0.
Step-3.1.2 Print “Leap Year”.
Step-3.1.3 ELSE Print “Not Leap Year.
Step-3.2 ELSE Print “Leap Year”.
4.Print “Not Leap Year”.
5.Stop.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}

RESULT:
Thus the program to find whether the given year is leap year or not was executed and the
output was verified.
AIM:
EX.NO:2(b) CHECK WHETHER A GIVEN NUMBER IS ARMSTRONG NUMBER OR NOT?
To write a C Program to Check whether a given number is Armstrong number or not .
DATE:
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:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
clrscr();
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");
getch();
}

RESULT:
Thus a C Program for Armstrong number checking was executed and the output was
obtained.
EX.NO:3 DESIGN A CALCULATOR TO PERFORM THE OPERATIONS, NAMELY,
AIM: ADDITION,SUBTRACTION, MULTIPLICATION,
DATE: Design a calculator to perform the operation,
DIVISION namely, addition,
AND SQUARE subtraction,multiplication,
OF A NUMBER.
division and square of a number.

ALGORITHM:

1.Start the program.


2.Read a, b ,c.
3.print menu
4.read choice
5.switch(ch):
5.1 Add
5.1.1 Resut->a+b
5.1.2 print result
5.2 subract
5.2.1 Resut->a-b
5.2.2 print result
5.3 multiply
5.3.1 resut->a*b
5.3.2 print result
5.4 divide
5.4.1 result->a/b
5.4.2 print result
5.5 square
5.5.1 result->a*a
5.5.2 result1->b*b
5.5.3 print result
6.stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,result,sq1,sq2,ch;
float divide;
clrscr();
printf("Enter two integers:");
scanf("%d%d",&a,&b);
printf("1.add,2.subtract,3.multiply,4.divide,5.square");
printf("\n Enter the choice;");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
result=a+b;
printf("Sum=%d\n",result);
break;
}
case 2:
{
result=a-b;
printf("Difference=%d\n",result);
break;
}
case 3:
{
result=a*b;
printf("Multiplication=%d\n",result);
break;
}
case 4:
{
result=a/(float)b;
printf("Division=%.2f\n",result);
break;
}
case 5:
{
sq1=a*a;
printf("Square=%d\n",sq1);
sq2=b*b;
printf("Second square=%d\n",sq2);
break;
}

}
getch();
}

RESULT:
Thus the program to Design a calculator to perform the operation, namely, addition,
subtraction, multiplication, division and square of a number was executed and the output was
verified.
EX.NO:4AIM: SORT THE NUMBERS BASED ON THE WEIGHT
To write a C Program to perform the following: Given a set of numbers like <10, 36, 54,
DATE: 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<conio.h>
#include<math.h>
void main (){
int narray[50],warray[50],nelem,i,j,t;
clrscr();
printf("\n enter the number of elements in an array:");
scanf("%d",&nelem);
printf("\n enter the %d elements \n",nelem);
for(i=0;i<nelem;i++){
scanf("%d",&narray[i]);
}
for(i=0;i<nelem;i++){
warray[i]=0;
if(percube(narray[i])){
warray[i]=warray[i]+5;
}
if(narray[i]%4==0&&narray[i]%6==0){
warray[i]=warray[i]+4;
}
if(prime(narray[i])){
warray[i]=warray[i]+3;
}
}
for(i=0;i<nelem;i++){
for(j=0;j<nelem-1;j++){
if(warray[i]<warray[j]){
int t1;
t=warray[j];
t1=narray[j];
warray[j]=warray[i];
narray[j]=narray[i];
warray[i]=t;
narray[i]=t1;
}
}
}
for(i=0;i<nelem;i++){
printf("<%d,%d>\n",narray[i],warray[i]);
getch();
}
}
int prime(int num){
int i,flag=1;
for(i=2;i<=num;i++){
if(num%i==0){
flag=0;
break;
}
return flag;
}
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.
AIM:
EX.NO:5(a) MATRIX ADDITION
To write a C Program to perform addition of two matrices using an array.
DATE:
ALGORITHM:

1. Start.
2. Declare and initialize 2 two-dimensional arrays A and B.
3. Calculate the number of rows and columns present in the array a (as dimensions of both
the arrays are same) and store it in variables rows and columns respectively.
4. Declare another array C with the similar dimensions.
5. Loop through the arrays A and B, add the corresponding elements.
6. Display the elements of array C.
7. Stop.

PROGRAM:

#include <stdio.h>
int main()
{
int A[10][10],B[10][10],C[10][10],i,j,row,col;
printf("\n Enter row and column: ");
scanf("%d %d",&row,&col);
printf("\n Enter A matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\n Enter B matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("\n Sum of the two matrices: \n ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t",C[i][j]);
}
printf("\n");
}
return 0;
}

RESULT:
Thus a C Program to perform addition of two matrices using an array was executed and
the output was obtained.
AIM:
EX.NO:5(b) MATRIX SUBTRACTION
To write a C Program to perform Subtraction of two matrices using an array.
DATE:

ALGORITHM:

1. Start.
2. Declare and initialize 2 two-dimensional arrays A and B.
3. Calculate the number of rows and columns present in the array a (as dimensions of both
the arrays are same) and store it in variables rows and columns respectively.
4. Declare another array C with the similar dimensions.
5. Loop through the arrays A and B, subtract the corresponding elements.
6. Display the elements of array C.
7. Stop.

PROGRAM:

#include <stdio.h>
int main()
{
int A[10][10],B[10][10],C[10][10],i,j,row,col;
printf("\n Enter row and column: ");
scanf("%d %d",&row,&col);
printf("\n Enter A matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\n Enter B matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("\n Sum of the two matrices: \n ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
C[i][j]=A[i][j]-B[i][j];
printf("%d \t",C[i][j]);
}
printf("\n");
}
return 0;
}

RESULT:
Thus a C Program to perform Subtraction of two matrices using an array was executed
and the output was obtained.
AIM:
EX.NO:6(a) MATRIX MULTIPLICATION
To write a C Program to perform multiplication of two matrices using an array.
DATE:
ALGORITHM:

1. Start.
2. Declare and initialize 2 two-dimensional arrays first and second.
3. Calculate the number of rows and columns present in the array a (as dimensions of both
the arrays are same) and store it in variables rows and columns respectively.
4. Declare another array multiply with the similar dimensions.
5. Loop through the arrays first and second, multiply the corresponding elements.
6. Display the elements of array multiply.
7. Stop.

PROGRAM:

#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

RESULT:
Thus a C Program to perform multiplication of two matrices using an array was executed
and the output was obtained.
AIM:
EX.NO:6(b) TRANSPOSE OF A MATRIX
To write a C Program to perform Transpose of a matrix using an array.
DATE:
ALGORITHM:

1. Start.
2. Declare an array.
3. Initialize the array.
4. Declare a transpose matrix.
5. Call a function that will perform the transpose operation.
6. Store the elements in the transpose matrix.
7. Now, print the elements in the transpose matrix.
8. Stop

PROGRAM:

#include <stdio.h>

int main()

int m, n, i, j, matrix[10][10], transpose[10][10];

printf("Enter rows and columns :\n");

scanf("%d%d", &m, &n);

printf("Enter elements of the matrix\n");

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

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

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

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

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

transpose[j][i] = matrix[i][j];

printf("Transpose of the matrix:\n");

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


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

printf("%d\t", transpose[i][j]);

printf("\n");

return 0;

RESULT:
Thus a C Program to perform transpose of a matrix using an array was executed and the
output was obtained.
AIM:
EX.NO:7(a) To write a C Program to Copy a String using aOPERATIONS
STRING String Function.
String Copy with String Function
DATE: ALGORITHM:

1. Start
2. Declare str1 and str2 using an array.
3. Using gets function read str1.
4. Copy the source sting str1 to the destination string str2 by using a string function strcpy().
5. Display str2.
6. Stop

PROGRAM:

#include<stdio.h>
#include<string.h>
void main()
{
char str1[100],str2[50];
printf("Enter string str1\n");
gets(str1);
strcpy(str2,str1);
printf("Copied String(str2) is %s",str2);
}

RESULT:
Thus a C Program to Copy a String using a String Function was executed and the output
was obtained.
AIM:
EX.NO:7(b) STRING OPERATIONS
To write a C Program to CopyString
a String without
Copy using
without a String
String Function.
Function
DATE:
ALGORITHM:

1. Start
2. Declare str and copy_str using an array.
3. Declare i.
4. Read str using scanf function.
5. Traverse the given str using a for loop.
6. Assign str[] to copy_str[].
7. Display copy_str.
8. Stop

PROGRAM:

#include<stdio.h>
int main()
{
char str[100],copy_str[100];
int i;
printf("Enter a string\n");
scanf("%s",str);
for(i = 0; str[i] != '\0'; i++)
copy_str[i] = str[i];
copy_str[i] = '\0';
printf("Copied string = %s\n",copy_str);
return 0;
}

RESULT:
Thus a C Program to Copy a String without using a String Function was executed and the
output was obtained.
AIM:
EX.NO:7(c) STRING OPERATIONS
To write a C Program to reverse a String
String usingwith
Reverse a String Function.
String Function
DATE:
ALGORITHM:

1. Start
2. Declare str using an array.
4. Read str using scanf function.
5. Reverse the str using strrev() function.
7. Display the reversed string.
8. Stop

PROGRAM:

#include <stdio.h>
#include <string.h>
int main()
{
char str[40];
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}

RESULT:
Thus a C Program to reverse a string using a String Function was executed and the output
was obtained.
AIM:
EX.NO:7(d) STRING OPERATIONS
To write a C Program to perform
Stringreverse without
Reverse changing
without Stringthe position of special
Function
DATE: 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()
{
charstr[100];
void reverse(char *);
intisAlpha(char);
void swap(char *a ,char *b);
clrscr();
printf("Enter the Given String : ");
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--;
}
}
}
intisAlpha(char x)
{
return ( (x >= 'A'&& x <= 'Z') ||
(x >= 'a'&& x <= 'z') );
}

RESULT:

Thus a c program of given string was executed and output was obtained.
AIM:
EX.NO:8 To write a C Program to perform number
NUMBER conversions using an user-defined functions.
CONVERSIONS

DATE: ALGORITHM:

1. Start.
2. Declare variables.
3. Read a binary, octal and hexadecimal numbers.
4. Use convert function as to convert the given decimal number into binary, octal and
hexadecimal number.
5. Call the convert function along with the number and base value.
6. Display the result.
7. Stop.

PROGRAM:

#include<stdio.h>
void convert(int, int);
int main()
{
int num;
printf("Enter a positive decimal number : ");
scanf("%d", &num);
printf("\nBinary number :: ");
convert(num, 2);
printf("\n");
printf("\nOctal number :: ");
convert(num, 8);
printf("\n");
printf("\nHexadecimal number :: ");
convert(num, 16);
printf("\n");
return 0;
}
void convert (int num, int base)
{
int rem = num%base;
if(num==0)
return;
convert(num/base, base);
if(rem < 10)
printf("%d", rem);
else
printf("%c", rem-10+'A' );
}
RESULT:
Thus a C Program to perform number conversions using an user-defined functions was
executed and the output was obtained.
AIM:
EX.NO:9 To write a C Program to perform string operations
STRING on a given paragraph for the following
OPERATIONS
using built-in functions:
DATE: 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 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

PROGRAM:

A. Find the total number of words.

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 100 // Maximum string size
void main()
{
char str[MAX_SIZE];
char prevChar;
int i, words;
clrscr();
/* Input string from user */
printf("\nEnter any string: ");
gets(str);
i = 0;
words = 0;
prevChar = '\0'; // The previous character of str[0] is null
/* Runs loop infinite times */
while(1)
{
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t' || str[i]=='\0')
{
/**
* It is a word if current character is whitespace and
* previous character is non-white space.
*/
if(prevChar != ' ' && prevChar != '\n' && prevChar != '\t' && prevChar != '\0')
{
words++;
}
}
/* Make the current character as previous character */
prevChar = str[i];

if(str[i] == '\0')
break;
else
i++;
}
printf("Total number of words = %d", words);
getch();
}

b. Capitalize the first word of each sentence.

#include<stdio.h>
#include<conio.h>
#define MAX 100
void main()
{
char str[MAX]={0};
int i;
clrscr();
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
//capitalize first character of words
for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet

if(str[i]>='a' && str[i]<='z')


{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it small/lowercase
}
}
printf("Capitalize string is: %s\n",str);
getch();
}

c. Replace a given word with another word.

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char s[] = "All work and no play makes Jack a dull boy.";
char word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("All work and no play makes Jack a dull boy.\n");
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(s);
for (k=0; k<p; k++)
{
if (s[k]!=' ')
{
str[i][j] = s[k];
j++;
}
else
{
str[i][j]='\0';
j=0; i++;
}
}
str[i][j]='\0';
w=i;
for (i=0; i<=w; i++)
{
if(strcmp(str[i],word)==0)
strcpy(str[i],rpwrd);
printf("%s ",str[i]);
}
getch();
}

RESULT:
Thus a C Program String operations was executed and the output was obtained.
AIM:
EX.NO:10(a) RECURSION
To write a C Program to find factorial of a given number using recursion.
Factorial
DATE:
ALGORITHM:

1. Start
2. Declare variables x and n.
3. Read the number.
4. Calculate factorial of the given number using a recursive function fact.
5. Display the result based on the condition.
6. Stop.

PROGRAM:

#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

RESULT:
Thus a C Program to find factorial of a given number using recursion was executed and the
output was obtained.
AIM:
EX.NO:10(b) RECURSION
To write a C Program to generate Fibonacci series using
Fibonacci recursion.
Series
DATE: .

ALGORITHM:

1. Start
2. Declare variables i, c and n.
3. Read the number.
4. Using for loop generate the Fibonacci series for the given numbers.
5. Display the result based on the condition.
6. Stop.

PROGRAM:

#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

RESULT:
Thus a C Program to generate Fibonacci series using recursion was executed and the output was
obtained.
AIM:
EX.NO:11 SORT LIST OF ARRAY ELEMENTS USING FUNCTION & POINTER
To write a C Program to Sort List of Array Elements using Function & Pointer.
DATE:
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>
void sort(int *x);
int main()
{
int a[5], i;
int *pa;
pa = &a[0];

printf("Enter array elements: ");


for(i=0;i<5;i++)
{
scanf("%d",pa+i);
}

sort( &a[0] );

printf("Sorted array is: ");


for(i=0;i<5;i++)
{
printf("%d\t", *(pa+i));
}

return 0;
}

void sort(int *x)


{
int i, j, temp;

for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if( *(x+i) > *(x+j) )
{
temp = *(x+i);
*(x+i) = *(x+j);
*(x+j) = temp;
}
}
}
}

RESULT:
Thus a C Program to Sort List of Array Elements using Function & Pointer was executed
and the output was obtained.
AIM:
EX.NO:12 To write a C Program
GENERATE SALARYto Generate
SLIP OFsalary slip of employees
EMPLOYEES USINGusing structures andAND
STRUCTURES pointers.
POINTERS

DATE: 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>
struct emp
{
int empno ;
char name[10] ;
nt bpay, allow, ded, npay ;
} e[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &e[i].empno) ;
printf("\nEnter the name : ") ;
scanf("%s", e[i].name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%d %d %d", &e[i].bpay, &e[i].allow, &e[i].ded) ;
e[i].npay = e[i].bpay + e[i].allow - e[i].ded ;
}
printf("\nEmp. No. Name \t Bpay \t Allow \t Ded \t Npay \n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", e[i].empno,e[i].name, e[i].bpay, e[i].allow, e[i].ded,
e[i].npay) ;
}
getch() ;
}

RESULT:
Thus a C Program Salary slip of employees was executed and the output was obtained.
AIM:
EX.NO:13 TELEPHONE DIRECTORY
To write a C Program to add, delete , display ,Search and exit options for telephone
DATE: 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 <conio.h>
#include <string.h>
struct person
{
char name[20];
long telno;
};
void appendData()
{
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","a");
printf("*****Add Record****\n");
printf("Enter Name : ");
scanf("%s",obj.name);
printf("Enter Telephone No. : ");
scanf("%ld",&obj.telno);
fprintf(fp,"%20s %7ld",obj.name,obj.telno);
fclose(fp);
}
void showAllData(){
FILE *fp;
struct person obj;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display All Records*****\n");
printf("\n\n\t\tName\t\t\tTelephone No.");
printf("\n\t\t=====\t\t\t==========\n\n");
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
printf("%20s %30ld\n",obj.name,obj.telno);
}
fclose(fp);
getch();
}
void findData()
{
FILE *fp;
struct person obj;
char name[20];
int totrec=0;
clrscr();
fp=fopen("data.txt","r");
printf("*****Display SpecificRecords*****\n");
printf("\nEnter Name : ");
scanf("%s",&name);
while(!feof(fp))
{
fscanf(fp,"%20s %7ld",obj.name,&obj.telno);
if(strcmpi(obj.name,name)==0){
printf("\n\nName : %s",obj.name);
printf("\nTelephone No : %ld",obj.telno);
totrec++;
}
}
if(totrec==0)
printf("\n\n\nNo Data Found");
else
printf("\n\n===Total %d Record found===",totrec);
fclose(fp);
getch();
}
void main(){
char choice;
while(1){
clrscr();
printf("*****TELEPHONE DIRECTORY*****\n\n");
printf("1) Append Record\n");
printf("2) Find Record\n");
printf("3) Read all record\n");
printf("4) exit\n");
printf("Enter your choice : ");
fflush(stdin);
choice = getche();
switch(choice){
case'1' : //call append record
appendData();
break;
case'2' : //call find record
findData();
break;
case'3' : //Read all record
showAllData();
break;
case'4' :
exit(1);
}
}
}

RESULT:
Thus a C Program was executed and the output was obtained.
AIM:
EX.NO:14 BANKING APPLICATION
To write a C Program to Count the number of account holders whose balance is less than
DATE: 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 choose to stop.
8. Stop

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. Delete All\n");
printf("6. 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' :
remove("acc.dat");
break;
case '6' :
fclose(fp);
exit(0);

}
printf("\nPress any key to continue....");
getch();
} while (choice!='6');
}

RESULT:
Thus a C Program for Banking Application was executed and the output was obtained.
AIM:
EX.NO:15 MINI PROJECT CREATE A ―RAILWAY RESERVATION SYSTEM.
Create a Railway reservation system in C with the following modules
DATE:  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].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();
}

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

You might also like