0% found this document useful (0 votes)
21 views110 pages

R23 CP LAB MANUAL

The document is a lab manual for the Computer Programming course (23CS102) at Audisankara College of Engineering and Technology, detailing course outcomes and a comprehensive list of programming experiments using the C language. It includes tasks such as writing programs for basic operations, data structures, file handling, and algorithms. The manual aims to equip students with practical programming skills and understanding of C language concepts.

Uploaded by

temp36716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views110 pages

R23 CP LAB MANUAL

The document is a lab manual for the Computer Programming course (23CS102) at Audisankara College of Engineering and Technology, detailing course outcomes and a comprehensive list of programming experiments using the C language. It includes tasks such as writing programs for basic operations, data structures, file handling, and algorithms. The manual aims to equip students with practical programming skills and understanding of C language concepts.

Uploaded by

temp36716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

AUDISANKARA COLLEGE

OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
(AUTONOMOUS)

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL
OF
COMPUTER PROGRAMMING
23CS102
AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
(AUTONOMOUS)

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

COMPUTER PROGRAMMING
(23CS102)
LAB MANUAL

Prepared by: Verified by:


AUDISANKARA COLLEGE OF ENGINEERING & TECHNOLOGY
(AUTONOMOUS)

I B.Tech. I Sem P C
3 1.5
Computer Programming Lab (23CS102)
(Common to All Branches of Engineering)

Course Outcomes:
At the end of the course, the student will be able to

CO1: Read, Understand and Trace the execution of programs written in C language.
CO2: Select the right control structure for solving the problem.
CO3: Develop C program which utilize memory efficiently using programming constructs
like pointers.
CO4: Develop, Debug and Execute programs to demonstrate the applications of arrays, functions,
basic concepts of pointer in C.
CO5: Develop C programs to handle File handling operations.

List of Experiments/Tasks

1. Illustration of DOS Commands.


2. Writing a simple program using printf(), scanf() statements.
3. Design an algorithm and flowchart for Sum and average of 3 numbers.
4. Design an algorithm and flowchart for Conversion of Fahrenheit to Celsius and vice
versa.
5. Design an algorithm and flowchart for Simple interest calculation.
6. Write a program to find distance travelled by an object.
7. Write a program to evaluate the given expressions.
8. Write a program to find the square root of a given number.
9. Write a program to find the maximum of three numbers using conditional operator.
10. Write a program to take marks of 5 subjects in integers, and find the total, average in
float.
11. Write a C program to find the max and min of four numbers using if-else.
12. Write a C program to generate electricity bill.
13. Write a C program to simulate a calculator using switch case.
14. Write a C program to find the given year is a leap year or not.
15. Write a program to find the factorial of given number using any loop.
16. Write a program to find the given number is a prime or not.
17. Write a program to check whether a given number is palindrome or not.
18. Write a program to construct a pyramid of numbers.
19. Write a program to find Min and Max of a 1-D integer array.
20. Write a program to perform linear search on 1-D array.
21. Write a program to print reverse of a 1-D integer array.
22. Write a program to find 2’s complement of the given binary number.
23. Write a program to eliminate duplicate elements in an array.
24. Write a program to perform Addition of two matrices.
25. Write a program to perform Multiplication of two matrices.
26. Write a program to sort array elements using bubble sort.
27. Write a program to concatenate two strings without built-in functions.

ASCET(Autonomous) R23- CP LAB MANUAL Page 2


28. Write a program to print Reverse a string using built-in functions.
29. Write a program to print Reverse a string without using built-in functions.
30. Write a C program to find the sum of a 1-D array using malloc() function.
31. Write a C program to find the total, average of n students using structures.
32. Write a program to enter n students data using calloc() and display failed students list.
33. Write a program to read student name and marks from the command line and display
the student details along with the total.
34. Write a C program to implement realloc() function.
35. Write a C program to create and display a singly linked list using self-referential
structure.
36. Write a program to demonstrate the differences between structure and union.
37. Write a C program to shift/rotate using bit fields.
38. Write a C program to copy one structure variable to another structure of the same
type.
39. Write a C program to swap two numbers using call by reference.
40. Demonstrate Dangling pointer problem using a C program.
41. Write a C program to copy one string into another using pointer.
42. Write a C program to find no of lowercase, uppercase, digits and other
characters using pointers.
43. Write a program to find compound interest value.
44. Write a program to find Area of a triangle using heron’s formulae.
45. Write a program to find the roots of the quadratic equation.
46. Write a program to compute Sine series.
47. Write a program to compute Cosine series.
48. Write a C function to calculate NCR value.
49. Write a C function to find the length of a string.
50. Write a C function to transpose of a matrix.
51. Write a C function to demonstrate numerical integration of differential equations
using Euler’s method.
52. Write a recursive function to generate Fibonacci series.
53. Write a recursive function to find the LCM of two numbers.
54. Write a recursive function to find the factorial of a number.
55. Write a C Program to implement Ackermann function using recursion.
56. Write a recursive function to find the sum of series.
57. Write a C program to write and read text into a file.
58. Write a C program to write and read text into a binary file using fread() and fwrite()
functions.
59. Write a program to copy the contents of one file to another file.
60. Write a C program to merge two files into the third file using command-line
arguments.
61. Write a program to find no. of lines, words and characters in a file.
62. Write a C program to print last n characters of a given file.

***

ASCET(Autonomous) R23- CP LAB MANUAL Page 3


SNO Name of the Experiment PageNo

1 Illustration of DOS Commands 07


2 Simple program using printf(), scanf() statements 09
3 Algorithm and flowchart for Sum and average of 3 10
numbers
4 Algorithm and flowchart for conversion of Fahrengheit to 11
Celsius and vice versa
5 Algorithm and flowchart for simple interest calculation 12
6 Distance traveled by an object 13
7 Evaluate the given expressions 14
8 Square root of a given number 16
9 Maximum of three numbers using conditional operator 17
10
Acces 5 subects in integers, and find the total, average in 18
float
11 Max and min of four numbers using if-else 19
12 Electricity bill generation 21
13 Simulate a calculator using switch case 23
14 Leap year checking 26
15 Factorial of a given number 27
Prime number checking
16 28
17 Palindrome number checking 29
18 Pyramid of numbers 30
19 Min and Max of a 1-D integer array 31
20 Linear search technique 33
21 Reverse the elements of a 1-D array 35
22 2’s complement of a given binary number 37

ASCET(Autonomous) R23- CP LAB MANUAL Page 4


SNO Name of the Experiment PageNo

23 Remove duplicate elements from an array 39


24 Addition of two matrices 41
25 Multiplication of two matrices 44
26 Bubble sort technique 47
27 Concatenation of two strings without built-in functions 49
28 Reverse of a string using built-in functions 50
29 Reverse of a string without using built-in functions 51
30 Sum of 1-D array using malloc( ) function 52
31 Total, average of n students using structures 53
32 Enter N students data using calloc( ) and display failed 56
students
33 Read student name and marks from command line and 58
display the student details with the total
34
Realloc( ) function 59
35 Create and display a single linked list using self-referential 60
structure
36 Difference between structure and union 64
37 Shift / rotate bit fields 65
Copy one structure variable to another variable of same type
38 66
39 Swap two numbers using call by reference 67
40 Dangling pointer 68
41 Copy one string into another string using pointer 69
42 Number of lowercase, uppercase, digits and other 70
characters using pointers
43 Compound interest value 72

ASCET(Autonomous) R23- CP LAB MANUAL Page 5


SNO Name of the Experiment PageNo

44 Area of a triangle using heron’s formula 73


45 Roots of a quadratic equation 74
46 Sine series 76
47 Cosine series 78
48 Function to calculate NCR value 80
49 Function to find length of a string 82
50 Function to transpose of a matrix 83
51 Numerical integration of differential equation using 86
Euler’s method
52 Recursive function to generate Fibonacci series 89
53
Recursive function to find the LCM of two numbers 91
54 Recursive function to find the factorial of a number 93
55 Ackermann function using recursion 94
56 Recursive function to find the sum of series 96
57 Write and read text into a file 97
58 Write and read text into a binary file 98
Copy the contents of one file to another file
59 101
60 Merge two files into the third file using command 103
line arguments
61 Number of lines, words and characters in a file 105
62 Print last n characters of a given file 108

***

ASCET(Autonomous) R23- CP LAB MANUAL Page 6


Program No: 1
Illustration of DOS Commands.

Description

DOS stands for Disk Operating System. Some of the important DOS commands are: DIR,
DIR/P, DIR/P/W, MD, CD, COPY CON, TYPE, COPY, DEL, RD etc.,

DIR: DIR command is used to display the directory contents of the existing drive.

Syntax: DIR
Example: DIR

DIR/P: DIR/P command is used to display the directory contents of the


existing drive page wise.

Syntax: DIR / P
Example: DIR / P

DIR/P/W: DIR/P command is used to display the directory contents of the existing drive
page and width wise.
Syntax: DIR /P/W
Example: DIR /P/W

MD (Make Directory): MD command is used to create a directory in the existing drive


with the specified name.
Syntax: MD DirectoryName
Example: MD SHABBIR

CD (Change Directory): CD command is used to enter into the specified directory.


Syntax: CD DirectoryName
Example: CD SHABBIR

COPY CON: COPY CON command is used to create a file with the specified name
for storing data.
Syntax: COPY CON FileName
Example: COPY CON cse

After typing data into the specified file press Ctrl + Z for save and exit from the file.

TYPE: TYPE command is used to display the contents of the file.


Syntax: TYPE FileName
Example: TYPE cse

ASCET(Autonomous) R23- CP LAB MANUAL Page 7


COPY: COPY command is used to copy the contents of one file into another
file.
Syntax: COPY OldFileName NewFileName
Example: COPY cse ece

DEL: DEL command is used to delete the specified file from the current directory.
Syntax: DEL FileName
Example: DEL ece

RD(Remove Directory): RD command is used to delete the directory from


the existing drive.
Syntax: RD DirectoryName
Example: RD SHABBIR

ASCET(Autonomous) R23- CP LAB MANUAL Page 8


Program No: 02
Writing simple program using printf(), scanf() statements.

Algorithm:

Step 1: START
Step 2: INPUT two values as X and Y
Step 3: WRITE X and Y Values
Step 4: Temp ← X
X←Y
Y ← Temp
Step 5: WRITE X and Y Values
Step 6: STOP

Program:

/* SWAPPING OF GIVEN TWO NUMBERS */

#include<stdio.h>
#include<conio.h>

void main()
{
int X,Y,Temp;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
printf("\n Before Swapping Values Are = %d %d",X,Y);
Temp=X;
X=Y;
Y=Temp;
printf("\n After Swapping Values Are = %d %d",X,Y);
}

Result:

Enter Two Values = 10 20

Before Swapping Values Are = 10 20


After Swapping Values Are = 20 10

ASCET(Autonomous) R23- CP LAB MANUAL Page 9


Program No: 03
Design an algorithm and flowchart for Sum and average of 3 numbers.

Algorithm:

Step 1: START
Step 2: INPUT three values as X,Y and Z
Step 3: Sum ← X + Y + Z
Step 4: Avg ← Sum / 3
Step 5: WRITE Sum , Avg Values
Step 6: STOP

