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

CPNM Lab Manual

The document is a laboratory manual for the Computer Programming and Numerical Methods Lab (EC1208) for B. Tech students at Sanketika Vidya Parishad Engineering College. It outlines the department's vision, mission, program educational objectives, specific outcomes, course objectives, and expected outcomes for students. Additionally, it includes general laboratory instructions, a syllabus with programming tasks, and sample programs to be implemented by students.

Uploaded by

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

CPNM Lab Manual

The document is a laboratory manual for the Computer Programming and Numerical Methods Lab (EC1208) for B. Tech students at Sanketika Vidya Parishad Engineering College. It outlines the department's vision, mission, program educational objectives, specific outcomes, course objectives, and expected outcomes for students. Additionally, it includes general laboratory instructions, a syllabus with programming tasks, and sample programs to be implemented by students.

Uploaded by

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

Computer Programming and Numerical

Methods Lab
(EC1208)
LABORATORY MANUAL & RECORD

B. Tech (Common for ECE, Civil, Mechanical)


(With effect from 2022-23 admitted batches)
(I YEAR- IISEM)

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

SANKETIKA VIDYA PARISHAD ENGINEERING COLLEGE


(APPROVED BY AICTE, AFFILIARED TO ANDHRA
UNIVERSITY, ACCREDITED BY NAAC-A GRADE, ISO 9001:2015
CERFIFIED) PM PALEM, VISAKHAPATNAM-41,
www.svpce.edu.in
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INSTITUTE VISION AND MISSION
VISION
To be a premier institute of knowledge of share quality research and development
technologies towards national buildings
MISSION
1. Develop the state of the art environment of high quality of learning
2. Collaborate with industries and academic towards training research innovation and
entrepreneurship
3. Create a platform of active participation co-circular and extra- circular activities

DEPARTMENT VISION AND MISSION


VISION
To impart quality education for producing highly talented globally recognizable techno carts
and entrepreneurs with innovative ideas in computer science and engineering to meet
industrial needs and societal expectations

MISSION
 To impart high standard value-based technical education in all aspects of
Computer Science and Engineering through the state of the art infrastructure and
innovative approach.
 To produce ethical, motivated, and skilled engineers through theoretical
knowledge and practical applications.
 To impart the ability for tackling simple to complex problems individually as well
as in a team.
 To develop globally competent engineers with strong foundations, capable of “out
of the box” thinking so as to adapt to the rapidly changing scenarios requiring
socially conscious green computing solutions.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)
Graduates of B. Tech in computer science and Engineering Programme shall be able to
PEO1: Strong foundation of knowledge and skills in the field of Computer Science
and Engineering.
PEO2: Provide solutions to challenging problems in their profession by applying
computer engineering theory and practices.
PEO3: Produce leadership and are effective in multidisciplinary environment.

PROGRAMME SPECIFIC OUTCOMES (PSOs)


PSO1: Ability to design and develop computer programs and understand the structure and
development methodologies of software systems.
PSO2: Ability to apply their skills in the field of networking, web design, cloud computing
and data analytics.
PSO3: Ability to understand the basic and advanced computing technologies towards getting
employed or to become an entrepreneur
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
PROGRAM OUTCOMES
1. Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem Analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/Development of Solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct Investigations of Complex Problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modelling to complex engineering
activities with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, give and
receive clear instructions.
11. Project Management and Finance: Demonstrate knowledge andunderstanding of the
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-Long Learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COURSE OBJECTIVES
• To impart writing skill of C programming to the students and solving problems.
• To write and execute programs in C to solve problems such as Modularize the problems
into small modules and then convert them into programs.,
• To write and execute programs in C to solve problems such as arrays, files, strings,
structures and different numerical methods.
• This reference has been prepared for the beginners to help them understand the basic to
advanced concepts related to Objective-C Programming languages.

COURSE OUTCOMES (CO)


After completion of the course, students will be able to:

 CO1: Identify basic elements of C programming structures like data types,


expressions, control statements, various simple functions and apply them in problem
solving.
 CO2: Apply various operations on derived data types like arrays and strings in problem
solving.
 CO3: Design and implement of modular Programming and memory management
using Functions, pointers.
 CO4: Apply Structure, Unions and File handling techniques to Design and Solve
different engineering programs with minimal complexity.
 CO5: Apply Numerical methods to Solve the complex Engineering problems.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
GENERAL LABORATORY INSTRUCTIONS

1. Students are advised to come to the laboratory at least 5 minutes before (to the starting
time), those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with
the synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments and other utensils (if any)
needed in the lab.
c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation
note book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain
the discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which
should be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out; if
anybody found loitering outside the lab / class without permission during working hours will
be treated seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the
lab after completing the task (experiment) in all aspects. He/she must ensure the system / seat
is kept properly
EC1208 Computer Programming and Numerical Methods Lab
Syllabus

Practice the following concepts with algorithm, flow chart and implementation.

1. Write a program to read x, y coordinates of 3 points and then calculate the area of a triangle
formed by them and print the coordinates of the three points and the area of the triangle.
What will be the output from your program if the three given points are in a straight line?
2.Write a program, which generates 100 random integers in the range of 1 to 100. Store them
in an array and then print the arrays. Write 3 versions of the program using different
loop constructs. (e.g. for, while, and do while).
3.Write a set of string manipulation functions e.g. for getting a sub-string from a given
position, copying one string to another, reversing a string, adding one string to another.
4.Write a program which determines the largest and the smallest number that can be
stored in different data types like short, int, long, float, and double. What happens when
you add 1 to the largest possible integer number that can be stored?
5.Write a program, which generates 100 random real numbers in the range of 10.0 to 20.0,
and sort them in descending order.
6. Write a function for transposing a square matrix in place (in place means that you are not
allowed to have full temporary matrix).
7.First use an editor to create a file with some integer numbers. Now write a program,
which reads these numbers and determines their mean and standard deviation.
8.Given two points on the surface of the sphere, write a program to determine the smallest
Arc length between them.
9.Implement bisection method to find the square root of a given number to a given accuracy.
10.Implement Newton Raphson method to det. a root of polynomial equation.
11.Given table of x and corresponding f(x) values, write a program which will determine
12.f(x) value at an intermediate x value by using Lagrange’s interpolation/
13.Write a function which will invert a matrix.
14.Implement Simpson’s rule for numerical integration.
15.Write a program to solve a set of linear algebraic equation.

Reference Books
1.Programming in ANSI C, E Balagurusamy, 6th Edition. McGraw Hill Education
(India) Private Limited.
2.Introduction to Numerical Methods, SS Sastry, Prentice Hall.
TABLE OF CONTENTS

Sl. CONTENT Page


No No
1 Write a program to read x, y coordinates of 3 points and then calculate the 1-2
area of a triangle formed by them and print the coordinates of the three
points and the area of the triangle. What will be the output from your
program if the three given points are in a straight line?

2 Write a program, which generates 100 random integers in the range 3-5
of 1 to 100. Store them in an array and then print the arrays. Write 3
versions of the program using different loop constructs. (e.g. for,
while, and do while).

3 Write a set of string manipulation functions e.g. for getting a sub- 6-7
string from a given position, copying one string to another, reversing
a string, adding one string to another.

4 Write a program which determines the largest and the smallest 8-10
number that can be stored in different data types like short, int, long,
float, and double. What happens when you add 1 to the largest
possible integer number that can be stored?

5 Write a program, which generates 100 random real numbers in the 11-12
range of 10.0 to 20.0, and sort them in descending order.

6 Write a function for transposing a square matrix in place (in place 13-14
means that you are not allowed to have full temporary matrix).

7 First use an editor to create a file with some integer numbers. Now 15-17
write a program, which reads these numbers and determines their
mean and standard deviation.

8 Given two points on the surface of the sphere, write a program to 18-19
determine the smallest arc length between them.

9 Implement bisection method to find the square root of a given number to 20-22
a given accuracy.

10 Implement Newton Raphson method to det. a root of polynomial 23-26


equation.

11 Given table of x and corresponding f(x) values, write a program 27-29


which will determine f(x) value at an intermediate x value by using
Lagrange’s interpolation/