Flowchart:

START

INPUT X, Y and Z

Sum ← X + Y + Z
Avg ← Sum / 3

WRITE Sum, Avg

STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 10


Program No: 04
Design an algorithm and flowchart for Conversion of Fahrenheit to Celsius and vice versa.

Algorithm:

Step 1: START
Step 2: INPUT Fahrenheit value as F
Step 3: C ← (F – 32 ) * (5 / 9 )
Step 4: WRITE C Value
Step 5: INPUT Celsius value as C
Step 6: F ← ( 9 / 5 ) * C + 32
Step 7: WRITE F Value
Step 6: STOP

Flowchart:

START

INPUT F

C ← (F – 32 ) * (5 / 9 )

WRITE C

INPUT C

F ← ( 9 / 5 ) * C + 32

WRITE F

STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 11


Program No: 05
Design an algorithm and flowchart for Simple interest calculation.

Algorithm:

Step 1: START
Step 2: INPUT three values as P, T and R
Step 3: SI ← ( P * T * R ) / 100
Step 4: WRITE SI Value
Step 5: STOP

Flowchart:

START

INPUT P, T and R

SI ← ( P * T * R ) / 100

WRITE SI

STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 12


Program No: 06
Write a program to find distance travelled by an object.

Algorithm:

Step 1: START
Step 2: INPUT Velocity as V, Time as T
Step 3: Dis ← V * T
Step 4: WRITE Dis Value
Step 5: STOP

Program:

/* DISTANCE TRAVELLED BY AN OBJECT */

#include<stdio.h>
#include<conio.h>

void main()
{
float Dis,V,T;
clrscr();
printf("\n Enter Velocity of Vehicle =");
scanf("%f",&V);
printf("\n Enter Time Travelled =");
scanf("%f",&T);
Dis=V*T;
printf("\n Distance Travelled =%.2f KM",Dis);
}

Result

Enter Velocity of Vehicle = 65


Enter Time Travelled = 3.3

Distance Travelled = 214.50 KM

ASCET(Autonomous) R23- CP LAB MANUAL Page 13


Program No: 07
Write a program to evaluate the given expressions.

Algorithm:

Step 1: START
Step 2: INPUT A,B,C,D,E,F and G Values
Step 3: R1 ← A + B * C + ( D * E ) + F * G
WRITE R1 value
Step 4: R2 ← A / B * C – B + A * D / 3
WRITE R2 value
Step 5: R3 ← A++ + B - --A
WRITE R3 value
Step 6: INPUT i value
Step 7: J ← ( i++ ) + ( ++i )
WRITE J value
Step 8: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int A,B,C,D,E,F,G,i,J,R1,R2,R3;
clrscr();
printf("\n Enter A,B,C,D,E,F and G Values =");
scanf("%d%d%d%d%d%d%d",&A,&B,&C,&D,&E,&F,&G);

R1=A+B*C+(D*E)+F*G;
printf("\n Expression Result 1 = %d",R1);

R2=A/B*C-B+A*D/3;
printf("\n Expression Result 2 = %d",R2);

R3=A+++B---A;
printf("\n Expression Result 3 = %d",R3);

printf("\n Enter i Value =");


scanf("%d",&i);
J=(i++)+(++i);
printf("\n Expression Result 4 = %d",J);
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 14


Result

Enter A,B,C,D,E,F and G Values = 2 4 3 2 3 4 2

Expression Result 1 = 28

Expression Result 2 = -3

Expression Result 3 = 4

Enter i Value = 7

Expression Result 4 = 14

ASCET(Autonomous) R23- CP LAB MANUAL Page 15


Program No: 08
Write a program to find the square root of a given number.

Algorithm:

Step 1: START
Step 2: INPUT N value
Step 3: b ← 0.0001
Step 4: REPEAT FOR a ← 0 TO N-1 DO STEPS BY b
IF a*a > N THEN
a←a–b
BREAK
ENDIF
ENDREPEAT
Step 5: WRITE a value
Step 6: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
double a,b,N;
clrscr();
printf("\n Enter A Value = ");
scanf("%lf",&N);
b=0.0001;
for(a=0;a<N;a=a+b)
{
if(a*a > N)
{
a=a-b;
break;
}
}
printf("\n Square Root Value = %.2lf",a);
}

Result

Enter A Value = 25

Square Root Value = 5.00

ASCET(Autonomous) R23- CP LAB MANUAL Page 16


Program No: 09
Write a program to find the maximum of three numbers using conditional operator.

Algorithm:

Step 1: START
Step 2: INPUT three values as X, Y and Z
Step 3: Max ← ( X > Y && X > Z ) ? X : ( Y > Z ? Y : Z )
Step 4: WRITE Max value
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int X,Y,Z,Max;
clrscr();
printf("\n Enter Three Numbers =");
scanf("%d%d%d",&X,&Y,&Z);
Max=(X>Y&&X>Z)?X:(Y>Z?Y:Z);
printf("\n Maximum Value = %d",Max);
}

Result

Enter Three Numbers = 15 45 27

Maximum Value = 45

ASCET(Autonomous) R23- CP LAB MANUAL Page 17


Program No: 10
Write a program to take marks of 5 subjects in integers, and find the total, average in float.

Algorithm:

Step 1: START
Step 2: INPUT five subject marks as S1, S2, S3, S4 and S5
Step 3: SUM ← S1 + S2 + S3 + S4 + S5
AVG ← (float) (SUM / 5)
Step 4: WRITE SUM and AVG values
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int S1,S2,S3,S4,S5,SUM;
float AVG;
clrscr();
printf("\n Enter 5 Subject Marks =");
scanf("%d%d%d%d%d",&S1,&S2,&S3,&S4,&S5);
SUM=S1+S2+S3+S4+S5;
AVG=(float)(SUM/5);
printf("\n Total Marks = %d",SUM);
printf("\n Average Marks = %.2f",AVG);
}

Result

Enter 5 Subject Marks= 75 80 92 85 90

Total Marks = 422

Average Marks = 84.40

ASCET(Autonomous) R23- CP LAB MANUAL Page 18


Program No: 11
Write a C program to find the max and min of four numbers using if-else.

Algorithm:

Step 1: START
Step 2: INPUT four values as P, Q, R and S
Step 3: Max ← P
Min ← P
Step 4: IF Q > Max THEN
Max ← Q
ELSE
IF Q < Min THEN
Min ← Q
ENDIF
ENDIF
Step 5: IF R > Max THEN
Max ← R
ELSE
IF R < Min THEN
Min ← R
ENDIF
ENDIF
Step 6: IF S > Max THEN
Max ← S
ELSE
IF S < Min THEN
Min ← S
ENDIF
ENDIF
Step 7: WRITE Max and Min values
Step 8: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 19


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int P,Q,R,S,Max,Min;
clrscr();
printf("\n Enter Four Values =");
scanf("%d%d%d%d",&P,&Q,&R,&S);
Max=P;
Min=P;
if(Q>Max)
Max=Q;
else if(Q<Min)
Min=Q;
if(R>Max)
Max=R;
else if(R<Min)
Min=R;
if(S>Max)
Max=S;
else if(S<Min)
Min=S;
printf("\n Maximum Number = %d",Max);
printf("\n Minimum Number = %d",Min);

Result

Enter Four Values = 75 80 92 85

Maximum Number = 92

Minimum Number = 75

ASCET(Autonomous) R23- CP LAB MANUAL Page 20


Program No: 12
Write a C program to generate electricity bill.

NO OF UNITS BILL PER UNIT SURCHARGE


0 - 50 Units 5.40 per Unit Rs: 30.00
51 – 100 Units 7.65 per Unit Rs: 40.00
101 – 300 Units 9.25 per Unit Rs: 75.00
Above 300 Units 10.65 per Unit Rs: 150.00

Algorithm:

Step 1: START
Step 2: INPUT Number of Units as Units
Step 3: IF Units ≤ 50 THEN
Amount ← Units * 5.40
SCharge ← 30.00
ELSEIF Units > 50 AND Units ≤ 100 THEN
Amount ← Units * 7.65
SCharge ← 40.00
ELSEIF Units > 100 AND Units ≤ 300 THEN
Amount ← Units * 9.25
SCharge ← 75.00
ELSE
Amount ← Units * 10.65
SCharge ← 150.00
ENDIF
Step 4: Bill ← Amount + SCharge
WRITE Bill value
Step 5: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 21


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int Units;
float Amount,SCharge,Bill;
clrscr();
printf("\n Enter Number of Units Consumed =");
scanf("%d",&Units);
if(Units<=50)
{
Amount=Units*5.40;
SCharge=30.00;
}
else if(Units>50&&Units<=100)
{
Amount=Units*7.65;
SCharge=40.00;
}
else if(Units>100&&Units<=300)
{
Amount=Units*9.25;
SCharge=75.00;
}
else
{
Amount=Units*10.65;
SCharge=150.00;
}
Bill=Amount+SCharge;
printf("\n Bill Amount = %.2f Rs",Bill);
}

Result

Enter Number of Units Consumed = 224

Bill Amount = 2147.00 Rs

ASCET(Autonomous) R23- CP LAB MANUAL Page 22


Program No: 13
Write a C program to simulate a calculator using switch case.

Algorithm:

Step 1: START
Step 2: INPUT Two Values as X and Y
Step 3: REPEAT WHILE TRUE
INPUT Option as ch
SWITCH ch
CASE ' + ' THEN
Z←X+Y
WRITE Z value
BREAK
ENDCASE
CASE ' - ' THEN Z
← X - Y WRITE
Z value BREAK
ENDCASE
CASE ' * ' THEN
Z←X*Y
WRITE Z value
BREAK
ENDCASE
CASE ' / ' THEN Z
← X / Y WRITE
Z value BREAK
ENDCASE
CASE ' % ' THEN
Z ← X MOD Y
WRITE Z value
BREAK
ENDCASE
CASE ' ^ ' THEN
EXIT
ENDCASE
ENDSWITCH
ENDREPEAT
Step 4: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 23


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
int X,Y,Z;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
while(1)
{
printf("\n +: ADDITION\n -: SUBTRACTION\n");
printf(" *: MULTIPLICATION\n /: DIVISION\n");
printf(" %: REMAINDER\n ^: EXIT\n");
printf("\n Enter Your Option =");
scanf(" %c",&ch);
switch(ch)
{
case '+':Z=X+Y;
printf("\n Addition Result = %d",Z);
break;
case '-':Z=X-Y;
printf("\n Subtraction Result = %d",Z);
break;
case '*':Z=X*Y;
printf("\n Multiplication Result = %d",Z);
break;
case '/':Z=X/Y;
printf("\n Division Result = %d",Z);
break;
case '%':Z=X%Y;
printf("\n Remainder Result = %d",Z);
break;
case '^':exit();
}
}
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 24


Result

Enter Two Values = 7 2

+: ADDITION
-: SUBTRACTION
*: MULTIPLICATION
/: DIVISION
%: REMAINDER
^: EXIT\n");

Enter Your Option = +

Addition Result = 9

+: ADDITION
-: SUBTRACTION
*: MULTIPLICATION
/: DIVISION
%: REMAINDER
^: EXIT\n");

Enter Your Option = *

Multiplication Result = 14

ASCET(Autonomous) R23- CP LAB MANUAL Page 25


Program No: 14
Write a C program to find the given year is a leap year or not.

Algorithm:

Step 1: START
Step 2: INPUT Year values as P
Step 3: IF (P MOD 4 = 0) AND ( ( P MOD 400 = 0 ) OR ( P MOD 100 ≠ 0 ) ) THEN
WRITE P as Leap Year
ELSE
WRITE P as Not a Leap Year
ENDIF
Step 4: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int P;
clrscr();
printf("\n Enter Year Value =");
scanf("%d",&P);
if((P%4==0)&&((P%400==0)||(P%100!=0)))
printf("\n %d Is A Leap Year",P);
else
printf("\n %d Is Not A Leap Year",P);
}

Result

Enter Year Value = 1992

1992 IS A Leap Year

ASCET(Autonomous) R23- CP LAB MANUAL Page 26


Program No: 15
Write a program to find the factorial of given number using any loop.

Algorithm:

Step 1: START
Step 2: INPUT a value as N
Step 3: Fact ← 1
i←1
Step 4: REPEAT WHILE i ≤ N
Fact ← Fact * i
i←i+1
ENDREPEAT
Step 5: WRITE Fact value
Step 6: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int N,i,Fact;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Fact=1; i=1;
while(i<=N)
{
Fact=Fact*i;
i=i+1;
}
printf("\n Factorial Value = %d",Fact);
}

Result

Enter A Number = 5

Factorial Value = 120

ASCET(Autonomous) R23- CP LAB MANUAL Page 27


Program No: 16
Write a program to find the given number is a prime or not.

Algorithm:

Step 1: START
Step 2: INPUT N value
Step 3: Flag ← 0
Step 4: REPEAT FOR i ← 2 TO N-1 DO STEPS BY 1
IF N MOD i = 0 THEN
Flag ← 1
BREAK
ENDIF
ENDREPEAT
Step 5: IF Flag = 0 THEN
WRITE N as a Prime Number
ELSE
WRITE N as not a Prime Number
ENDIF
Step 6: STOP

Program:

void main()
{
int N,i,Flag;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Flag=0;
for(i=2;i<N;i++)
{
if(N%i==0)
{
Flag=1;
break;
}
}
if(Flag==0)
printf("\n %d Is A Prime Number",N);
else
printf("\n %d Is Not A Prime Number",N);
}

Result
Enter A Number = 17
17 Is A Prime Number

ASCET(Autonomous) R23- CP LAB MANUAL Page 28


Program No: 17
Write a program to check whether a given number is palindrome or not.

Algorithm:

Step 1: START
Step 2: INPUT a value as N
Step 3: Temp ← N
Rev ← 0
Step 4: REPEAT WHILE N > 0
K ← N MOD 10
Rev ← Rev * 10 + K
N ← N / 10
ENDREPEAT
Step 5: IF Temp = Rev THEN
WRITE Temp as a Palindrome
ELSE
WRITE Temp as not a Palindrome
ENDREPEAT
Step 6: STOP

Program:

void main()
{
int N,K,Temp,Rev;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Temp=N;
Rev=0;
while(N>0)
{
K=N%10;
Rev=Rev*10+K;
N=N/10;
}
if(Temp==Rev)
printf("\n %d Is A Palindrome",Temp);
else
printf("\n %d Is Not A Palindrome",Temp);
}

Result

Enter A Number = 121


121 Is A Palindrome

ASCET(Autonomous) R23- CP LAB MANUAL Page 29


Program No: 18
Write a program to construct a pyramid of numbers.

Algorithm:

Step 1: START
Step 2: INPUT number of lines as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
REPEAT FOR j ← 1 TO 20 DO STEPS BY 1
WRITE ‘ ‘
ENDREPEAT
REPEAT FOR k ← 1 TO i DO STEPS BY 1
WRITE i
ENDREPEAT
ENDREPEAT
Step 4: STOP

Program:

void main()
{
int N,i,j,k;
clrscr();
printf("\n Enter How Many Lines =");
scanf("%d",&N);
printf("\n Pyramid Format = \n\n");
for(i=1;i<=N;i++)
{
printf("\n");
for(j=1;j<=20-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf(" %d",i);
}
}

Result

Enter How Many Lines = 5


Pyramid Format =
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

ASCET(Autonomous) R23- CP LAB MANUAL Page 30


Program No: 19
Write a program to find Min and Max of a 1-D integer array.

Algorithm:

Step 1: START
Step 2: INPUT Number of values as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: Max ← K[1]
Min ← K[1]
Step 5: REPEAT FOR i ← 2 TO N DO STEPS BY 1
IF K[ i ] > Max THEN
Max ← K[ i ]
ENDIF
IF K[ i ] < Min THEN
Min ← K[ i ]
ENDIF
ENDREPEAT
Step 6: WRITE Max and Min values
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 31


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K[50],Max,Min,i,N;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
Max = K[1];
Min = K[1];
for( i = 2 ; i < = N ; i++ )
{
if ( K[i] > Max )
Max = K[i];
if ( K[i] < Min )
Min = K[i];
}
printf("\n Maximum Value = %d",Max);
printf("\n Minimum Value = %d",Min);
}

Result

Enter How Many Elements = 5

Enter 5 Values = 10 25 79 8 34

Maximum Value = 79

Minimum Value = 8

ASCET(Autonomous) R23- CP LAB MANUAL Page 32


Program No: 20
Write a program to perform linear search on 1-D array.

Algorithm:

Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: INPUT Search element as Key
Step 5: Flag ← -1
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
IF K[ i ] = Key THEN
Flag ← i
BREAK
ENDIF
ENDREPEAT
Step 7: IF Flag = -1)
WRITE ‘Search Key Found’
ELSE
WRITE ‘Search Key Not Found’
ENDFIF
Step 8: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 33


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K[50],i,N,Key,Flag;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n Enter An Element To Search = ");
scanf("%d",&Key);
Flag = -1 ;
for ( i = 1 ; i < = N ; i++ )
{
if ( K[i] = = Key )
{
Flag = i;
break;
}
}
if ( Flag = = -1 )
printf("\n Search Key Not Found");
else
printf("\n Search Key Found At Index Position = %d",Flag);
}

Result

Enter How Many Elements = 5

Enter 5 Values = 10 25 79 18 34

Enter An Element To Search = 18

Search Key Found At Index Position = 4

ASCET(Autonomous) R23- CP LAB MANUAL Page 34


Program No: 21
Write a program to print reverse of a 1-D integer array.