12 Write a function which will invert a matrix 30-31


13 Implement Simpson’s rule for numerical integration. 32-33
14 Write a program to solve a set of linear algebraic equations 34-35
15 Write a C program to calculate matrix addition 36-38

16 Write a C program to calculate matrix multiplication

17 Write a C program to store information structure and display it.


18 Write a C program to calculate find the biggest number in an array
19
20
Sanketika Vidya Parishad Engineering
College
PROGRAM-1
Aim: Write a program to read x, y coordinates of 3 points and then calculate the area of a
triangle formed by them and print the coordinates of the three points and the area of the
triangle. What will be the output from your program if the three given points are in a straight
line?
Algorithm:
Step 1: Program start.
Step 2: Declare the choice, area, a, b, c, base,
height x1,x2,x3,y1,y2,y3.

Step 3: Read choices.


Step 4: If choice is matched with case :1 read base and height.
Step 5: finding area =(base*height) *0.5 store the result
in area.
Step 6: Display area.
Step 7: If choice is matched with case 2, Read a,b,c.
Step 8: Add a,b,c and divide by 2 and store in ‘ S’.
S=a+b+c/2
Step 9: Find area =sqrt(s*(s-a)*(s-b)*(s-c) and store in
area.
Step 10: Display area.
Step 11: If choice is matched with case 3, Read
x1,x2,x3,y1,y2,y3.
Step 12: Find area =x1*(y1-y2)+x2*(y3-y1)+x3(y1-y2) and store
in area.
Step 13: Display area.
Program:
#include<stdio.h
>
#include<conio.
h>
#include<math.
h>
void main ()
{
Int
choice;
1
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Float area, a, b, c, base, height, s,College
x1, x2, x3, y1, y2, y3.
Clrscr();
printf("Enter the value to calculate area of triangle by one
method:\n");
printf(“1.using base and height\n”);
printf(“2.using sides of a triangle \n”);
printf(“3.using coordinates of a triangle\n”);
printf(“ enter your choice:\n”);
Scanf(“%d”,&choice);

Switch(choice)
{
Case1: scanf (“%f %f”, &base ,&height);
Area=(base*height)*0.5;
Printf(“area=%f(h”,area);
Break;
Case2: Scanf(“%f %f %f ”,&a,&b,&c);
S=a+b+c/2;
Break;
Case 3: Scanf(%f%f%f%f%f”,&x1,&x2,&x3,&y1,&y2,&y3);
Area = x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
Printf(“area=%f(h”,area);
Break;
Default;Printf(“enter your appropriate choice(n)”);
}
If(area=0)
{
Printf(“coordinates are in straight line :\n”);
}
Else
{
If(a>0)
{
Printf(“%f=area”,area);
}
Else
{
Area=area*(-1);
Printf(“%f=area”,area);
}
getch ();
}
2
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Output: College
Enter the value to calculate the area of triangle by any one
method
1.using base and height
2.Using sides of triangle

3.Using coordinates of traiangle

Enter your choice 1

10.0 20.0

Area=100.00000

Enter your choice2

2.5 4.5 1.5

Area=1.875000

Enter your choice 3

1.5 2.5 3.5 4.5 5.5 6.5

Area=0.00000

Coordinates are in a straight line

3
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

4
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
PROGRAM -2
Aim: Write a program, which generates 100 random integers in the range of 1 to 100.
Store them in an array and then print the arrays. Write 3 versions of the program using
different loop constructs. (e.g. for, while, and do while).
Algorithm:

Step 1: Start
Step 2: Declare the variable a[100] and I for running loop.
Step 3: Now initialization the value of variable i=0 and check the condition i<100
Step 4: If the condition is true enters into loop. if the condition false it will exit from the
loop.
Step 5: Stop

Program:

#include<stdio.h
>
#include<conio.
h>
#include<stdlib.
h> int main()
{
Int a[100], i ;
Clrscr();
Printf(“1 to 100 numbers randomly”);
For(i=0;i<100;i++)
{
a[p]=random(100)+1;
printf(“%d\+”,a[i];
}
getch();
}

5
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Output:
1 to numbers randomly
72 19 93 51 85 54 63 73
60 46
70 87 1 27 73 16 16 75
45 17
34 31 1 67 35 29 4 74
77 59
21 10 71 81 67 86 18 98
55 60
75 65 19 34 34 54 84 100
82 27
12 33 46 86 4 37 49 92
81 81
71 76 31 51 21 32 46 53
66 5
89 49 42 45 81 82 79 30
64 33
76 8 36 43 44 77 53 45
49 28
12 56 3 56 84 52 5 4 2
20

6
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flowchart:

7
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

8
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
PROGRAM -3
Write a set of string manipulation functions e.g. for getting a sub-string from a given
position, copying one string to another, reversing a string, adding one string to another.
Aim: Write a program to copy a string from s1to s2 without using string functions.

Algorithm:
Step 1: Start.
Step 2: Declare the strings s1[10], s2[10], i.
Step 3: Read the string s1
Step 4: Assign i=0 and check the condition s1[i]=%d
Step 5: If the condition is true enters into the block of statements.
Step 6: String from s1 is copied to s2.
Step 7: Check s2[i]=”10”
Step 8: Print the copied string.
Step 9: Stop.

Program:
#include
<stdio.h>
#include
<conio.h> int
main()
{
int i;
char s1[10],s2[10];
clrscr();
printf("Enter the string :");
Scanf("%s”,s1);
for(i=0;s1[i]!=’10’;i++;
{
S2[i]=s1[i];
}
S1[i]=’10’;
Printf(“the copied string from s1to s2 is %s”,s2);
getch();
}

Output:
Enter the string Srilatha
The copied string from s1 to s2 is Srilatha

9
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

10
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

11
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Aim: Write a C program to find length of string without using string


function
Algorithm:
Step 1: Start.
Step 2: Declare the string s1[10];
Step 3: Read the Strings s1
Step 4: Assign i=0 and check s1[i]!=’10’
Step 5: If the condition is true enters into true block statement ‘i’ will be incremented.
Step 6: Again check the condition and runs the loop until the loop fails
Step 7: stop

Program:
#include<stdio.h
>
#include<conio.
h> void main()
{
int i;
char s1[10];
printf("Enter any string: ");
scanf("%s",s1);
for (i=0; s1[i]!=’10’;i++)
{
Printf(“the length of string is %d”,i);
getch ();
}
Output:
Enter any string Rama
The length of string is 4

12
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

13
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Aim: Write a program to reverse a string without using string


functions.

Algorithm:
Step 1: Start
Step 2: Declare the string s1[10],s2[10],i,j.
Step 3: Read the string s1
Step 4: Assign i=0,check s[i]=’10’ enter into the length true block
Step 5: the length of string is printer.
Step 6: Assign i=i-1,j=0 check i>=0 enter into true block.
Step 7: the string from s1is copied to s2
Step 8: then i-- and j++ will be done run the until the loop fails
Step 9: Stop

#include<stdio.h
Program:

>
#include<stdio.h
> int main ()
{
Int i , j ;
Char s1[10], s2[10];
Clrscr();
Printf(“enter any
string”);
Scanf(“%s”,s1);
for(i=0;s1[i]!=’Φ’;i++)
{
Printf(“the length of string %d\
n”,i);
for (i=i-1;j=0;i>=0;i--,j++)
{
S2[j]=s1[i];
}
S2[j]=’\0’;
Printf(“the reverse of a string is
%s\n”,s2);
getch();
14
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
} College

Output:
Enter any string rama
The length of string 4
The reverse of a string is amar

15
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

16
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Aim: Write a program of strings changing to uppercase copying


string from s1 to s2 , string reverse ,string concatenation with
using string functions

Algorithm:

Step 1: Start
Step 2: Declare the string s1[10],s2[10]
Step 3: Read the string s1
Step 4: Changing s1 to uppercase by using strupr() string function
Step 5:copying string from s1 to s2 by using strcpy() string function
Step 6: Reversing string s2by using strrev() string function
Step 7: Concatenation string s2 to s1by using strcat().
Step 8: Printing strupr,strcpy,strrev,strcat,results.
Step 9:Stop

Program:
#include<stdio.h
>
#include<conio.
h>
#include<string.
h> int main ()
{
Char s1[10],s2[10];
Clrscr();
Printf(“enter any string”);
Scanf(“%s”,s1);
Printf(“\n changing s1 to uppercase%s”,strupr(s1));
Strcpy(s2,s1);
Printf(“\n after copying string from s1to s2is %s”,s2);
Strrev(s2);
Printf(“\n after reversing the string s2 is %s”,s2);
Strcat(s1/s2);
Printf(“\n after concatenation of string s2to s1is %s”,s1);
17
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
getch(); College
}

18
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
output:
enter any string rama
changing s1 to uppercase rama
after copying string from s1 to s2is RAMA
after reversing the string s2 is AMAR
after concatenation of string s2 to s1 RAMA AMAR

Flow chart:

19
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Aim: Write C program to display the concatenation of strings

Algorithm:
Step 1: Start
Step 2: Declare the strings s1[10],s2[10],s3[20],i,j.
Step 3: Read the string s1[10],s2[10].
Step 4: Assign i=0 and check s1[i] !=’\0’ if the condition is true enters
into true block. the string from s1[i] is copied into s3[9]
Step 5: If the condition fails until loop
Step 6: Assign j=0.check s2[j]=’\0’.if the condition is true then enters
into true block. The string from s2[j] is copied into s3[1],i++,j++ will be
increment and run the loop.
Step 7: If the condition fails goes to first for loop and run the loop.
Step 8: check s3[i]=’\0’ then print concatenation of string.
Step 9: stop.
Program:
#include
<stdio.h>
#include
<conio.h> void
main ()

Char
s1[10],s2[10],s3[10]
;
Int i,j;
Clrscr();
Printf(“enter any string”);
Scanf(“%s”,s1);
Printf(“enter any string”);
Scanf(“%s”,s2);
For(i=0;s1[i]!=’\0’;i++)
{
S3[i]=s1[i];
}
For(j=0;s2[j]!=’\0’;j++,i++)
{

20
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
S3[i]=s2[j]; College
}
S3[i]=’10’;

Printf(“the concatenation of a string is


%s”,s3);
getch ();
}

Output:
Enter any string Rama
Enter any string Sita
The concatenation of a string is RamaSita
Flowchart:

21
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

22
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Aim: Write a C program to check whether the given string is


palindrome or not

Algorithm:

Step 1: Start
Step 2: Declare the strings s1[10],s2[10].
Step 3: Read the strings s1
Step 4: Copying string from s1 to s2 by using strcpy(s2,s1).
Step 5: Reversing string s2 by using strrev(s2)
Step 6: Check strcmp(s1s2==0 if the condition is true then the string is palindrome.
Step 7: if the condition fails then the string is not a palindrome.
Step 9: Stop

Program:
#include
<stdio.h>
#include
<conio.h>
#include <string.h>
int main ()
{
Char s1[10],s2[10];
Clrscr();
Printf(“ enter the string”);
Scanf(“%s”,s1);
Strcpy(s2,s1);
Printf(“After copying string from s1 to s2 is %s”,s2);
Strrev(s2);
Printf(“After reversing s2 if %s”,s2);
If(strcmp(s1,s2)==0)
{
Printf(“%s string is a palindrome”,s1);
else
{
Printf(“%s string is not a palindrome “,s2);
}
getch();
}
Output:
23
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Enter any string wow College
After copying string from s1 to s2 is wow
After reversing s2 is wow
Wow string is palindrome.

24
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow Chart:

25
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-4

Aim: Write a program which determine the largest and the smallest number that can be
stored in data types like short int,long,float and double.

Algorithm:
Step 1: Start
Step 2: Print shortest integer value
Step 3: print longest integer value
Step 4: print shortest short integer value
Step 5: print longest short integer value
Step 6:print shortest long integer value
Step 7: print longest long integer value
Step 8:print minimum value of float
Step 9:print maximum value of float
Step 10:print minimum value of double
Step 11: print maximum value of double
Step 12: Stop.

Program:
#include <stdio.h>
#include<conio.h>
#include<limits.h>
#include<float.h>

Void main ()
{
Printf(“Shortest integer values is %d\n”,INT-MIN);
Printf(“longest integer value is %d\n”,INT-MIX);
Printf(“shortest short integer value is %d\n”,SHORT-MIN);
Printf(“longest short integer value is %d\n”,SHORT-MAX);
Printf(“shortest long integer value is %d\n”,LONG-MIN);
Printf(“longest long integer value is %d\n”,LONG-MAX);
Printf(“minimum value of float is %e\n”,FLT-MIN);
Printf(“maximum value of float is %e\n”,FLT-MAX);
Printf(“minimum value of double is %e\n”,DBL-MIN);
Printf(“maximum value of double is %e\n”,DBL-MAX);
getch();
}

Output:
Shortest integer values is -32768
longest integer value is -32768
shortest short integer value is-32768
longest short integer value is-32768
shortest long integer value is-2147483648
longest long integer value is -2147483647
minimum value of float is 1.17549e-38
maximum value of float is 3.4028e+38
minimum value of double is 2.2507e.308
maximum value of double is1.797679e+308
26
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Flowchart: College

27
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Program-5
Aim: Write a program which generates 100 random real numbers in the range 10.0to
20.0 and sort them in descending order.
Algorithm:
Step 1: Start
Step 2: Declare the variables i,j for running loop ,a[100]and temp used as a
temporary variable
Step 3: For generating the random values numbers enter into the loop for(i=0;i<100;i+
+)
Step 4: If the condition i<100 is true enters enters into loop10.0+random(100)/10.0 the
result is saved in a[i]
Step 5: display result a[i]
Step 6: for (i=0;i<100;i++){for(j=0;j<100;j++) if the both the conditions are true check
the condition a[i]<a[j+1]
Step 7: If that condition is true enters into loop then
Temp=a[i];a[j]=a[j+1]=temp:sort in of random numbers takes place
Step 8: Display the result of sorted random list
Step 9: stop

Program:
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
Void main()
{
Int i,j;
Float a[100].temp;
Clrscr();
Printf(“ the generated 100 random real numbers is \n”);
For(i=0;i<100;i++)
{
a[i]=10.0+random(100)/10.0;
printf(“%2f\t,a[i];
}
Printf(“the sorted list of the real numbers is \n”);
For(“i=0;i<100;i++)
{
For(j=0;j<10;j++)
{
For(a[j]<a[j+1])
{
Temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
Printf(“%2f\t”,a[j]);
}
}

28
getch();

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
College

Output:
The generated 100 random real numbers

14.60 13.00 18.20 19.00 15.60 11.70 19.50 11.50 14.80 12.60
10.40 15.80 17.10 17.90 19.20 16.00 12.10 12.16 6.30 14.70
11.90 14.10 19.00 18.60 11.40 19.90 17.10 17.16 17.90 19.20
18.90 15.10 19.00 19.30 13.40 11.00 19.50 17.90 16.10 15.40
12.70 15.10 16.00 16.30 19.30 16.40 16.40 13.60 17.40 13.30
18.90 16.00 19.50 16.10 17.10 19.50 18.00 15.20 10.00 19.00
15.30 15.80 16.60 16.00 19.50 14.80 19.50 18.20 19.30 16.30
19.50 15.90 16.10 19.10 19.50 16.10 15.60 18.10 17.30 19.00
10.50 15.90 16.10 19.50 19.10 16.10 19.10 19.50 19.00 10.00

The sorted list of the real numbers is:

14.60 13.00 18.20 19.00 15.60 13.00 19.50 11.70 14.80 12.60
15.20 19.00 19.50 15.60 14.60 14.80 12.60 11.70 11.50 11.50
19.50 18.20 19.00 15.60 14.60 14.80 13.00 12.60 11.70 11.50
19.50 19.00 18.20 19.50 15.60 14.60 14.80 13.00 12.60 11.50
19.50 19.00 19.00 15.60 14.60 14.80 13.00 12.60 11.70 11.50
19.50 19.50 19.50 15.60 14.60 14.80 13.00 12.60 11.70 11.50
19.50 19.50 19.50 15.60 14.60 14.80 13.00 12.60 11.70 11.50
19.50 19.50 19.50 15.60 14.60 14.80 13.00 12.60 11.70 11.50
19.50 19.50 19.00 15.60 14.60 14.80 13.00 12.60 11.70 11.50

29
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Flowchart:

30
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

31
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Program-6
Aim: Write a program to display the transpose of a matrix.

Algorithm:

Step 1: Start
Step 2: Declare a[3][3], b[3][3], i, j.
Step 3: Now for (i = 0; i < 3; i++) if the condition is true, enter into for (j = 0; j < 3; j++) if
this condition is also true, read a[i][j].
Step 4: Now again for (i = 0; i < 3; i++) if the condition is true, enter into
for (j = 0; j < 3; j++) if the condition is true, display a[i][j].
Step 5: for (j = 0; j < 3; j++) while the condition is true, enter into the loop b[j][i] = a[i][j].
Step 6: for (i = 0; i < 3; i++) if the condition is true, enter into for (j = 0; j < 3; j++) display
b[i][j].
Step 7: Stop.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], i, j;
clrscr();
printf("Enter A matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nDisplay A matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n");
}

printf("\nTranspose of a matrix:\n");

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


{
for(j=0; j<3; j++)

32
{

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
b[i][j]=a[j][i]; College
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
printf("\n");
getch();
}

Output:
Enter A matrix: 123456789
Display a Matrix:
123
456
789
Transpose of a matrix:
147
258
369

33
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Flowchart:

false

True

True

True

j<3 False
i++

b[i][j] = a[j][i]

j++

34
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-7
Aim: First use an Editor to create a file with some integer numbers. Now write a
program, which reads the numbers and determines the mean and standard deviation.

Algorithm:
Step 1: Start
Step 2: Declare the variables n, a[20], mean, std, and assign sum=0, count=0
Step 3: Open the file fp and give the permissions to write by fp=fopen("num.txt", "w")
Step 4: Read the numbers from the file until end of the file n=fscanf(fp, "%d", &a[i])
Step 5: Display the result of file: and close the file
Step 6: Open another file and give permission to read
Step 7: Read n using while loop assign sum=a[i], i++, mean=sum/n
Step 8: Display mean
Step 9: Assign sum=0 for count=0; count<n; count++
Step 10: Assign sum=pow(a[count]-mean, 2);
Step 11: Close the file fp.

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

void main()
{
float a[20], n, mean, sd, sum;
FILE *fp;
fp = fopen("num.txt", "w");
printf("Enter the numbers:\n");
while (scanf("%f", &n) != EOF)
{
fprintf(fp, "%f\n", n);
}
fclose(fp);
fp = fopen("number", "r");
while (fscanf(fp, "%f", &a[i]) != EOF)
{
i++;
}
sum = 0;
for (j = 0; j < i; j++)
{
sum += a[j];
}
mean = sum / i;
printf("The mean is %f\n", mean);
sum = 0;
for (count = 0; count < i; count++)
{
sum += pow((a[count] - mean), 2);
}

35
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
sd = sqrt(sum / i); College

printf("The standard deviation is %f\n", sd);

fclose(fp);
getch();
}

Output:
Enter the number
Press ctrl+7 to step
12
13
14
15
16
The mean is 3.000000

36
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flowchart:

sum+=pow(a(count)m
ean,2)

fclose(fp)

37
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Stop
Program-8

Aim: Given two points on the surface of the sphere, write a program to determine the
smallest arc length between them.

Algorithm:

Step1: Start
Step2: Declare the variables x, y, z, x1, y1, z1, x2, y2, z2, r, d.
Step3: Read the variables x, y, z, x1, y1, z1, x2, y2, z2, r.
Step4: d1 = sqrt(pow(x - x1, 2) + pow(y - y1, 2) + pow(z - z1, 2))
Step5: d2 = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2))
Step6: Now A = 2 * asin(d / (2 * r)); storing result in A
Step7: Now = r * A store the result in s
Step8: Display s
Step9: Stop.

Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
int x, y, z, x1, y1, z1, x2, y2, z2;
double r, d, A, s;
clrscr();
printf("Enter x, y, z co-ordinates\n");
scanf("%d %d %d", &x, &y, &z);
printf("Enter x1, y1, z1 co-ordinates\n");
scanf("%d %d %d", &x1, &y1, &z1);
printf("Enter x2, y2, z2 co-ordinates\n");
scanf("%d %d %d", &x2, &y2, &z2);
d1 = sqrt(pow(x - x1, 2) + pow(y - y1, 2) + pow(z - z1, 2));
d = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2));
A = 2 * asin(d / (2 * r));
s = r * A;
printf("The smallest arc length between the given sphere is %f", s);
getch();
}

Output:
Enter x, y, z co-ordinates
1
2
3
Enter x1, y1, z1 co-ordinates
4
5

38
6

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
College

Enter x2, y2, z2 co-ordinates


7
8
9

The smallest arc length between the given sphere is


5.449398

Flowchart:

39
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-9

Aim: Implement bisection method to find the square root of a given number to a given
accuracy.

Algorithm

Step 1: Start of the program to compute the real root of Equation


Step 2: Input the value of x0, and x2
Step 3: Check f(x0) * f(x2) <0. If no, print "error" and exit.
Step 4: If yes compute, x0 = x1 + x2 / 2
Step 5: Compute f(x0)
Step 6: Again, if f(x0) * f(x2) < 0
Step 7: Set x1 = x0
Step 8: Else, set x2 = x0
Step 9: Continue the process (Step 5 to Step 9) till to get required accuracy
Step 10: Print output
Step 11: End of the program.

Program:
#include <math.h>
float f(float x)
{
return x*x*x-x-1;
}

void main()
{
int i, j, n;
float f0, f1, x0, x1, x2, f2;
clrscr();
printf("Enter x0, x1, n");
scanf("%f %f %d", &x0, &x1, &n);
f0 = f(x0);
f1 = f(x1);
if (f0 * f1 > 0)
{
printf("Not possible");
exit(0);
}
printf("%f\t%f\t%f\n", x0, x1, f(x0));
while (fabs(x1 - x0) > eps && i < n)
{
x2 = (x0 + x1) / 2;
f2 = f(x2);
printf("%f\t%f\t%f\n", x2, f2, fabs(x2 - x0));
if (f0 * f2 < 0)
{
x0 = x2;
}

40
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
else
{
x1 = x2;
j++;
}
})
printf("\n converging solution is x2 =%f in f0(x2)=%f\", x2,f2);
getch();
}

Output:
Enter x0, x1, n
1 .2
0.001
15

x0 x1 x2 f(x2)
1.000000 2.000000 1.500000 0.875000
1.000000 1.500000 1.250000 -0.296875
1.250000 1.500000 1.375000 0.214609
1.250000 1.375000 1.312500 -0.082611
1.312500 1.375000 1.343750 0.018711
1.312500 1.343750 1.328125 -0.006209
1.328125 1.343750 1.335938 0.006250
1.328125 1.335938 1.332031 -0.006209
1.332031 1.335938 1.333984 0.000021
1.332031 1.333984 1.333008 -0.006209
1.333008 1.333984 1.333496 -0.002037

Converging solution is x2=1.333496 in f(x2)=0.002037

41
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flowchart:

42
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-10

Aim: Implement Newton Raphson method to find the root of polynomial Equation.

Algorithm:

Step 1: Start
Step 2: Input the value of x0, n and e
Step 3: For i=0 and repeat if i<=n
Step 4: f=f(x0)
Step 5: df=df(x0)
Step 6: compute x=x0 - (f/df)
Step 7: If |x1-x0| <= e
Step 8: print "convergent"
Step 9: print x, f(x)
Step 10: End of the program
Step 11: Else, set x0=x1
Step 12: Repeat the process until to get required accuracy
Step 13: End the program.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <process.h>
void main()
{
float x0 = 0, x1 = 0, a = 0, b = 0, c = 0, f0 = 0, x = 0, d; Step = 0, acc;
int i = 0, n = 0;
clrscr();
printf("Enter the value of x0 = ");
scanf("%f", &x0);
printf("Enter the accuracy allowed in the root of equation = ");
scanf("%f", &acc);
printf("Enter the coefficients of equation:");
printf("Enter the value of a = ");
scanf("%f", &a);
printf("Enter the value of b = ");
scanf("%f", &b);
printf("Enter the value of c = ");
scanf("%f", &c);
printf("Enter the number of iterations = ");
scanf("%d", &n);

scanf("%f", &x);
for (i = 0; i < n; i++)
{
f0 = a*x*x + b*x + c;
df0 = 2*a*x + b;

43
if (fabs(df0) <= slope)

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
{ College
printf("In Slope too small, so will become divergent from solution");
exit(0);
}
x1 = x - (f0 / df0);
if (fabs(x1 - x0) < e)
{
printf("In Root of the equation is %f", x1);
printf("In No.of iterations for calculating the root of the equation is %d", i);
getch();
exit(0);
}
else
{
x0 = x1;
}
}

Output:

Enter the interval


Enter the value of x0, x1, n
Enter the Error allowed in the root of equation (0.001)
Enter the coefficients of the equation
Enter the value of a=1
Enter the value of b=1
Enter the value of c=2

Enter the number of iterations=10


Root of the equation is -4.561553
No. of iterations for calculating the root of the equation
is 3.

44
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flowchart:

45
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-11

Aim: Given a table of x and corresponding f(x) values, write a program which will
determine f(x) value at intermediate x value using Lagrange's interpolation.

Algorithm:

Step1: Start
Step2: Input the number of terms n
Step3: Input the array ax as array
Step4: for(i=0; i<n; i++)
Step5: nr=1
Step6: dr=1
Step7: for(j=0; j<n; j++)
Step8: if(i!=j)
a) nr=nr*(ax(i)-ax(j))
b) dr=dr*(ax(x)-ax(j))
Step9: cud loop
Step10: y = (nr/dr + ay(i))
Step11: Enter loop
Step12: print output x, y
Step13: End of the program.

Program:
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, j, k;
float x[10], y[10], x1, y1;
clrscr();
printf("Enter n:");
scanf("%d", &n);
printf("Enter data\n");
for (i = 0; i < n; i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
printf("y[%d] = ", i);
scanf("%f", &y[i]);
}

printf("Enter interpolated value");


scanf("%f", &x1);
sum = 0;
for (k = 0; k < n; k++)
{
term = 1;
for (j = 0; j < n; j++)
{
if (k != j)
46
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
{ College
Printf(“x=%dx[i]=%dx[k]=%d\n”,x,x[j],x[k];
t=x-x[j] ;
t=t/(x[k].x[j]
printf(“term=%f\n”,t);
term=termk*t;
}
}
termk = term * y[k];
printf(“%f\n\n”,term k);
sum = sum + termk;
}
printf("Interpolated value at x=%f is %f", x1, sum);
getch();
}

Output:
Enter no. of x values 3
Enter x values 7 11 15
Enter f(x) values 150 392 1452
Enter interpolated value 6

x[0]=7 x[1]=11 x[2]=15


term: NAN
G2 500000
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
G2 500000
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
G2 500000
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
G2 500000
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
G2 500000
x-6 x[0]=1 x[1]=5 x[2]=9
term: NAN
G2 500000
Interpolated value is 247.000000
x27

47
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Flowchart:

48
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Program-12

Aim: Write a program to display inverse of a matrix.

Algorithm:

Step 1: Start
Step 2: Declare the variable a[3][3], i, j, determinant = 0
Step 3: For i = 0 to i < 3; i++
For j = 0 to j < 3; j++
Read a[i][j]
Step 4: For i = 0 to i < 3; i++
For j = 0 to j < 3; j++
Display a[i][j]
Step 5: For i = 0 to i < 3; i++
determinant = determinant + a[0][i] * (a[1][(i + 1) % 3] * a[2][(i + 2) % 3] - a[1][(i + 2)
% 3] * a[2][(i + 1) % 3])
Step 6: For i = 0 to i < 3; i++
For j = 0 to j < 3; j++
Display (a[(i + 1) % 3] * a[(j + 2) % 3] - a[(i + 2) % 3] * a[(j + 1) % 3]) / determinant
Step 7: Stop

Program:

#include <stdio.h>
#include <conio.h>
int main () {
int a[3][3], i, j;
float determ=0;
clrscr();
printf ("Enter of Elements of the matrix: ");
for (i=0; i<3; i++) {
printf("\n");
for (j=0; j<3; j++) {
scanf ("%d", &a[i][j]);
}
}
for (i=0; i<3; i++) {
determ=determ+ (a[0][i] * (a[1][(i+1)%3] * a[2][(i+2)%3] - a[1][(i+2)%3] * a[2]
[(i+1)%3]));
}
printf("\n");
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
printf ("%d", (a[(i+1)%3] * a[(j+2)%3] - a[(i+2)%3] * a[(j+1)%3]) / determ);
printf(“\n”);
}
getch();

49
}

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
College

output:
Enter the 9 elements of the matrix:
111
222
333
The matrix is
111
222
333
Inverse matrix is
0.00 0.00 0.00
0.00 0.00 0.00
0.00 0.00 0.00

50
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Flowchart:

51
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Program-13

Aim: Implement Simpson's 3/8 rule in Numerical Integration

Algorithm:

Step 1: Start
Step 2: Input the upper and lower limits a and b
Step 3: Obtain the number of subintervals by n=b-a/h
Step 4: Input the number of subintervals
Step 5: Sum = 0
Step 6: Sum = func(a) + 4x func(a+b) + func(b)
Step 7: For i = 1 to 3; i = i+3
Step 8: Sum = Sum + 2x func(a+(i-1)h) + 4x func(a+ih)
Step 9: End loop
Step 10: Result = Sum*h/3
Step 11: Print output
Step 12: End of the program to and start of section function.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
float f(float x) {
return (sqrt(1 - x*x));
}
void main() {
float a, b, n, sum = 0, step, integral;
int i;
printf("Enter a, b, n: ");
scanf("%f%f%f", &a, &b, &n);
step = (b - a) / n;
sum = f(a);
for (i = 1; i <= n - 1; i++) {
x = a + i * step;
if (i % 2 == 0) {
sum = sum + 2 * f(x);
} else {
sum = sum + 4 * f(x);
}
}
sum = sum + f(b);
integral = (step / 3) * sum;
printf("Integral = %f", integral);
}

Output:
Enter the limits and n value 0 66
Integral value 4.800000

52
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Flowchart:

53
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Program-14
Aim: Write a program to solve a set of linear algebraic equations

Algorithm:

Step 1: Start
Step 2: Declare the variables n, k, i, j, a[20][20], b[20], x[20], p
Step 3: Read the variables n
Step 4: For i = 0 to i < n; i++
For j = 0 to j < n+1; j++
Read a[i][j]
If the second loop fails
Read b[i]
Step 5: For k = 0 to k < n-1; k++
For i = k+1 to i < n; i++
p = a[i][k] / a[k][k]
For j = k to j < n+1; j++
a[i][j] = a[i][j] - p * a[k][j]
Step 6: If the loop fails x[n-1] = a[n-1][n] / a[n-1][n-1]
Step 7: For i = n-2 to i >= 0; i--
x[i] = a[i][n]
For j = i+1 to j < n; j++
x[i] = x[i] - a[i][j] * x[j]
x[i] = x[i] / a[i][i]
Step 8: For i = 0 to i < n; i++
Display x[i]
Step 9: Stop

Program:
#include <stdio.h>
#include <conio.h>
void main() {
int i, j, k, n;
float a[20][20], x[20];
double s, p;
clrscr();
printf("Enter the number of equation: ");
scanf("%d", &n);
printf("Enter the co-efficients of the equations:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("a[%d][%d] = ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
printf("b[%d] = ", i + 1);
scanf("%f", &a[i][n]);
}
for (k = 0; k < n - 1; k++) {

54
for (i = k + 1; i < n; i++) {

Department of Computer Science and P.Gayathri


Engineering
Sanketika Vidya Parishad Engineering
p = a[i][k] / a[k][k]; College

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


a[i][j] = a[i][j] - p * a[k][j];
}
}
}
x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
for (i = n - 2; i >= 0; i--) {
s = 0;
for (j = i + 1; j < n; j++) {
s = s + a[i][j] * x[j];
}
x[i] = (a[i][n] - s) / a[i][i];
}
printf("Solution:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %f\n", i + 1, x[i]);
}
}

output:
Enter the number of Equation :3
Enter the co-efficients of the Equations:
[1][1] 1
[1][2] 2
[1][3] 3

[2][1] -5
[2][2] 2
[2][3] 4

[3][1] 3
[3][2] -3
[3][3] 2

the result is
1=-1.00
2=-1.00
3=1.00

55
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

56
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-15

Aim: Write a C program to calculate matrix


addition Algorithm:
Step 1: Start
Step 2: Declare matrix a[3] [3];
matrix b[3] [3];
matrix c[3] [3]:
Step 3: Read i,j of a[3][3] and
b[3] [3] Step 4: Declare variable
i=0;j=0
Step 5: Repeat until i<3
(a) Repeat until j<3
C[i] [j]=a[i] [j]+b[i]
[j] Step 6: C is the required
matrix
Step 7: Stop
Program:
#include
<stdio.h> int
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter 1st matrix
elements: "); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter 2nd matrix
elements: "); for(i=0;i<3;i++)
{

57
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
for(j=0;j<3;j++) College
{
scanf("%d",&b[i][j]);
}

58
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The resultant matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",c[i][j]);
} }
printf("\n");
}

Enter 1st matrix


Output:

elements 1
2
3
4
5
6
7
8
9
Enter 2nd matrix
elements 9
8
7
6
5
4
3
2
1
The resultant matrix 10 10 10 10 10 10 10 10 10

59
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow Chart:

60
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-16
Aim: Write a C program to calculate matrix
multiplication Algorithm:
Step 1: Start
Step 2: Declare variable and initialize necessary
variables Step 3: Enter the element of matrices by
row wise using loops
Step 4: check the number of rows and column of first and
second matrices Step 5: multiply the matrices using nested
loops
Program: Step 6: print the production matrix form as console

output Step 7: Stop

#include
<stdio.h> int
main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,k;
printf("Enter the 1st matrix
elements"); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the 2nd matrix
elements"); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}

61
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
} College
for(i=0;i<3;i++)
{

62
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[j][k];
}
}
}
printf("\n Resultant matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the 1st matrix
elements 2
2
2
2
2
2
2
2
2
Enter the 2nd matrix
elements3 3
3
3
3
3
3
3
3
Resultant matrix
is 18 18 18
63
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
18 18 18 College

64
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
18 18 18
Output:
Enter the 1st matrix
elements11 22
33
44
55
66
77.
88
99
Enter the 2nd matrix
elements1 2
3
4
5
6
7
8
9

Resultant matrix is
154 352 550
352 847 1342
550 1342 2134

65
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

66
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-17
Aim: Write a C to calculate the factorial of number using
recursion Algorithm:
Step 1:-Start
Step 2:-Input
n
Step 3:-Call
function (n) Step 4:-
Print output Step
5:-Stop
Fact(k)
Step1:
Start
Step 2: if(k=1) go to step 3 else go to
step 4 Step 3: return 1 and exit
Program:

Step 4: return k*fact(k-


1) Step 5: end

#includ e
<stdio.h> long
int fact(int k);
int main() {
int n;
printf("Enter a
number: ");
scanf("%d", &n);
printf("Factorial = %ld\n",
fact(n)); return 0;
}
long int fact(int
k) { if (k == 0)
return 1;
return k * fact(k - 1);
}
67
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Output: College
Case 1:
Enter a number:
4 Factorial = 24

Flowchart:

68
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-18
Aim: Write a C program to store information structure and
display it. Algorithm:
Step 1: Start
Step 2: Declare student, structure with numbers name,
roll no, marks Step 3: Declare an array of type structure
and variable I of type int Step 4: Start loop with i=0 and
repeat until i<5
Step 5: s[i].roll=i+1;
Step 6: Read name, roll no, marks of student
Step 7: Start loop with i=0 and repeat it
until i<5 Step 8: Print name, roll no, marks
Program: of the student Step 9: Stop

#include<stdio.h
> struct student
{
char
firstname[50];
int roll;
float marks;
};
int main() {
int i, n;
printf("Enter the number of
students: "); scanf("%d", &n);
struct student s[n];
printf("Enter information of
students:\n"); for (i = 0; i < n; i++) {
s[i].roll = i + 1;
printf("\nFor roll number %d:\n",
s[i].roll); printf("Enter first name: ");
scanf("%s",
s[i].firstname);

69
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
printf("Enter marks: College
"); scanf("%f",
&s[i].marks);
}
printf("\nDisplaying information:\n");

70
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
for (i = 0; i < n; i++) {
printf("\nRoll number: %d\n",
s[i].roll); printf("First name: %s\n",
s[i].firstname); printf("Marks: %.2f\n",
s[i].marks);
}
return 0;
}
Output:
Enter the number of
students: 2 Enter
information of students:

For roll number 1:


Enter first name:
Ramu Enter marks:
80

For roll number 2:


Enter first name:
Raju Enter marks:
75

Displaying information:

Roll number: 1
First name:
Ramu Marks:
80.00

Roll number: 2
First name:
Raju Marks:
75.00
71
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Flow chart:

72
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-19
Aim: Write a C program to write data to text file and read
it. Algorithm:
Step 1: Start
Step 2: Create a file pointer of type file
Step 3: Open the text file in write mode and store
it in fp Step 4: if(fp==null)then file does not exist
Step 5: Otherwise, the character entered from the key board
will be printed in the text file until the character(dot) is
entered.
Step 6: Close the opened file
Step 7: Now open the file in read mode
Step 8: Read the characters from the file until end of
Program: the s file(eof) Step 9: Stop

#include
<stdio.h> int
main() {
FILE *fp;
char data[100];
fp = fopen("data.txt",
"w"); if (fp == NULL) {
printf("Error opening file for
writing.\n"); return 1;
}
printf("Enter some text to write to the
file: "); fgets(data, sizeof(data), stdin);
fprintf(fp, "%s", data);
fclose(fp);
fp = fopen("data.txt",
"r"); if (fp == NULL) {
printf("Error opening file for
reading.\n"); return 1;
}

73
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
printf("Data read from the file:\n");
while (fgets(data, sizeof(data), fp) != NULL) {

74
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
printf("%s", data);
}
fclose(fp);
return 0;
}

75
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Output:
Case 1: No file
Error opening file for
writing. Case 2: ardino
Data read from the
file: Hello, world!
Flow chart:

76
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Program-20
Aim: Write a C program to calculate find the biggest
number in an array Algorithm:
Step 1: start
Step 2: declare array a[n] and variable i, big
Step 3: read n from user and read all the
elements of a[ ] Step 4: initialize variable i=1
and big=a[0]
Step 5: repeat until i< = n-1
(i) If ( a[i] > big),set big=a[0]
(ii) Increment i
Step 6: print “the biggest element
is” big Step 7: stop

Program:

#include
<stdio.h> int
main () {
int i, n, big, a[10];
printf("Enter number of values: ");
scanf("%d", &n);
printf("Enter values:
"); for (i = 0; i < n;
i++) { scanf("%d",
&a[i]);
}
big = a[0];
for (i = 1; i < n; i+
+) { if (a[i] > big) {
big = a[i];
}
}
printf("big = %d\n",
big); return 0;
77
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
} College

Output:
Case 1:
Enter number of values: 5
values: 20 40 30 14 50
big = 50
Case 2:
Enter number of values: 7
Enter values: 300 345 987 456 98 45 1
big = 987

Flowchart:

78
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

Viva Questions
Prepare five viva questions for this. Write a program to read x, y
coordinates of 3 points and then calculate the area of a triangle
formed by them and print the coordinates of the three points
and the area of the triangle. What will be the output from your
program if the three given points are in a straight line?

1. Question: What is the formula used to calculate the area of


the triangle formed by three points?

Answer: The area is calculated using the formula: \( \text{Area} = \


frac{1}{2} \times |x_1(y_2 - y_3) + x_2(y_3 - y_1) + x_3(y_1 - y_2)| \),
where \( (x_1, y_1), (x_2, y_2), (x_3, y_3) \) are the coordinates of the
three points.

2. Question: How do you handle input for


the three points in your program?

Answer: The program reads the x, y


coordinates of three points from the user
through standard input.

3. Question: What will happen if the


three points provided are collinear (lie on
a straight line)?

Answer: If the points are collinear, the


area of the triangle will be zero since the
determinant of the area formula will
result in zero.

4. Question: How does the program


79
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
display the result? College

Answer: The program prints the


coordinates of the three points along with
the computed area of the triangle.

5. Question : Can this program handle


negative coordinates as input?

Answer: Yes, the program can handle


negative coordinates as input since the
area formula works with all integer and
floating-point values.

Write a program, which generates 100


random integers in the range of 1 to 100.
Store them in an array and then print
the arrays. Write 3 versions of the
program using different loop constructs.
(e.g. for, while, and do while).

Here are five very short viva questions


with answers:

1. Question: What is the range of


random integers generated in the
program?

Answer: The random integers are


generated in the range of 1 to 100.

2. Question: What data structure is used


to store the random integers?

80
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
Answer: An array is used to storeCollege
the
random integers.

3. Question: How does the for loop


version of the program work?

Answer: The for loop runs 100 times,


generating and storing a random integer
in each iteration.

4. Question: What is the difference


between a while loop and a do-while
loop?

Answer: A while loop checks the


condition before executing, while a do-
while loop executes at least once before
checking the condition.

5. Question: Why do we use random


number generation in this program?

Answer: To create a set of random


values that can be stored in an array and
later printed for testing or other
purposes.

Write a set of string manipulation


functions e.g. for getting a sub-string
from a given position, copying one string
to another, reversing a string, adding one
81
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
string to another. College

Here are five very short viva questions


and answers based on the program you
described:

1. Question: What is the purpose of the


program?

Answer: The program determines the


largest and smallest values for different
data types like short, int, long, float, and
double.

2. Question: What happens when you


add 1 to Integer.MAX_VALUE?

Answer: It causes an overflow,


wrapping the value around to
Integer.MIN_VALUE.

3. Question: How do we get the


maximum and minimum values for each
data type?

Answer: We use predefined constants


like Short.MAX_VALUE,
Integer.MAX_VALUE, etc., in Java.

4. Question: What is the largest value an


82
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
int can hold? College

Answer: The largest value an int can


hold is 2,147,483,647.

5. Question: Why does an overflow occur


when adding 1 to the largest integer?

Answer: An overflow occurs because


the int data type has a fixed range, and
adding 1 exceeds this range.

Write a program which determines the


largest and the smallest number that can
be stored in different data types like
short, int, long, float, and double. What
happens when you add 1 to the largest
possible integer number that can be
stored?

1. Question: What is the purpose of this


program?

Answer: The program finds the largest


and smallest values that can be stored in
short, int, long, float, and double data
types.

2. Question: What happens if you add 1


to Integer.MAX_VALUE?

Answer: It causes an overflow, and the


result wraps around to
Integer.MIN_VALUE.
83
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

3. Question: How do you get the


minimum and maximum values of data
types in Java?

Answer: By using predefined constants


like Short.MAX_VALUE,
Integer.MAX_VALUE, etc.

4. Question: What is the largest value


that can be stored in a long?

Answer: The largest value for long is


9,223,372,036,854,775,807.
5. Question: Why does an integer
overflow happen when adding 1 to the
largest integer?

Answer: An overflow happens because


the value exceeds the maximum limit of
the int type, causing it to wrap around.

Write a program, which generates 100


random real numbers in the range of
10.0 to 20.0, and sort them in descending
order.

1. Q: What is the purpose of generating


random numbers in the program?

A: The purpose is to generate 100 real


numbers in the range of 10.0 to 20.0 for
sorting and processing.
84
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

2. Q: How do you generate random real


numbers in a specific range?

A: Using the random.uniform(a, b)


function in Python, which generates a
random float number between a and b.

3. Q: What method is used to sort the


generated numbers in descending order?

A: The sort(reverse=True) method is


used to sort the list in descending order.

4. Q: Why is the range 10.0 to 20.0


chosen for generating random numbers?

A: It is an arbitrary range chosen to


demonstrate the functionality of
generating and sorting real numbers.

5. Q: How would you modify the program


to generate a different number of random
numbers?

A: You can change the number 100 in


the loop or array size to generate a
different number of random numbers.

Write a function for transposing a square


matrix in place (in place means that you
are not allowed to have full temporary
matrix).

### Viva Questions and Answers:

85
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

1. Q: What does transposing a matrix


mean?

A: Transposing a matrix means


swapping its rows and columns, i.e.,
converting element at position (i, j) to
position (j, i).

2. Q: How do you transpose a square


matrix in place without using extra
space?

A: You can swap elements


symmetrically across the main diagonal
by using two nested loops: one for rows
and one for columns.

3. Q: Why is it called "in-place"


transposition?

A It is called in-place because the


transposition is done directly on the
matrix without using any additional
matrix to store the result.

4. Q: What is the time complexity of


transposing a matrix in place?

A: The time complexity is O(n^2),


where n is the number of rows or
columns in the square matrix.

5. Q: Can you transpose a non-square


86
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
matrix in place? College

A: No, in-place transposition can only


be performed on square matrices as they
have equal rows and columns.

First use an editor to create a file with


some integer numbers. Now write a
program, which reads these numbers and
determines their mean and standard
deviation.

### Viva Questions and Answers:

1. Q: How do you read integers from a


file in Python?

A: You can use the open() function to


read the file and then use read() or
readlines() to access the numbers.

2. Q: What is the formula for calculating


the mean?

A: The mean is calculated by summing


all the numbers and dividing by the count
of numbers:

\( \text{mean} = \frac{\sum \
text{numbers}}{n} \).

3. Q: How do you calculate the standard


deviation?

A: The standard deviation is calculated


by taking the square root of the variance,
where variance is the average of squared
87
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
differences from the mean. College

4. Q: Why is it necessary to open a file in


read mode ('r')?

A: Opening a file in read mode ensures


you can only read its contents and not
modify it.

5. Q: What is the time complexity of


reading and processing numbers to
calculate the mean and standard
deviation?

A: The time complexity is O(n), where


n is the number of integers in the file, as
we need to read and process each
number once.

Given two points on the surface of the


sphere, write a program to determine the
smallest arc length between them.

Viva Questions and Answers:

1. Q: How do you calculate the arc


length between two points on the surface
of a sphere?

A: The arc length is calculated using


the formula:

\( \text{arc length} = R \cdot \theta \),


where \( R \) is the radius of the sphere
and \( \theta \) is the central angle
between the points in radians.
88
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

2. Q: How do you find the central angle


between two points on a sphere?

A: The central angle \( \theta \) can be


calculated using the spherical law of
cosines:

\( \cos(\theta) = \sin(\phi_1) \cdot \


sin(\phi_2) + \cos(\phi_1) \cdot \cos(\
phi_2) \cdot \cos(\Delta \lambda) \),
where \( \phi_1 \) and \( \phi_2 \) are the
latitudes, and \( \Delta \lambda \) is the
difference in longitudes.

3. Q: What is the role of latitude and


longitude in this problem?

A: Latitude and longitude represent


the positions of the two points on the
sphere's surface, and are used to
compute the central angle between them.

4. Q: How do you convert the angle


from degrees to radians?

A: To convert an angle from degrees to


radians, multiply it by \( \frac{\pi}
{180} \).

5. Q: What is the time complexity of the


program that calculates the arc length
between two points?

A: The time complexity is O(1), as the


89
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
calculation involves a fixed number College
of
operations regardless of the size of input
data.

Implement bisection method to find the


square root of a given number to a given
accuracy.

Here are five viva questions with brief


answers based on the Bisection Method
for finding the square root:

1. What is the Bisection Method?

- The Bisection Method is a numerical


technique to find the root of a continuous
function by repeatedly halving the
interval and selecting the subinterval
where the function changes sign.

2. How does the Bisection Method apply


to finding square roots?

A: - To find the square root of a number


\( N \), we solve the equation \( f(x) =
x^2 - N = 0 \). The square root is the
point where this function crosses zero.

3. What is the stopping criterion for the


Bisection Method?

A: - The method stops when the


difference between the upper and lower
bounds of the interval is less than a
specified tolerance (accuracy), or the
90
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
function value at the midpoint is College
sufficiently close to zero.

4. What are the limitations of the


Bisection Method?

A: - The method requires that the


function is continuous and the initial
interval contains a sign change. It can be
slow and less efficient compared to other
methods for higher accuracy.

5. Why is the Bisection Method reliable


for square root calculation?

A: - The method is reliable because it


systematically reduces the interval size,
ensuring convergence to the correct root
within the given tolerance.

Implement Newton Raphson method to


det. a root of polynomial equation

Here are five viva questions with brief


answers based on the Newton-Raphson
Method for finding the root of a
polynomial equation:

1. What is the Newton-Raphson method?

A: - The Newton-Raphson method is an


iterative numerical technique used to
find successively better approximations
of the root of a real-valued function.

91
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
2. How is the Newton-Raphson method
applied to find the root of a polynomial?

A: - To find the root of a polynomial \


( f(x) = 0 \), the method uses the
formula:

\[

x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}

\]

where \( f'(x_n) \) is the derivative of


the polynomial at \( x_n \).

3. What is the stopping criterion in the


Newton-Raphson method?

A: - The iteration stops when the


difference between successive
approximations, \( |x_{n+1} - x_n| \), is
smaller than a specified tolerance or
when \( f(x_n) \) is close enough to zero.

4. What are the main advantages of the


Newton-Raphson method?

A: - The Newton-Raphson method is fast


and has quadratic convergence near the
root, meaning it requires fewer iterations
compared to methods like bisection.

5. What are the possible drawbacks of


the Newton-Raphson method?*

A: - It may fail if the initial guess is too

92
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
far from the actual root or if the College
derivative \( f'(x) \) is zero at some point,
leading to division by zero.

Given table of x and corresponding f(x)


values,

Write a program which will determine


f(x) value at an intermediate x value by
using Lagrange’s interpolation/

Here are five viva questions with brief


answers based on Lagrange's
Interpolation for determining \( f(x) \) at
an intermediate value:

1. What is Lagrange’s Interpolation?

A: - Lagrange's Interpolation is a
method for estimating the value of a
function at an intermediate point using a
set of known values. It constructs a
polynomial that passes through all the
given data points.

2. How is Lagrange’s Interpolation


formula expressed?

A: - The Lagrange interpolation formula


for estimating \( f(x) \) at a point \( x \)
is:

\[

f(x) = \sum_{i=0}^{n} f(x_i) \cdot L_i(


\]

where \( L_i(x) \) is the Lagrange


93
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
basis polynomial given by: College

\[

L_i(x) = \prod_{\substack{j=0 \\ j \neq


i}}^{n} \frac{x - x_j}{x_i - x_j}

\]

3. What is the main advantage of


Lagrange’s Interpolation?

A: - The advantage is that it provides an


exact polynomial that passes through all
given data points, making it highly
accurate for interpolation when the data
points are sparse.

4. What is the role of \( L_i(x) \) in the


interpolation formula?

A: - \( L_i(x) \) is a basis polynomial that


equals 1 at \( x = x_i \) and 0 at other
data points. It ensures the function
interpolates the data points correctly.

5. What are the limitations of Lagrange’s


Interpolation?

A: - Lagrange interpolation can be


computationally expensive for large
datasets and may lead to oscillations
(Runge's phenomenon) when applied to
equally spaced data points over large
intervals.

Write a function which will invert a


94
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
matrix. College

Here are five viva questions with brief


answers for inverting a matrix:

1. What is matrix inversion?

A: - Matrix inversion is the process of


finding a matrix \( A^{-1} \) such that
when multiplied by the original matrix \
( A \), the result is the identity matrix \( I
\), i.e., \( A \cdot A^{-1} = I \).

2. What is the condition for a matrix to be


invertible?

A: - A matrix is invertible (or non-


singular) if its determinant is non-zero (\(
\text{det}(A) \neq 0 \)).

3. How can you invert a matrix using


Python?

A: - In Python, you can use the NumPy


library to invert a matrix using the
numpy.linalg.inv() function, which
returns the inverse of a square matrix if
it is invertible.

4. What is the method to invert a matrix


manually?

A: - A common method is to use the


adjoint method or Gaussian elimination.
The matrix is augmented with the
95
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
identity matrix, and row operations College
are
used to convert the original matrix to the
identity, resulting in its inverse.

5. What happens if you try to invert a


singular matrix?

A: - If a matrix is singular (i.e., its


determinant is zero), it does not have an
inverse, and attempting to invert it will
result in an error or undefined behavior.

Implement Simpson’s rule for numerical


integration.

Here are five viva questions with brief


answers based on implementing
Simpson’s Rule for numerical integration:

1. What is Simpson's Rule?

A: - Simpson’s Rule is a method for


numerical integration that approximates
the integral of a function using parabolic
segments. It is particularly accurate for
smooth functions.

2. How is Simpson’s Rule formula


expressed?

A: - The formula for Simpson’s Rule is:

\[

\int_a^b f(x) \, dx \approx \frac{b - a}


{6} \left[ f(a) + 4f\left(\frac{a + b}{2}\
right) + f(b) \right]
96
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
\] College

This is for a single interval. For


multiple intervals, the formula is applied
to each segment and the results are
summed.

3. What is the advantage of using


Simpson's Rule?

A: - Simpson’s Rule is more accurate


than the Trapezoidal Rule because it
approximates the function with a second-
degree polynomial, providing better
results for smooth functions.

4. When is Simpson’s Rule most


effective?

A: - Simpson's Rule is most effective for


smooth functions where the second
derivative is continuous and doesn't
change abruptly.

5. What is the error in Simpson’s Rule?

A: - The error in Simpson’s Rule is


proportional to the fourth power of the
interval width, i.e., \( O(h^4) \), where \(
h \) is the width of the subintervals. This
means it decreases rapidly with smaller
intervals.

Write a program to solve a set of linear


algebraic equations.
97
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College
Here are five viva questions with brief
answers for solving a set of linear
algebraic equations:

1. What is the method to solve a set of


linear algebraic equations?

A: - A common method to solve a set of


linear algebraic equations is by using
matrix methods such as Gaussian
elimination, Cramer's rule, or by using
matrix inversion (if the matrix is
invertible).

2. How can a system of linear equations


be represented in matrix form?

A: - A system of linear equations \( A \


cdot X = B \) can be represented in
matrix form, where \( A \) is the
coefficient matrix, \( X \) is the column
matrix of variables, and \( B \) is the
column matrix of constants.

3. How can a set of linear equations be


solved using Python?

A: - In Python, a system of linear


equations can be solved using the
numpy.linalg.solve() function, which
solves \( A \cdot X = B \) efficiently.

4. What is the role of the determinant in


98
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
solving linear equations? College

A: - The determinant of the coefficient


matrix \( A \) determines if the system
has a unique solution. If the determinant
is zero, the system may have no solution
or infinitely many solutions.

5. What happens if the coefficient matrix


is singular?

- If the coefficient matrix is singular


(i.e., its determinant is zero), the system
of equations has no unique solution. It
may be inconsistent or have infinitely
many solutions.

99
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

100
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

101
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

102
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

103
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

104
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

105
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

106
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

107
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

108
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

109
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

110
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

111
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

112
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

113
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

114
Department of Computer Science and P.Gayathri
Engineering
Sanketika Vidya Parishad Engineering
College

115
Department of Computer Science and Dr. T. Sunil
Engineering Kumar
Sanketika Vidya Parishad Engineering
College

116
Department of Computer Science and Dr. T. Sunil
Engineering Kumar

You might also like