Algorithm:

Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 1, j ← N TO i < j DO STEPS BY 1 , -1
Temp ← K[ i ]
K[ i ] ← K[ j ]
K[ j ] ← Temp
ENDREPEAT
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 35


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K[50],Temp,N,i,j;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for ( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n Actual Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
for ( i = 1 , j = N ; i < j ; i++ , j-- )
{
Temp = K[i];
K[i] = K[j];
K[j] = Temp;
}
printf("\n Array Elements In Reverse Order = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
}

Result

Enter How Many Elements = 5

Enter 5 Values = 10 25 79 18 34

Actual Array Elements = 10 25 79 18 34

Array Elements In Reverse Order = 34 18 79 25 10

ASCET(Autonomous) R23- CP LAB MANUAL Page 36


Program No: 22
Write a program to find 2’s complement of the given binary number.

Algorithm:

Step 1: START
Step 2: INPUT Number of Digits as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT X[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← N-1 TO 0 DO STEPS BY -1
k ← X[ i ]
j ← i
IF k = 0 THEN
Y[ j ] ← k
ELSE
Y[ j ] ← k
REPEAT FOR p ← j - 1 TO 0 DO STEPS BY -1
IF X[ p ] = 0 THEN
Y[ p ] ← 1
ELSE
Y[ p ] ← 0
ENDIF
ENDREPEAT
GOTO T
ENDIF
ENDREPEAT
Step 5: T: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE Y[ i ] value
ENDREPEAT
Step 6: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 37


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int N,i,j,k,p,X[10],Y[10];
clrscr();
printf("\n Enter How Many Digits = ");
scanf("%d",&N);
printf("\n Enter %d Binary numbers (0 or 1) = ",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d",&X[i]);
for ( i = N-1 ; i > = 0 ; i-- )
{
k = X[i];
j=i;
if ( k = = 0 )
Y[j] = k;
else
{
Y[j] = k;
for ( p = j-1 ; p > = 0 ; p-- )
{
if ( X[p] = = 0 )
Y[p] = 1;
else
Y[p] = 0;
}
goto T;
}
}
T: printf("\n 2'S COMPLEMENT OF GIVEN NUMBER IS = ");
for ( i = 0 ; i < N ; i++ )
printf(" %d",Y[i]);
}

Result

Enter How Many Digits = 6

Enter 6 Binary numbers (0 or 1) = 1 0 0 1 0 0

2'S COMPLEMENT OF GIVEN NUMBER IS = 0 1 1 1 0 0

ASCET(Autonomous) R23- CP LAB MANUAL Page 38


Program No: 23
Write a program to eliminate duplicate elements in an array.

Algorithm:

Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 0 TO N-2 DO STEPS BY 1
REPEAT FOR j ← i + 1 TO N-1 DO STEPS BY 1
IF K[ i ] = K[ j ] THEN
REPEAT FOR p ← j TO N-2 DO STEPS BY 1
K[ p ] ← K[ p + 1 ]
ENDREPEAT
N←N+1
ENDIF
ENDREPEAT
ENDREPEAT
Step 6: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 39


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K[50],i,j,p,N;
clrscr();
printf("\n Enter How Many Values = ");
scanf("%d",&N);
printf("\n Enter %d Elements = ",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d",&K[i]);
printf("\n Actual Array Elements = \n");
for ( i = 0 ; i < N ; i++ )
printf(" %d",K[i]);
for ( i = 0 ; i < = N-2 ; i++ )
{
for ( j = i+1 ; j < = N-1 ; j++ )
{
if ( K[i] = = K[j] )
{
for ( p = j ; p < = N-2 ; p++ )
K[p] = K[p+1];
N = N-1;
}
}
}
printf("\n After Removing Duplicate Elements Array Elements = \n");
for ( i = 0 ; i < N ; i++ )
printf(" %d",K[i]);
}

Result

Enter How Many Values = 6

Enter 6 Elements = 10 12 10 25 12 10

Actual Array Elements = 10 12 10 25 12 10

After Removing Duplicate Elements Array Elements = 10 12 25

ASCET(Autonomous) R23- CP LAB MANUAL Page 40


Program No: 24
Write a program to perform Addition of two matrices.

Algorithm:

Step 1: START
Step 2: INPUT R1, C1, R2 and C2 values
Step 3: IF R1 = R2 AND C1 = C2 THEN
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
INPUT X[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R2 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
INPUT Y[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
Z[ i ][ j ] ← X[ i ][ j ] + Y[ i ][ j ]
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
WRITE Z[ i ][ j ] value
ENDREPEAT
ENDREPEAT
ELSE
WRITE ‘ Matrix Addition Not Possible ‘
ENDIF
Step 4: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 41


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int X[10][10],Y[10][10],Z[10][10],i,j,R1,C1,R2,C2;
clrscr();
printf("\n Enter Number of Rows of Matrix 1 =");
scanf("%d",&R1);
printf("\n Enter Number of Columns of Matrix 1 =");
scanf("%d",&C1);
printf("\n Enter Number of Rows of Matrix 2 =");
scanf("%d",&R2);
printf("\n Enter Number of Columns of Matrix 2 =");
scanf("%d",&C2);
if ( R1 = = R2 && C1 = = C2 )
{
printf("\n Enter Matrix 1 Elements =");
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
scanf("%d",&X[i][j]);
}
printf("\n Enter Matrix 2 Elements =");
for ( i = 0 ; i < R2 ; i++ )
{
for ( j = 0 ; j <C2 ; j++ )
scanf("%d",&Y[i][j]);
}
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
Z[i][j] = X[i][j] + Y[i][j];
}
printf("\n Addition Matrix Elements =\n");
for ( i = 0 ; i < R1 ; i++ )
{
printf("\n");
for ( j = 0 ; j <C1 ; j++ )
printf(" %d",Z[i][j]);
}
}
else
printf("\n Matrix Addition Not Possible");
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 42


Result

Enter Number of Rows of Matrix 1 = 3

Enter Number of Columns of Matrix 1 = 3

Enter Number of Rows of Matrix 2 = 3

Enter Number of Columns of Matrix 2 = 3

Enter Matrix 1 Elements = 1 2 3


4 5 6
7 8 9

Enter Matrix 2 Elements = 1 1 1


2 1 6
3 2 1

Addition Matrix Elements = 2 3 4


6 6 12
10 10 10

ASCET(Autonomous) R23- CP LAB MANUAL Page 43


Program No: 25
Write a program to perform Multiplication of two matrices.

Algorithm:

Step 1: START
Step 2: INPUT R1, C1, R2 and C2 values
Step 3: IF C1 = R2 THEN
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
INPUT X[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R2 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
INPUT Y[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
Z[ i ][ j ] ← 0
REPEAT FOR k ← 0 TO C1 - 1 DO STEPS BY 1
Z[ i ][ j ] ← Z[ i ][ j ] + X[ i ][ k ] * Y[ k ][ j ]
ENDREPEAT
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
WRITE Z[ i ][ j ] value
ENDREPEAT
ENDREPEAT
ELSE
WRITE ‘ Matrix Multiplication Not Possible ‘
ENDIF
Step 4: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 44


Program:

void main()
{
int X[10][10],Y[10][10],Z[10][10],i,j,k,R1,C1,R2,C2;
printf("\n Enter Number of Rows of Matrix 1 =");
scanf("%d",&R1);
printf("\n Enter Number of Columns of Matrix 1 =");
scanf("%d",&C1);
printf("\n Enter Number of Rows of Matrix 2 =");
scanf("%d",&R2);
printf("\n Enter Number of Columns of Matrix 2 =");
scanf("%d",&C2);
if ( C1 = = R2 )
{
printf("\n Enter Matrix 1 Elements =");
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
scanf("%d",&X[i][j]);
}
printf("\n Enter Matrix 2 Elements =");
for ( i = 0 ; i < R2 ; i++ )
{
for ( j = 0 ; j <C2 ; j++ )
scanf("%d",&Y[i][j]);
}
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C2 ; j++ )
{
Z[i][j] = 0;
for ( k = 0 ; k < C1 ; k++ )
Z[i][j] = Z[i][j] + X[i][k] * Y[k][j];
}
}
printf("\n Multiplication Matrix Elements =\n");
for ( i = 0 ; i < R1 ; i++ )
{
printf("\n");
for ( j = 0 ; j < C2 ; j++ )
printf(" %d",Z[i][j]);
}
}
else
printf("\n Matrix Multiplication Not Possible");
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 45


Result

Enter Number of Rows of Matrix 1 = 3

Enter Number of Columns of Matrix 1 = 3

Enter Number of Rows of Matrix 2 = 3

Enter Number of Columns of Matrix 2 = 3

Enter Matrix 1 Elements = 1 2 3


4 5 6
7 8 9

Enter Matrix 2 Elements = 1 0 0


0 1 0
0 0 1

Multiplication Matrix Elements = 1 2 3


4 5 6
7 8 9

ASCET(Autonomous) R23- CP LAB MANUAL Page 46


Program No: 26
Write a program to sort array elements using bubble sort.

Algorithm:

Step 1: START
Step 2: INPUT Number of elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 1 TO N - 1 DO STEPS BY 1
j←1
REPEAT WHILE j ≤ N - i
IF K[ j ] > K[ j + 1 ] THEN
Temp ← K[ j ]
K[j] ← K[ j + 1 ]
K[ j + 1 ] ← Temp
ENDIF
j← j+1
ENDREPEAT
ENDREPEAT
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 47


Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K[25],i,j,Temp,N;
clrscr();
printf("\n Ente How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for ( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n\n Before Sorting Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
for ( i = 1 ; i < N ; i++ )
{
j = 1;
while ( j < = N-i )
{
if ( K[j] > K[j+1] )
{
Temp = K[j];
K[j] = K[j+1];
K[j+1] = Temp;
}
j = j + 1;
}
}
printf("\n\n After Sorting Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
}

Result

Enter How Many Elements = 5

Enter 5 Values = 10 25 12 72 38

Before Sorting Array Elements = 10 25 12 72 38

After Sorting Array Elements = 10 12 25 38 72

ASCET(Autonomous) R23- CP LAB MANUAL Page 48


Program No: 27
Write a program to concatenate two strings without built-in functions.

Algorithm:

Step 1: START
Step 2: INPUT First String as S1 and Second String as S2
Step 3: REPEAT FOR i ← 0 TO S1[ i ] ≠ ‘\0’ DO STEPS BY 1
ENDREPEAT
Step 4: REPEAT FOR j ← 0 TO S2[ j ] ≠ ‘\0’ DO STEPS BY 1
S1 [ i + j ] ← S2 [ j ]
ENDREPEAT
Step 5: S1[ i + j ] ← ‘\0’
Step 6: WRITE S1 as result string
Step 7: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
char S1[50],S2[50];
int i,j;
clrscr();
printf("\n Enter First String =");
gets(S1);
printf("\n Enter Second String =");
gets(S2);
for ( i = 0 ; S1[i] ! = '\0' ; i++ ) ;
for ( j = 0 ; S2[j] ! = '\0' ; j++ )
S1[i+j] = S2[j];
S1[i+j] = '\0' ;
printf("\n Result String = %s",S1);
}

Result

Enter First String = C and C++ Are

Enter Second String = Programming Languages

Result String = C and C++ Are Programming Languages

ASCET(Autonomous) R23- CP LAB MANUAL Page 49


Program No: 28
Write a program to print Reverse a string using built-in functions.

Algorithm:

Step 1: START
Step 2: INPUT A String as S1
Step 3: WRITE S1
Step 4: CALL strrev(S1)
Step 5: WRITE S1
Step 6: STOP

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char S1[50];
clrscr();
printf("\n Enter A String =");
gets(S1);
printf("\n Original String = %s",S1);
strrev(S1);
printf("\n Reverse String = %s",S1);
}

Result

Enter A String = PBR VITS

Original String = PBR VITS

Reverse String = STIV RBP

ASCET(Autonomous) R23- CP LAB MANUAL Page 50


Program No: 29
Write a program to print Reverse a string without using built-in functions.

Algorithm:

Step 1: START
Step 2: INPUT A String as S1
Step 3: WRITE S1
Step 4: REPEAT FOR N ← 0 TO S1[ N ] ≠ 0 DO STEPS BY 1
ENDREPEAT
Step 5: REPEAT FOR i ← 0 , j ← N - 1 TO i < j DO STEPS BY 1 , -1
Temp ← S1 [ i ]
S1 [ i ] ← S1 [ j ]
S1 [ j ] ← Temp
ENDREPEAT
Step 6: WRITE S1
Step 7: STOP

Program:

void main()
{
char S1[50],Temp;
int i,j,N;
clrscr();
printf("\n Enter A String =");
gets(S1);
printf("\n Original String = %s",S1);
for ( N = 0 ; S1[N] ! = '\0' ; N++ ) ;
for ( i = 0 , j = N-1 ; i < j ; i++ , j-- )
{
Temp = S1[i];
S1[i] = S1[j];
S1[j] = Temp;
}
printf("\n Reverse String = %s",S1);
}

Result

Enter A String = PBR VITS

Original String = PBR VITS

Reverse String = STIV RBP

ASCET(Autonomous) R23- CP LAB MANUAL Page 51


Program No: 30
Write a C program to find the sum of a 1-D array using malloc() function.

Algorithm:

Step 1: START
Step 2: INPUT Number of values as N
Step 3: Allocate memory for a pointer variable K with malloc ( ) Function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT K + i
ENDREPEAT
Step 5: Sum ← 0
Step 6: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
Sum ← Sum + * ( K + i )
ENDREPEAT
Step 7: WRITE Sum value
Step 8: STOP

Program:

#include<stdio.h>
#include<alloc.h>

void main()
{
int *K , i , N , Sum ;
clrscr();
printf("\n Enter How Many Values = ");
scanf("%d", &N);
K = ( int * ) malloc ( N * sizeof ( int ) ) ;
printf("\n Enter %d Elements =",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d" , K + i ) ;
Sum = 0 ;
for ( i = 0 ; i < N ; i++ )
Sum = Sum + * ( K + i ) ;
printf("\n Sum Result Value = %d" , Sum ) ;
}

Result

Enter How Many Values = 5

Enter 5 Elements = 10 35 26 42 89

Sum Result Value = 202

ASCET(Autonomous) R23- CP LAB MANUAL Page 52


Program No: 31
Write a C program to find the total, average of n students using structures.

Algorithm:

Define a structure student with the members RNO , S1 , S2 , S3 , TOTAL as integers, NAME
as a string and AVG as float.

Step 1: START
Step 2: INPUT number of students as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT ST[ i ] . RNO , ST[ i ] . NAME
INPUT ST[ i ] . S1 , ST[ i ] . S2 , ST[ i ] . S3
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
ST[i].TOTAL ← ST[i].S1 + ST[i].S2 + ST[i].S3
ST[i].AVG ← (float) ( ST[i].TOTAL / 3 )
ENDREPEAT
Step 5: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE ST[i].RNO , ST[i].NAME , ST[i].TOTAL , ST[i].AVG
ENDREPEAT
Step 6: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 53


Program:

#include<stdio.h>
#include<conio.h>

struct student
{
int RNO , S1 , S2 , S3 , TOTAL ;
char NAME[50];
float AVG;
}ST[50];

void main()
{
int N , i ;
clrscr();
printf("\n Enter How Many Students = " ) ;
scanf("%d" , &N);
for ( i = 1 ; i <= N ; i++ )
{
printf("\n Enter Student %d Roll Number = " , i ) ;
scanf("%d" , &ST[i].RNO ) ;
fflush(stdin);
printf("\n Enter Name of the Student =");
gets(ST[i].NAME);
printf("\n Enter Marks in Three Subjects = " ) ;
scanf("%d%d%d", &ST[i].S1 , &ST[i].S2 , &ST[i].S3 ) ;
}
for ( i = 1 ; i < = N ; i++ )
{
ST[i].TOTAL = ST[i].S1 + ST[i].S2 + ST[i].S3 ;
ST[i].AVG = (float) ( ST[i].TOTAL / 3 ) ;
}
printf("\n TOTAL AND AVERAGE MARKS OF STUDENTS ARE =\n\n");
for ( i = 1 ; i < = N ; i++ )
{
printf("\n");
printf("%d\t%s\t%d\t%.2f",ST[i].RNO,ST[i].NAME,ST[i].TOTAL,ST[i].AVG);
}
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 54


Result

Enter How Many Students = 2

Enter Student 1 Roll Number = 111


Enter Name of the Student = Mahesh
Enter Marks in Three Subjects= 78 90 65

Enter Student 2 Roll Number = 222


Enter Name of the Student = Ravi
Enter Marks in Three Subjects= 76 95 85

TOTAL AND AVERAGE MARKS OF STUDENTS ARE =

111 Mahesh 233 77.00


222 Ravi 256 85.00

ASCET(Autonomous) R23- CP LAB MANUAL Page 55


Program No: 32
Write a program to enter n students data using calloc() and display failed students list.

Algorithm:

Define a structure Student with the members RNO , S1 , S2 , S3 as integers, NAME as a


string.

Step 1: START
Step 2: INPUT number of students as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT ST[ i ] . RNO , ST[ i ] . NAME
INPUT ST[ i ] . S1 , ST[ i ] . S2 , ST[ i ] . S3
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
IF ST[ i ].S1 ≤ 40 OR ST[ i ].S2 ≤ 40 OR ST[ i ].S3 ≤ 40 THEN
WRITE ST[ i ].RNO , ST[ i ].NAME
ENDIF
ENDREPEAT
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct
{
int RNO,S1,S2,S3;
char NAME[50];
}Student;

void main()
{
Student *ST;
int N,i;
clrscr();
printf("\n Enter How Many Students =");
scanf("%d",&N);
ST=(Student *)calloc(N,sizeof(Student));
for(i=0;i<N;i++)
{
printf("\n Enter Student %d Roll Number =",i);
scanf("%d",&ST[i].RNO);
fflush(stdin);

ASCET(Autonomous) R23- CP LAB MANUAL Page 56


printf("\n Enter Name of the Student =");
gets(ST[i].NAME);
printf("\n Enter Marks in Three Subjects=");
scanf("%d%d%d",&ST[i].S1,&ST[i].S2,&ST[i].S3);
}
printf("\n\n Failed Student Details Are =\n\n");
for(i=0;i<N;i++)
{
if(ST[i].S1<=40 || ST[i].S2<=40 || ST[i].S3<=40)
printf("%d\t%s\n",ST[i].RNO,ST[i].NAME);
}
}

Result

Enter How Many Students = 2

Enter Student 0 Roll Number = 111


Enter Name of the Student = Mahesh
Enter Marks in Three Subjects= 28 90 65

Enter Student 1 Roll Number = 222


Enter Name of the Student = Ravi
Enter Marks in Three Subjects= 76 95 85

Failed Student Details Are =

111 Mahesh

ASCET(Autonomous) R23- CP LAB MANUAL Page 57


Program No: 33
Write a program to read student name and marks from the command line and display
the student details along with the total.

Algorithm:

Step 1: START
Step 2: CALL strcpy( STName , argv[ 1 ] )
S1 ← atoi( argv[ 2 ] )
S2 ← atoi( argv[ 3 ] )
Total ← S1 + S2
Step 3: WRITE argc , STNAME , S1 , S2 , Total values
Step 4: STOP

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int main(int argc,char *argv[])


{
char STName[50];
int S1,S2,Total;
clrscr();
strcpy(STName,argv[1]);
S1=atoi(argv[2]);
S2=atoi(argv[3]);
Total=S1+S2;
printf("\n Number of Arguments = %d",argc);
printf("\n Student Name = %s",STName);
printf("\n Subject 1 Marks = %d",S1);
printf("\n Subject 2 Marks = %d",S2);
printf("\n Total Marks = %d",Total);
return 1;
}

Result

C:\TC\BIN> Prg33 Mahesh 70 80

Number of Arguments = 4
Studetn Name = Mahesh
Subject 1 Marks = 70
Subject 2 Marks = 80
Total Marks = 150

ASCET(Autonomous) R23- CP LAB MANUAL Page 58


Program No: 34
Write a C program to implement realloc( ) function.

Algorithm:

Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: Allocate memory for a pointer Variable P with malloc ( ) function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 5: Re Allocate memory for a pointer Variable P with realloc ( ) function
Step 6: REPEAT FOR i ← 0 TO N+1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 7: REPEAT FOR i ← 0 TO N+1 DO STEPS BY 1
WRITE * ( P + i ) values
ENDREPEAT
Step 8: STOP

Program:

#include<alloc.h>
void main()
{
int *P,N,i;
clrscr();
printf("\n Enter How Many Elements =");
scanf("%d",&N);
P=(int*)malloc(N*sizeof(int));
printf("\n Enter %d Values =",N);
for(i=0;i<N;i++) scanf("%d",P+i);
P=(int*)realloc(P,(N+2)*sizeof(int));
printf("\n Re Enter %d Values =",N+2);
for(i=0;i<N+2;i++)
scanf("%d",P+i);
printf("\n\n Array Elements =\n\n");
for(i=0;i<N+2;i++)
printf("%d ",*(P+i));

}
Result

Enter How Many Elements = 5


Enter 5 Values = 10 23 56 42 78
Re Enter 7 Values = 11 22 33 44 55 66 77
Array Elements = 11 22 33 44 55 66 77

ASCET(Autonomous) R23- CP LAB MANUAL Page 59


Program No: 35
Write a C program to create and display a singly linked list using self-referential structure.

Algorithm creation (): This procedure creates a single liked list with the
specified number of elements.

Step 1: Repeat WHILE TRUE


READ an element as x
IF x = -999 THEN
RETURN
ELSE
Allocate memory for a NEW node
DATA(NEW) ← x
LINK(NEW) ← NULL
IF START = NULL THEN
START ← END ← NEW
ELSE
LINK(END) ← NEW
END ← NEW
ENDIF
ENDIF
EndRepeat

Algorithm display(): This procedure is used to display the elements of the


single linked list from the first node to the last node.

Step 1: TEMP ← START


Step 2: IF START ≠ NULL THEN
Repeat WHILE TEMP ≠ NULL
WRITE DATA(TEMP)
TEMP ← LINK(TEMP)
EndRepeat
ELSE
WRITE ‘LIST EMPTY’
ENDIF
Step 3: RETURN

ASCET(Autonomous) R23- CP LAB MANUAL Page 60


Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct list


{
int DATA;
struct list *LINK;
}NODE;

NODE *START=NULL,*END=NULL;

void create();
void display();

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n 1:CREATION\n 2:DISPLAY\n 3:EXIT\n");
printf("\n Enter Your Choice =");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:exit();
}
}
}
void create()
{
NODE *NEW;
int K;
while(1)
{
printf("\n Enter An Element (STOP:-999) =");
scanf("%d",&K);
if(K==-999)
return;

ASCET(Autonomous) R23- CP LAB MANUAL Page 61


else
{
NEW=(NODE*)malloc(sizeof(NODE));
NEW->DATA=K;
NEW->LINK=NULL;
if(START==NULL)
START=END=NEW;
else
{
END->LINK=NEW;
END=NEW;
}
}
}
}
void display()
{
NODE *TEMP;
if(START==NULL)
printf("\n LIST EMPTY");
else
{
printf("\n SINGLE LINKED LIST ELEMENTS =");
TEMP=START;
while(TEMP!=NULL)
{
printf(" %d",TEMP->DATA);
TEMP=TEMP->LINK;
}
}
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 62


Result:

1:CREATION
2:DISPLAY
3:EXIT

Enter Your Choice = 1

Enter An Element(Stop:-999) = 11
Enter An Element(Stop:-999) = 22
Enter An Element(Stop:-999) = 33
Enter An Element(Stop:-999) = -999

1:CREATION
2:DISPLAY
3:EXIT
Enter Your Choice = 2

List of Elements Are = 11 22 33

ASCET(Autonomous) R23- CP LAB MANUAL Page 63


Program No: 36
Write a program to demonstrate the differences between structure and union.

Algorithm:

Step 1: START
Step 2: Define a structure Demo1 with the members a as character, b as integer, c as float and
d as double data members.
Define a union Demo2 with the members a as character, b as integer, c as float and d
as
double data members.
Step 3: WRITE memory size of structure and union variables.
Step 4: STOP

Program:

#include<stdio.h>
#include<conio.h>

struct Demo1
{
char a;
int b;
float c;
double d;
}ST;

union Demo2
{
char a;
int b;
float c;
double d;
}UP;

void main()
{
clrscr();
printf("\n Memory Size of Structure ST = %d Bytes",sizeof(ST));
printf("\n Memory Size of Union UP = %d Bytes",sizeof(UP));
}

Result

Memory Size of Structure ST = 15 Bytes

Memory Size of Union UP = 8 Bytes

ASCET(Autonomous) R23- CP LAB MANUAL Page 64


Program No: 37
Write a C program to shift/rotate using bit fields.

Algorithm:

Step 1: START
Step 2: K ← 25
WRITE K << 2
Step 3: K ← 78
WRITE K >> 1
Step 4: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int K;
clrscr();
K=25;
printf("\n Left Shift Result = %d",K<<2);
K=78;
printf("\n Right Shift Result = %d",K>>1);
}

Result

Left Shift Result = 100

Right Shift Result = 39

ASCET(Autonomous) R23- CP LAB MANUAL Page 65


Program No: 38
Write a C program to copy one structure variable to another structure of the same type.

Algorithm:

Step 1: START
Step 2: Define as structure BOOK with BID as integer, BNAME as string and BPRICE as
float.
Step 3: Create two variables B1 and B2 for the Book
Step 4: INPUT B1 values
Step 5: B2 ← B1
Step 6: WRITE B1 , B2 values
Step 7: STOP

Program:

#include<stdio.h>
#include<conio.h>

struct BOOK
{
int BID;
char BNAME[50];
float BPRICE;
};

void main()
{
struct BOOK B1={121,"Let Us C",275.00},B2;
clrscr();
printf("\n Original Book Data = %d\t%s\t%.2f RS",B1.BID,B1.BNAME,B1.BPRICE);
B2=B1;
printf("\n Copied Book Data = %d\t%s\t%.2f RS",B2.BID,B2.BNAME,B2.BPRICE);
}

Result

Original Book Data = 121 Let Us C 275.00 RS

Copied Book Data = 121 Let Us C 275.00 RS

ASCET(Autonomous) R23- CP LAB MANUAL Page 66


Program No: 39
Write a C program to swap two numbers using call by reference.

Algorithm:
Step 1: START
Step 2: INPUT two values as X and Y
Step 3: WRITE X , Y values
Step 4: CALL swap(&X , &Y)
Step 5: WRITE X , Y values
Step 6: STOP

swap (*P , *Q): Procedure used to swap the given two values linked with P and Q.
Step 1: Temp ← *P
*P ← *Q
*Q ← Temp
Step 2: RETURN

Program:

#include<stdio.h>

void swap(int*,int*);

void main()
{
int X,Y;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
printf("\n\n Before Swapping Values Are = %d\t%d",X,Y);
swap(&X,&Y);
printf("\n\n After Swapping Values Are = %d\t%d",X,Y);
}
void swap(int *P,int *Q)
{
int Temp;
Temp=*P;
*P=*Q;
*Q=Temp;
}

Result

Enter Two Values = 10 20

Before Swapping Values Are = 10 20


After Swapping Values Are = 20 10

ASCET(Autonomous) R23- CP LAB MANUAL Page 67


Program No: 40
Demonstrate Dangling pointer problem using a C program.

Dangling Pointer: A dangling pointer in C is a pointer that points to a memory location


that has been de-allocated or is no longer valid.

Algorithm:

Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: Allocate memory for a pointer Variable P with malloc ( ) or calloc ( ) function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 5: WRITE *P
Step 6: Release memory of P

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

void main()
{
int *P,N,i;
clrscr();
printf("\n Enter How Many Values =");
scanf("%d",&N);
P=(int*)calloc(N,sizeof(int));
printf("\n Enter %d Elements =",N);
for(i=0;i<N;i++)
scanf("%d",P+i);
printf("\n Starting Value =%d",*P);
free(P);
P=NULL;
printf("\n Dangling Pointer Result = %d",*P);
}

Result

Enter How Many Values = 5

Enter 5 Elements = 10 23 56 42 78

Starting Value = 10
Dangling Pointer Result = 0

ASCET(Autonomous) R23- CP LAB MANUAL Page 68


Program No: 41
Write a C program to copy one string into another using pointer.

Algorithm:

Step 1: START
Step 2: INPUT a string as S1
Step 3: WRITE S1
Step 4: Define a pointer P and initialize with S1
Step 5: REPEAT FOR i ← 0 TO * ( P + i ) ≠ '\0' DO STEPS BY 1
S2 [ i ] ← * ( P + i )
ENDREPEAT
Step 6: S2 [ i ] ← '\0'
Step 7: WRITE S2
Step 8: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
char S1[50],S2[50],*P;
int i;
clrscr();
printf("\n Enter A String = ");
gets(S1);
printf("\n Actual String = %s",S1);
P=S1;
for(i=0;*(P+i)!='\0';i++)
S2[i]=*(P+i);
S2[i]='\0';
printf("\n Copied String = %s",S2);
}

Result

Enter A String = C LAB DATA

Actual String = C LAB DATA

Copied String = C LAB DATA

ASCET(Autonomous) R23- CP LAB MANUAL Page 69


Program No: 42
Write a C program to find no of lowercase, uppercase, digits and other characters
using pointers.

Algorithm:

Step 1: START
Step 2: INPUT a string as S1
Step 3: NLC ← 0
NUC ← 0
ND ← 0
NSP ← 0
Step 4: Define a pointer P and initialize with S1 as P ← S1
Step 5: REPEAT FOR i ← 0 TO * ( P + i ) ≠ '\0' DO STEPS BY 1
IF isalpha( * ( P + i ) ) THEN
IF islower( * ( P + i ) ) THEN
NLC ← NLC + 1
ELSE
NUC ← NUC + 1
ENDIF
ELSEIF isdigit( * ( P + i ) ) THEN
ND ← ND + 1
ELSE
NSP ← NSP + 1
ENDIF
ENDREPEAT
Step 6: WRITE NLC , NUC , ND and NSP values
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 70


Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{
char S1[100],*P;
int i,NLC,NUC,ND,NSP;
clrscr();
printf("\n Enter A String = ");
gets(S1);
NLC=0; NUC=0; ND=0; NSP=0;
P=S1;
for(i=0;*(P+i)!='\0';i++)
{
if(isalpha(*(P+i)))
{
if(islower(*(P+i)))
NLC=NLC+1;
else
NUC=NUC+1;
}
else if(isdigit(*(P+i)))
ND=ND+1;
else
NSP=NSP+1;
}
printf("\n Number of Lower Case Characters = %d",NLC);
printf("\n Number of Upper Case Characters = %d",NUC);
printf("\n Number of Digits = %d",ND);
printf("\n Number of Special Characters = %d",NSP);
}

Result

Enter A String = C Language In 1972.

Number of Lower Case Characters = 8


Number of Upper Case Characters = 3
Number of Digits = 4
Number of Special Characters = 4

ASCET(Autonomous) R23- CP LAB MANUAL Page 71


Program No: 43
Write a program to find compound interest value.

Compound Interest formula:

Formula to calculate compound interest annually is given by:


Amount = P ( 1 + R / 100 ) T
Compound Interest = Amount – P
Where,
P is principle amount
R is the rate of Interest and
T is the time span
Algorithm:

Step 1: START
Step 2: INPUT P , R , T values
Step 3: A ← P * ( pow ( ( 1 + R / 100 ) , T ) )
CI ← A – P
Step 4: WRITE CI value
Step 5: STOP

Program:

#include<math.h>
void main()
{
float P,R,T,A,CI;
clrscr();
printf("\n Enter Principle Amount = ");
scanf("%f",&P);
printf("\n Enter Rate of Interest = ");
scanf("%f",&R);
printf("\n Enter Number of Years = ");
scanf("%f",&T);
A = P * ( pow ( ( 1 + R / 100 ) , T ) ) ;
CI = A – P ;
printf("\n Compound Interest Amount = %.2f Rs" , CI );
}

Result

Enter Principle Amount = 15000


Enter Rate of Interest = 2.25
Enter Number of Years = 3

Compound Interest Amount = 1035.45 Rs

ASCET(Autonomous) R23- CP LAB MANUAL Page 72


Program No: 44
Write a program to find Area of a triangle using heron’s formulae.

Suppose, a triangle ABC, whose sides are a, b and c, respectively.


Thus, the area of a triangle can be given by:

Area = sqrt (s(s-a)(s-b)(s-c))

Where “s” is semi-perimeter = (a+b+c) / 2

Algorithm:

Step 1: START
Step 2: INPUT Three Sides of the Triangle as A , B , C values
Step 3: SP ← ( A + B + C ) / 2 ;
Area ← sqrt ( ( SP * ( SP – A ) * ( SP – B ) * ( SP – C ) ) )
Step 4: WRITE Area value
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
float A,B,C,SP,Area;
clrscr();
printf("\n Enter Three Sides of the Triangle = ");
scanf("%f%f%f",&A,&B,&C);
SP = ( A + B + C ) / 2 ;
Area = sqrt ( ( SP * ( SP – A ) * ( SP – B ) * ( SP – C ) ) ) ;
printf("\n Area of the Triangle = %.2f", Area );
}

Result

Enter Three Sides of the Triangle = 8 7 4

Area of the Triangle = 14.00

ASCET(Autonomous) R23- CP LAB MANUAL Page 73


Program No: 45
Write a program to find the roots of the quadratic equation.

Algorithm:

Step 1: START
Step 2: INPUT three values as a , b and c
Step 3: Dis ← ( b * b ) - ( 4 * a * c )
Step 4: IF Dis = 0 THEN
WRITE ‘Roots Are Real and Equal’
R1 ← - b / ( 2 * a )
R2 ← - b / ( 2 * a )
WRITE R1 , R2 values
ELSEIF Dis > 0 THEN
WRITE ‘Roots Are Real and Distinct’
R1 ← ( - b + sqrt ( Dis ) ) / ( 2 * a )
R2 ← ( - b – sqrt ( Dis ) ) / ( 2 * a )
WRITE R1 , R2 values
ELSE
WRITE ‘Roots Are Imaginary Parts’
ENDIF
Step 5: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 74


Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
double a,b,c,R1,R2,Dis;
clrscr();
printf("\n Enter A, B and C Values =");
scanf("%lf%lf%lf",&a,&b,&c);
Dis = ( b * b ) - ( 4 * a * c ) ;
if ( Dis = = 0 )
{
printf("\n Roots Are Real and Equal ");
R1 = - b / ( 2 * a ) ;
R2 = - b / ( 2 * a ) ;
printf("\n ROOT 1 = %.2lf" , R1 ) ;
printf("\n ROOT 2 = %.2lf" , R2 ) ;
}
else if ( Dis > 0 )
{
printf("\n Roots Are Real and Distinct ");
R1 = ( - b + sqrt ( Dis ) ) / ( 2 * a ) ;
R2 = ( - b – sqrt ( Dis ) ) / ( 2 * a ) ;
printf("\n ROOT 1 = %.2lf" , R1 ) ;
printf("\n ROOT 2 = %.2lf" , R2 ) ;
}
else
printf("\n Roots Are Imaginary Parts ");
}

Result

Enter A, B and C Values = 1 2 1

Roots Are Real and Equal

ROOT 1 = -1.00
ROOT 2 = -1.00

ASCET(Autonomous) R23- CP LAB MANUAL Page 75


Program No: 46
Write a program to compute Sine series.

Sin (x) = x – x3 / 3! + x5 / 5! – x7 / 7! + - - - - - - - - - - - -

Algorithm:

Step 1: START
Step 2: INPUT number of terms as N
Step 3: INPUT x value
Step 4: Sum ← 0
Count ← 0
Step 5: REPEAT FOR i ← 1 TO 2 * N – 1 DO STEPS BY 2
Count ← Count + 1
Fact ← 1
REPEAT FOR j ← 1 TO i DO STEPS BY 1
Fact ← Fact * j
ENDREPEAT
Term ← pow ( x , i ) / Fact
IF Count MOD 2 = 0 THEN
Sum ← Sum – Term
ELSE
Sum ← Sum + Term
ENDIF
ENDREPEAT
Step 6: WRITE Sum value
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 76


Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int x,N,i,j,Count;
double Fact,Sum,Term;
clrscr();
printf("\n Enter How Many Terms = ");
scanf("%d",&N);
printf("\n Enter X Value = ");
scanf("%d",&x);
Sum = 0 ;
Count = 0 ;
for ( i = 1 ; i < = 2 * N – 1 ; i + = 2 )
{
Count = Count + 1 ;
Fact = 1 ;
for ( j = 1 ; j < = i ; j++ )
Fact = Fact * j ;
Term = pow ( x , i ) / Fact ;
if ( Count % 2 = = 0 )
Sum = Sum – Term ;
else
Sum = Sum + Term ;
}
printf("\n Sine Series Result = %.2lf" , Sum );
}

Result

Enter How Many Terms = 4

Enter X Value = 2

Sine Series Result = 0.91

ASCET(Autonomous) R23- CP LAB MANUAL Page 77


Program No: 47
Write a program to compute Cosine series.

Cos (x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + - - - - - - - - - - - -

Algorithm:

Step 1: START
Step 2: INPUT number of terms as N
Step 3: INPUT x value
Step 4: Sum ← 1
Count ← 1
Step 5: REPEAT FOR i ← 2 TO 2 * N – 2 DO STEPS BY 2
Count ← Count + 1
Fact ← 1
REPEAT FOR j ← 1 TO i DO STEPS BY 1
Fact ← Fact * j
ENDREPEAT
Term ← pow ( x , i ) / Fact
IF Count MOD 2 = 0 THEN
Sum ← Sum – Term
ELSE
Sum ← Sum + Term
ENDIF
ENDREPEAT
Step 6: WRITE Sum value
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 78


Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int x,N,i,j,Count;
double Fact,Sum,Term;
clrscr();
printf("\n Enter How Many Terms = ");
scanf("%d",&N);
printf("\n Enter X Value = ");
scanf("%d",&x);
Sum = 1 ;
Count = 1 ;
for ( i = 2 ; i < = 2 * N – 2 ; i + = 2 )
{
Count = Count + 1 ;
Fact = 1 ;
for ( j = 1 ; j < = i ; j++ )
Fact = Fact * j ;
Term = pow ( x , i ) / Fact ;
if ( Count % 2 = = 0 )
Sum = Sum – Term ;
else
Sum = Sum + Term ;
}
printf("\n Cosine Series Result = %.2lf" , Sum );
}

Result

Enter How Many Terms = 4

Enter X Value = 2

Cosine Series Result = -0.42

ASCET(Autonomous) R23- CP LAB MANUAL Page 79


Program No: 48
Write a C function to calculate NCR value.

NCR = N! / (R! * (N-R)!)

Algorithm:

Step 1: START
Step 2: INPUT N and R values
Step 3: Res ← Fact ( N ) / ( Fact ( R ) * Fact ( N – R ) )
Step 4: WRITE Res value
Step 5: STOP

Fact ( K ): Function returns factorial value of the argument K.

Step 1: F ← 1
Step 2: REPEAT FOR i ← 1 TO K DO STEPS BY 1
F ← F*i
ENDREPEAT
Step 3: RETURN F

ASCET(Autonomous) R23- CP LAB MANUAL Page 80


Program:

#include<stdio.h>
#include<conio.h>

int Fact(int);

void main()
{
int N,R,Res;
clrscr();
printf("\n Enter N Value = ");
scanf("%d",&N);
printf("\n Enter R Value = ");
scanf("%d",&R);
Res = Fact ( N ) / ( Fact ( R ) * Fact ( N – R ) ) ;
printf("\n NCR Result Value = %d" , Res ) ;
}

int Fact(int K)
{
int i , F = 1 ;
for ( i = 1 ; i < = K ; i++ )
F=F*i;
return F;
}

Result

Enter N Value = 5

Enter R Value = 3

NCR Result Value = 10

ASCET(Autonomous) R23- CP LAB MANUAL Page 81


Program No: 49
Write a C function to find the length of a string.

Algorithm:

Step 1: START
Step 2: INPUT a string as ch
Step 3: N ← Length ( ch )
Step 4: WRITE N value
Step 5: STOP

Length ( STR ): Function returns length of the string STR.

Step 1: count ← 0
Step 1: REPEAT FOR i ← 0 TO STR[ i ] ≠ ‘\0’ DO STEPS BY 1
count ← count + 1
ENDREPEAT
Step 3: RETURN count

Program:

#include<stdio.h>

int Length( char[ ] ) ;

void main()
{
char ch[50];
int N;
clrscr();
printf("\n Enter A String = ");
gets(ch);
N = Length ( ch ) ;
printf("\n String Length = %d" , N ) ;
}
int Length(char STR[50])
{
int i , count = 0 ;
for ( i = 0 ; STR[i] ! = '\0' ; i++ )
count = count + 1 ;
return count;
}

Result

Enter A String = New Delhi


String Length = 9

ASCET(Autonomous) R23- CP LAB MANUAL Page 82


Program No: 50
Write a C function to transpose of a matrix.

Algorithm:

Step 1: START
Step 2: INPUT Number of Rows as R and Columns as C
Step 3: REPEAT FOR i ← 0 TO R - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C - 1 DO STEPS BY 1
INPUT K[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO R - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C - 1 DO STEPS BY 1
WRITE K[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 5: Call Transpose ( K , R , C )
Step 6: REPEAT FOR i ← 0 TO C - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO R - 1 DO STEPS BY 1
WRITE T[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 7: STOP

Transpose ( A , R1 , C1): This procedure transposes the given matrix elements of A.

Step 1: REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1


REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
T[j][i] ← A[i][j]
ENDREPEAT
ENDREPEAT
Step 2: RETURN

ASCET(Autonomous) R23- CP LAB MANUAL Page 83


Program:

#include<stdio.h>
#include<conio.h>

void Transpose(int[ ][10],int,int);

int K[10][10],T[10][10];

void main()
{
int R,C,i,j;
clrscr();
printf("\n Enter How Many Rows = ");
scanf("%d",&R);
printf("\n Enter How Many Columns = ");
scanf("%d",&C);
printf("\n Enter Matrix Elements =");
for ( i = 0 ; i < R ; i++ )
{
for ( j = 0 ; j < C ; j++ )
scanf("%d" , &K[i][j] ) ;
}
printf("\n\n Actual Matrix Elements = ");
for ( i = 0 ; i < R ; i++ )
{
printf("\n");
for ( j = 0 ; j < C ; j++ )
printf("%d " , K[i][j] ) ;
}
Transpose ( K , R , C ) ;
printf("\n\n Transpose Matrix Elements = ");
for ( i = 0 ; i < C ; i++ )
{
printf("\n");
for ( j = 0 ; j < R ; j++ )
printf("%d ", T[i][j] ) ;
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 84


void Transpose(int A[10][10] , int R1 , int C1)
{
int i,j;
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
T[j][i] = A[i][j] ;
}
}
.

Result

Enter How Many Rows = 3

Enter How Many Columns = 2

Enter Matrix Elements = 11 22


33 44
55 66

Actual Matrix Elements = 11 22


33 44
55 66

Transpose Matrix Elements = 11 33 55


22 44 66

ASCET(Autonomous) R23- CP LAB MANUAL Page 85


Program No: 51
Write a C function to demonstrate numerical integration of differential equations using
Euler’s method.

Euler method
The Euler method gives an approximation for the solution of the differential equation:
dy / dt = f ( t , y )
with the initial condition:
y (t0) = y0
where t is continuous in the interval [a, b].

Algorithm:

Step 1: START
Step 2: Define a Macro as f ( x, y ) x + y
Step 3: INPUT x0 , y0 , P and N values
Step 4: h ← ( P - x0 ) / N
Step 5: REPEAT FOR i ← 1 TO N DO STEPS BY 1
Slope ← f ( x0 , y0 )
Res ← y0 + h * Slope
WRITE x0 , y0 , Res values
y0 ← Res
x0 ← x0 + h
ENDREPEAT
Step 6: WRITE Res value
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 86


Program:

#include<stdio.h>
#include<conio.h>

#define f ( x , y ) x+y

void main()
{
float x0,y0,h,P,Res,Slope;
int N,i;
clrscr();
printf("\n Enter Initial Value X0 = ");
scanf("%f",&x0);
printf("\n Enter Initial Value Y0 = ");
scanf("%f",&y0);
printf("\n Enter Calculation Point = ");
scanf("%f",&P);
printf("\n Enter Number of Steps = ");
scanf("%d",&N);
h = ( P - x0 ) / N ;
printf("\n X0\t Y0\t Res\n");
for ( i = 1 ; i < = N ; i++ )
{
Slope = f ( x0 , y0 ) ;
Res = y0 + h * Slope ;
printf("\n %.2f\t %.2f\t %.2f" , x0 , y0 , Res ) ;
y0 = Res ;
x0 = x0 + h ;
}
printf("\n Result Value at %.2f is = %.2f" , P , Res ) ;
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 87


Result

Enter Initial Value X0 = 0

Enter Initial Value Y0 = 1

Enter Calculation Point = 1

Enter Number of Steps = 10

X0 Y0 Res
0.00 1.00 1.10
0.10 1.10 1.22
0.20 1.22 1.36
0.30 1.36 1.53
0.40 1.53 1.72
0.50 1.72 1.94
0.60 1.94 2.20
0.70 2.20 2.49
0.80 2.49 2.82
0.90 2.82 3.19

Result Value at 1.00 is = 3.19

ASCET(Autonomous) R23- CP LAB MANUAL Page 88


Program No: 52
Write a recursive function to generate Fibonacci series.

Algorithm:

Step 1: START
Step 2: INPUT number of terms as K
Step 3: REPEAT FOR i ← 0 TO K - 1 DO STEPS BY 1
P ← Fib ( i )
WRITE P value
ENDREPEAT
Step 4: STOP

Fib ( N ): Function returns Fibonacci term of the argument number N.

Step 1: IF N = 0 OR N = 1 THEN
RETURN N
ELSE
RETURN Fib ( N – 2 ) + Fib ( N – 1 )
ENDIF

ASCET(Autonomous) R23- CP LAB MANUAL Page 89


Program:

#include<stdio.h>
#include<conio.h>

int Fib(int);

void main()
{
int i,K,P;
clrscr();
printf("\n Enter How Many Terms = " ) ;
scanf("%d",&K);
printf("\n Fibonacci Sequence = ");
for ( i = 0 ; i < K ; i++ )
{
P = Fib ( i ) ;
printf(" %d" , P ) ;
}
}

int Fib(int N)
{
if ( N = = 0 | | N = = 1 )
return N;
else
return Fib ( N – 1 ) + Fib ( N – 2 ) ;
}

Result

Enter How Many Terms = 5

Fibonacci Sequence = 0 1 1 2 3

ASCET(Autonomous) R23- CP LAB MANUAL Page 90


Program No: 53
Write a recursive function to find the LCM of two numbers.

Algorithm:

Step 1: START
Step 2: INPUT two values as P and Q
Step 3: R ← LCM ( P , Q )
Step 4: WRITE R value
Step 5: STOP

LCM ( X , Y ): Function returns LCM of given two values X and Y.

Step 1: Define a static variable count and initialized as count ← 1


Step 2: IF count MOD X = 0 AND count MOD Y = 0 THEN
RETURN count
ELSE
count ← count + 1
Call LCM ( X , Y ) ;
RETURN count
ENDIF

ASCET(Autonomous) R23- CP LAB MANUAL Page 91


Program:

#include<stdio.h>
#include<conio.h>

int LCM(int,int);

void main()
{
int P,Q,R;
clrscr();
printf("\n Enter Two Values = " ) ;
scanf("%d%d",&P,&Q);
R = LCM ( P , Q ) ;
printf("\n LCM Result = %d" , R ) ;
}

int LCM(int X,int Y)


{
static int count = 1 ;
if ( count % X = = 0 && count % Y = = 0 )
return count;
else
{
count = count + 1 ;
LCM ( X , Y ) ;
return count;
}
}

Result

Enter Two Values = 24 37

LCM Result = 888

ASCET(Autonomous) R23- CP LAB MANUAL Page 92


Program No: 54
Write a recursive function to find the factorial of a number.

Algorithm:

Step 1: START
Step 2: INPUT a values as K
Step 3: P ← Fact ( K )
Step 4: WRITE P value
Step 5: STOP

Fact ( N ): Function returns factorial value of the argument N.

Step 1: IF N = 1 THEN)
RETURN 1
ELSE
RETURN N * Fact ( N – 1 )
ENDIF

Program:

#include<stdio.h>

int Fact(int);

void main()
{
int K,P;
clrscr();
printf("\n Enter A Value = " ) ;
scanf("%d",&K);
P = Fact ( K ) ;
printf("\n Factorial Value = %d" , P ) ;
}
int Fact(int N)
{
if ( N = = 1 )
return 1;
else
return N * Fact ( N – 1 ) ;
}

Result

Enter A Value = 5

Factorial Value = 120

ASCET(Autonomous) R23- CP LAB MANUAL Page 93


Program No: 55
Write a C Program to implement Ackermann function using recursion.

Algorithm:

Step 1: START
Step 2: INPUT two values as P and Q
Step 3: R ← Ack ( P , Q )
Step 4: WRITE R value
Step 5: STOP

Ack ( M , N ): Function returns Ackermann functional value of the given


two arguments M and N.

Step 1: IF M = 0 THEN
RETURN N + 1
ELSEIF N = 0 THEN
RETURN Ack ( M – 1 , 1 )
ELSE
RETURN Ack ( M – 1 , Ack ( M , N – 1 ) )
ENDIF

ASCET(Autonomous) R23- CP LAB MANUAL Page 94


Program:

#include<stdio.h>
#include<conio.h>

int Ack(int,int);

void main()
{
int P,Q,R;
clrscr();
printf("\n Enter Two Non-Negative Values = " ) ;
scanf("%d%d",&P,&Q);
R = Ack ( P , Q ) ;
printf("\n Ackermann Function Result = %d" , R ) ;
}

int Ack(int M,int N)


{
if ( M = = 0 )
return N + 1 ;
else if ( N = = 0 )
return Ack ( M – 1 , 1 ) ;
else
return Ack ( M – 1 , Ack ( M , N – 1 ) ) ;
}

Result

Case 1: Enter Two Non-Negative Values = 0 5

Ackermann Function Result = 6

Case 2: Enter Two Non-Negative Values = 1 0

Ackermann Function Result = 2

Case 3: Enter Two Non-Negative Values = 2 1

Ackermann Function Result = 5

ASCET(Autonomous) R23- CP LAB MANUAL Page 95


Program No: 56
Write a recursive function to find the sum of series.

Algorithm:

Step 1: START
Step 2: INPUT Number of terms as N
Step 3: R ← RSum (N)
Step 4: WRITE R value
Step 5: STOP

Rsum ( K ): Function is used to find recursive sum of the passed terms K.

Step 1: IF K = 0 THEN
RETURN 0
ELSE
RETURN K + RSum ( K – 1)
ENDIF

Program:

#include<stdio.h>
int RSum(int);
void main()
{
int N,R;
clrscr();
printf("\n Enter How Many Terms = " ) ;
scanf("%d",&N);
R = RSum ( N ) ;
printf("\n Sum Result Value = %d" , R ) ;
}

int RSum(int K)
{
if ( K = = 0 )
return 0;
else
return K + RSum ( K – 1 ) ;
}

Result

Enter How Many Terms = 5

Sum Result Value = 15

ASCET(Autonomous) R23- CP LAB MANUAL Page 96


Program No: 57
Write a C program to write and read text into a file.

Algorithm:

Step 1: START
Step 2: Open a file pointer FP in “Write” mode
Step 3: REPEAT WHILE (ch = getchar( )) ≠ EOF
fputc( ch, FP)
ENDREPEAT
Step 4: Close the file pointer FP
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
FILE *FP;
char ch;
clrscr();
FP=fopen("Demodata.txt","w");
printf("\n Enter Data Into the File =");
while((ch=getchar())!=EOF)
fputc(ch,FP);
fclose(FP);
FP=fopen("Demodata.txt","r");
if(FP==NULL)
{
printf("\n File Opening Error");
exit();
}
printf("\n File Contents Are =\n");
while((ch=fgetc(FP))!=EOF)
putchar(ch);
fclose(FP);
}

Result

Enter Data Into the File = C and C++


Languages.
File Contents Are =
C and C++
Languages.

ASCET(Autonomous) R23- CP LAB MANUAL Page 97


Program No: 58
Write a C program to write and read text into a binary file using fread() and fwrite()
functions.

Algorithm:

Step 1: START
Step 2: Define a student structure with rno as integer, name and branch as strings
Step 3: Open a file pointer f in “wb” mode
Step 4: INPUT number of students as n
Step 5: REPEAT FOR i ← 1 TO n DO STEPS BY 1
INPUT s[ i ].rno , s[ i ].name , s[ i ].branch
fwrite(&s[i],sizeof(s[i]),1,f)
ENDREPEAT
Step 6: Close file pointer f
Step 7: Open a file pointer f in “rb” mode
Step 8: REPEAT FOR i ← 1 TO n DO STEPS BY 1
fread(&s[i],sizeof(s[i]),1,f);
WRITE s[ i ].rno , s[ i ].name , s[ i ].branch
ENDREPEAT
Step 9: Close file pointer f
Step 10: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 98


Program:

#include<stdio.h>
#include<conio.h>

typedef struct
{
int rno;
char name[25],branch[5];
}student;

void main()
{
int i,n;
student s[20];
FILE *f = fopen("stdemo.txt","wb");
clrscr( );
printf("\n Enter how many students:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Student %d data:\n",i);
printf("\n Enter Student Roll Number =");
scanf("%d",&s[i].rno);
fflush(stdin);
printf("\n Enter Name of the Student =");
gets(s[i].name);
fflush(stdin);
printf("\n Enter Branch of the Student =");
gets(s[i].branch);
fwrite(&s[i],sizeof(s[i]),1,f);
}
fclose(f);
f = fopen("stdemo.txt","rb");
printf("\n Database Details Are:\n");
for(i=1;i<=n;i++)
{
fread(&s[i],sizeof(s[i]),1,f);
printf("\n%d\t%s\t%s",s[i].rno,s[i].name,s[i].branch);
}
fclose(f);
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 99


Result

Enter how many students: 2

Student 1 data:

Enter Student Roll Number = 111

Enter Name of the Student = Ravi

Enter Branch of the Student = CSE

Student 2 data:

Enter Student Roll Number = 222

Enter Name of the Student = Vishnu

Enter Branch of the Student = CSEAI

Database Details Are:

111 Ravi CSE

222 Vishnu CSEAI

ASCET(Autonomous) R23- CP LAB MANUAL Page 100


Program No: 59
Write a program to copy the contents of one file to another file.

Algorithm:

Step 1: START
Step 2: Open a file pointer FP1 in “Read” mode
IF FP1 = NULL THEN
WRITE ‘File Not Opened’
EXIT
ENDIF
Step 3: Open a file pointer FP2 in “Write” mode
Step 4: REPEAT WHILE (ch = fgetc(FP1 )) ≠ EOF
fputc( ch, FP2)
ENDREPEAT
Step 4: Close the file pointers FP2, FP1
Step 5: STOP

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
FILE *FP1,*FP2;
char ch;
clrscr();
FP1=fopen("Demodata.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
FP2=fopen("Copydata.txt","w");
while((ch=fgetc(FP1))!=EOF)
fputc(ch,FP2);
fclose(FP2);
fclose(FP1);
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 101


Result

Demodata.txt

C and C++
Programming Languages.

Copydata.txt

C and C++
Programming Languages.

ASCET(Autonomous) R23- CP LAB MANUAL Page 102


Program No: 60
Write a C program to merge two files into the third file using command-line arguments.

Algorithm:

Step 1: START

Step 6: STOP

Program:

#include<stdio.h>
#include<conio.h>

int main(int argc, char *argv[ ])


{
char ch;
FILE *FP1,*FP2,*FP3;
clrscr();
FP1=fopen(argv[1] , "r");
if(FP1==NULL)
{
printf("\nFile Opening Error");
exit();
}
FP3=fopen(argv[3] , "w");
while((ch=fgetc(FP1))!=EOF)
fputc(ch,FP3);
fclose(FP3);
fclose(FP1);
FP2=fopen(argv[2],"r");
if(FP2==NULL)
{
printf("\nFile Opening Error");
exit();
}
FP3=fopen(argv[ 3 ] , "a");
while((ch=fgetc(FP2))!=EOF)
fputc(ch,FP3);
fclose(FP3);
fclose(FP2);
return 1;
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 103


Result

Demodata.txt

C and C++
Programming Languages.

Copydata.txt

Java Language.

Resultdata.txt

C and C++
Programming Languages.
Java Language.

ASCET(Autonomous) R23- CP LAB MANUAL Page 104


Program No: 61
Write a program to find no. of lines, words and characters in a file.

Algorithm:

Step 1: START
Step 2: Open a file pointer FP1 in “Read” mode
IF FP1 = NULL THEN
WRITE ‘File Not Opened’
EXIT
ENDIF
Step 3: Lines ← 1
Words ← 1
Nchar ← 0
Step 4: REPEAT WHILE (ch = fgetc(FP1 )) ≠ EOF
Nchar ← Nchar + 1
IF isspace(ch) THEN
Words ← Words + 1
Nchar ← Nchar + 1
ENDIF
IF ch = ‘\n’ THEN
Lines ← Lines + 1
ENDIF
ENDREPEAT
Step 5: WRITE Lines, Words, Nchar values
Step 6: Close the file pointer FP1
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 105


Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{
FILE *FP1;
char ch;
int Lines,Words,Nchar;
clrscr();
FP1=fopen("Count.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
Lines=1;
Words=1;
Nchar=0;
while((ch=fgetc(FP1))!=EOF)
{
Nchar=Nchar+1;
if(isspace(ch))
{
Words=Words+1;
Nchar=Nchar-1;
}
if(ch=='\n')
{
Lines=Lines+1;
}
}
printf("\n Number of Lines = %d",Lines);
printf("\n Number of Words = %d",Words);
printf("\n Number of Characters = %d",Nchar);
fclose(FP1);
}

ASCET(Autonomous) R23- CP LAB MANUAL Page 106


Result

Count.txt
C Language
In 1972.

Number of Lines = 2

Number of Words = 4

Number of Characters = 16

ASCET(Autonomous) R23- CP LAB MANUAL Page 107


Program No: 62
Write a C program to print last n characters of a given file.

Algorithm:

Step 1: START
Step 2: Open a file pointer FP1 in “r” mode
Step 3: INPUT number of characters as N
Step 4: fseek(FP1,-N,2)
Step 5: REPEAT WHILE (ch = fgetc(FP1) ) ≠ EOF
WRITE ch
ENDREPEAT
Step 6: Close the file pointer FP1
Step 7: STOP

ASCET(Autonomous) R23- CP LAB MANUAL Page 108


Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{
FILE *FP1;
char ch;
int N;
clrscr();
FP1=fopen("Check.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
printf("\n Enter How Many Characters You Need =");
scanf("%d",&N);
printf("\n Resultant Characters = \n");
fseek(FP1,-N,2);
while((ch=fgetc(FP1))!=EOF)
putchar(ch);
fclose(FP1);
}

Result

Check.txt
C and C++
Programming Languages.

Enter How Many Characters You Need = 5

Resultant Characters = ages.

THE END

ASCET(Autonomous) R23- CP LAB MANUAL Page 109

You might also like