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

NACP Lab Manual

The document is a lab manual for Numerical Analysis and Computer Programming prepared for NIT-Agartala, containing a comprehensive index of programs covering various programming tasks and MS-DOS commands. It includes basic programs for arithmetic operations, data handling, matrix operations, file management, and numerical methods. The manual is authored by Goutam Acharjee under the guidance of faculty members from the Department of Mechanical Engineering for the academic year 2015-2016.

Uploaded by

udheepta999
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)
5 views

NACP Lab Manual

The document is a lab manual for Numerical Analysis and Computer Programming prepared for NIT-Agartala, containing a comprehensive index of programs covering various programming tasks and MS-DOS commands. It includes basic programs for arithmetic operations, data handling, matrix operations, file management, and numerical methods. The manual is authored by Goutam Acharjee under the guidance of faculty members from the Department of Mechanical Engineering for the academic year 2015-2016.

Uploaded by

udheepta999
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/ 96

Numerical Analysis and

Computer Programming

Lab Manual
Prepared according to the Syllabus
of NIT -Agartala
(With few basic programs)

Prepared by,
Goutam Acharjee
Under the guidance of
Dr. Ankuran Saha, Assistant Professor,
Dr. Madhujit Deb, Assistant Professor,
Department ofMechanical Engineering

2015-2016
UME04P17Numerical Analysis and Computer Programming

INDEX OF PROGRAMS
S.No. Programs Page No
1 MS - DOS Commands 4
2 Program to print Hello World 12
3 Program to perform the arithmetic operators on two numbers. 12
4 Program to find sum and average of 5 numbers. 13
5 Program to find the simple interest. 14
6 Program to evaluate the expression 15
i. (ax + by)/ (ax - by)
ii. a² + b² + √(2ab)
7 Program to check whether given number is even or odd. 16
8 Program to find largest of given three numbers. 17
9 Program to find roots of a quadratic equation. 18
10 Program to find grade of the student for his 5 subject marks out of 100. 20
11 Program to find area of the triangle when 22
i. Sides are given
ii. Base and Height are given
iii. Co-ordinates are given; using switch statement.
12 Program to check whether the given number is prime or not. 24
13 Program to find the reverse of a given number. 26
14 Program to check whether the given number is palindrome or not. 27
15 Program to print the pattern in following manner: 28
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

16 Program to print the pattern in following manner: 29


1
2 2
3 3 3
4 4 4 4
17 Program to compute exponential series: 30

18 Program to compute cosine series: 32

19 Program to print the prime numbers up to given range n. 34


20 Program to print the Fibonacci series until n. 36
21 Program to convert the numbers from 37
i. binary to decimal
ii. decimal to binary; using switch case statement.
22 Program to perform sorting operation of the given array (both 39
ascending and descending).

Dept. ofM.E., NITA Page 2


Numerical Analysis and Computer Programming

23 Program to display random integer numbers in the range 1-100 and 42


find the average of these by using
a) for loop b) while c) do-while
24 Program to generate 100 random real numbers in the range 10 to 20 43
and sort them.
25 Program which determines largest and smallest number that can be 45
stored in different data types.
26 Program to multiply two matrices. 46
27 Program to find the transpose of a matrix. 49
28 Program to find the trace and norm of a matrix. 50
29 Program to implement a set of string handling functions using switch 52
case.
30 Program to create a structure for a student with details (name, roll no, 55
5 subject marks, total marks, percentage) and sort the records
according to percentage.
31 Program to illustrate how to use pointer to structure. 58
32 Program to assign values to union members. 59
33 Program to display strings using array of pointers. 60
34 Program to transpose elements of a matrix using pointers. 61
35 Program to calculate the area of triangle when 62
a. base and height are given
b. sides of triangle are given using functions
36 Program to illustrate call by value and call by reference. 65
37 Program to obtain the values of Ǯncrǯ and Ǯnprǯ using functions. 66
38 Program to find GCD of two numbers using recursion. 68
39 Program to print the Fibonacci series using recursion. 70
40 Program to count the number of alphabets, integers and special 71
characters in a file.
41 Program to create a file with some integers and which reads the 73
numbers & determines the mean and standard deviation.
42 Program to print and display the contents of a file in reverse order. 75
43 Program to merge two files into third auxiliary file and display the 76
content.
44 Program to print every fifth character in a given file. 78
45 Program to convert uppercase letters into lowercase and vice versa for 79
contents in a file and display the result.
46 Program to implement Lagrange's interpolation. 81
47 Program to find the square of given x2-25 by false-position method. 83
48 Program to find the square of given x2-25=0 using bisection method. 86
49 Program to implement Newton's Rapson method. 88
50 Program to implement Secant method. 89
51 Program to get the integration of given function using Simpson's 1/3 91
rule.
52 Program to find the smallest arc length of a sphere. 93

Dept. of M.E., NITA Page 3


Numerical Analysis and Computer Programming

1. MS-DOS
MS-DOS
MS-DOS is an operating system for x86-based personal computers mostly developed by
Microsoft, was first developed as early as 1980 by Bill Gates at the age of 19. DOS is a single
user and single task operating system. It is a character user interface operated with keyboard
only.
It is a collection of programs & other files. It is designed to provide a method of organizing and
using the information stored on disks, application programs, system programs and the
computer itself.

Files and File names: A file is a collection of related information. The files should have suitable
names for their identification in later use.
Rules for naming the files: <FILENAME>. < EXTENSION>
1. File names should be of one to eight characters in length with an option of one to three
character extension
2. File names can include any one of the following characters:
A to Z (or a to z) 0 to 9, $, &, #, @. %, ( ), { }, _
3. The characters which are not allowed are : , ; + / \ * as these have special meaning.
4. (.) is used to separate first part of file name from extension. (letter.txt etc.,)
5. When a file name includes an extension, it should be referred along with its extension
and not only with the first part
Directories: It is a collection of files, size, date and time of creation of files. A directory may
contain directories also. The main directory of a drive is called Root Directory into which
several directories and sub-directories can exist.
DOS Commands
1. Command: VER
Description: It displays the version of operating system
Syntax: C:\> VER
Example Output:
Microsoft Windows [Version 5.1.7601]
2. Command: DATE
Description: It displays current date and asks for new date in (mm)-(dd)-(YY) format. If no date is
to be changed, the old date can be retained by pressing enter key.
Syntax: C:\> DATE
Example Output:
The current date is: 10/08/2012
Enter the new date: (mm-dd-yy)
3. Command: TIME
Description: It displays current time and asks for new tine and if no new time is to be entered,
pressing enter retains the old time.
Syntax: C:\> TIME
Example Output:
The current time is: 21:38:51.06
Enter the new time:
4. Command: TITLE

Dept. of M.E., NITA Page 4


Numerical Analysis and Computer Programming

Description: Sets the window title for the command prompt window.
Syntax: C:\> TITLE [string]
where "string" specifies the text to set the title.
Example Output:
TITLE CPNMLAB
CPNMLAB — x
C:\> TITLE CPNMLAB COMMAND PROMPT WINDOW

5. Command: CLS
Description: It clears the screen
Syntax: C:\> CLS
Example Output:
Screen gets cleared and displays C:\> at the top
6. Command: [DRIVE]:
Description: To change the drive letter in MS-DOS, type the drive letter followed by a colon.
Syntax: C:\> [drive]:
Example Output:
C:\> D: {changes the drive letter from C to D}
D:\>
7. Command: MD
Description: It is used to make a new directory (or sub-directory) which is subordinate to the
current (or root) directory.
Syntax: C:\> MD <directory_name>
Example Output:
C:\> MD IT_24
8. Command: CD
Description: It is used to change from one directory to the other.
Syntax: C:\> CD <directory_name>
Example Output:
C:\> CD IT_24
C:\IT_24>
9. Command: CD..
Description: Goes back one directory.
Syntax: C:\DIRECTORY_NAME>CD..
Example Output:
C:\IT_24>CD..
C:\>
10. Command: CD\
Description: Goes to the highest level, the root of the drive.
Syntax: C:\DIRECTORY_NAME\SUB_DIRECTORY>CD\
Example Output:
C:\Documents and Settings\User>CD\
C:\>
11. Command: COPY CON
Description: It allows the creation of a file through command prompt.

Dept. of M.E., NITA Page 5


Numerical Analysis and Computer Programming

Syntax: COPY CON <FILENAME>. < EXTENSION>


Type copy con followed by the name of the file. After this you'll be returned to a blank
line, which is the start of your file. Enter the lines you want to insert into the file and when
done press Ctrl + Z to create the file. If you wish to cancel the creation of the file press Ctrl
+ C.
Example Output:
D:\IT_24> COPY CON sample.txt
Name: Santosh
Branch: IT
Rank: 22000
College: ANITS
^Z [Ctrl + Z]
1 file(s) copied.
D:\IT_24>
12. Command: TYPE
Description: Displays the contents of a text file or files.
Syntax: TYPE [drive:\path\filename]
Example Output:
D:\IT_24> TYPE sample.txt
Name: Santosh
Branch: IT
Rank: 22000
College: ANITS
D:\IT_24>
13. Command: EDIT
Description: Edit allows a user to view, create, or modify their computer files.
Syntax: EDIT [drive:\path\filename]
To Save the file Press Alt + F, a menu appears that contain "Save" option.
To Exit the file Press Alt + F, a menu appears that contain "Exit" option.
14. Command: DIR
Description: displays continuously a list of files and sub-directories in a directory, displays total
number of files, directories, bytes used and remaining bytes (storage).
Syntax: D:\> DIR [drive:]
D:\> DIR F: {displays list of files and directories from F drive.}
Attributes:
• DIR /p: pauses the listing whenever the screen is full. Next screen loads when any key is
pressed.
• DIR /w: displays files of directory in 5 column formats. Only filenames and extensions will
be displayed.
• DIR /d: displays files in wide format and in sorted order.
• DIR /ON: Displays all the files in A to Z order.
• DIR /O-N: Displays all the files in Z to A order.
Wildcards: DIR can also be specified with wild card characters (such as *) to list files sharing a common
element in the filename or extension.
• D:\> DIR *.txt : list all files with extension .txt {text files} in drive D.
• D:\> DIR W*.*: list all files that start with W & other extensions in drive D.
15. Command: DEL
Description: Deletes a specified file

Dept. of M.E., NITA Page 6


Numerical Analysis and Computer Programming

Syntax: DEL {filename}


Example Output:
D:\IT_24> DEL sample.txt: Deletes file sample.txt from IT_24 directory
D:\IT_24> DEL *.txt: Deletes all files with extension .txt
D:\IT_24> DEL W *.* : Deletes all files that start with W & other extensions.
16. Command: REN
Description: Changes the name of old file with new name.
Syntax: REN old_file_name new_file_name
Example Output:
D:\IT_24> REN sample.txt Santosh24.txt
{Changes the file sample.txt to Santosh24.txt}
17. Command: COPY
Description: Copy files from one place to another.
Syntax: COPY [drive:\path]<source file> [drive:\path]<destination>
Example Output:
D:\IT_24> COPY Santosh24.txt santoshit.txt
{Copies the file Santosh24.txt with the name santoshit.txt on the same directory }
D:\IT_24> COPY santoshit.txt C:\
{Copies the file santoshit.txt with the same name into C directory }
C:\> COPY D:\IT_24\*.txt C:\
{Copies all files with extension .txt with the same names into C directory }
C:\> COPY D:\W*.* C:\
{Copies all files that start with W & other extensions with same names into C directory }
18. Command: COLOR
Description: Sets the default console foreground and background colors.
Syntax: COLOR [attr] where attr specifies color attribute of console output.
Color attributes are specified by TWO hex digits -- the first corresponds to the
background; the second the foreground. Each digit can be any of the following values:
0 = Black 6 = Yellow C = Light Red
1 = Blue 7 = White D = Light Purple
2 = Green 8 = Gray E = Light Yellow
3 = Aqua 9 = Light Blue F = Bright White
4 = Red A = Light Green
5 = Purple B = Light Aqua
Example Output:
C:\> COLOR 1F
{Changes Background Color to blue and foreground color to Bright White }
19. Command: RD
Description: Removes or deletes a directory with a condition that directory should be empty.
The directory can be removed from root directory/main directory.
Syntax: RD [option] [drive:] path
Options:
• RD /s directory_name: Removes all directories and files in the specified
directory
and the directory itself & asks for confirmation. Used to delete the directory
tree.
• RD /q directory_name: Quiet mode, do not ask if ok to remove a directory tree
with /S
Example Output:
D:\ANITS> RD IT

Dept. ofM.E., NITA Page 7


Numerical Analysis and Computer Programming

{removes IT directory if it is empty else displays Directory is not empty }


D:\ANITS> RD /s IT
IT, Are you sure (Y/N)? y
{removes IT directory even when not empty as it removes all directories and files}
D:\> RD /s /q ANITS
{removes ANITS directory without confirmation all the files. }
20. Command: COMP
Description: Compares contents of two files.
Syntax: COMP filename1 filename2
Example Output:
D:\IT_24> COMP Santosh24.txt santoshit.txt
Comparing Santosh24.txt and santoshit.txt...
Files compare OK
D:\IT_24> COMP Santosh24.txt aa.txt
Comparing Santosh24.txt and aa.txt...
Files are different sizes.
21. Command: FC
Description: Compares two files and displays the difference between them.
Syntax: FC filename1 filename2
Example Output:
D:\IT_24> FC Santosh24.txt aa.txt
Comparing files Santosh24.txt and aa.txt
***** Santosh24.txt
Name: Santosh
Branch: IT
***** aa.txt
Santosh IT
Rs 50200
*****
22. Command: MORE
Description: Allows information to be displayed one page at a time. Displays a text file to the
screen.
Syntax: MORE filename
DIR | MORE
Example Output:
D:\> DIR | MORE
Volume in drive D is New Volume
Volume Serial Number is 96B5-3300
Directory of D:\
11-10-2012 22:14 <DIR> ANITS
28-03-2012 22:03 15,543 Books and Authors.docx
30-09-2012 16:26 <DIR> c material
07-10-2012 12:56 186 C.txt
-- More --
11-10-2012 22:14 <DIR> ANITS
28-03-2012 22:03 15,543 Books and Authors.docx
30-09-2012 16:26 <DIR> c material
07-10-2012 12:56 186 C.txt

Dept. of M.E., NITA Page 8


Numerical Analysis and Computer Programming

03-02-2012 22:49 28,497 Write a C program to print all permutations


of a given string.docx
59 File(s) 44,477,964 bytes
20 Dir(s) 64,920,227,840 bytes free
23. Command: START
Description: Starts a separate window to run a specified program or command.
Syntax: START [title] [/d path]
Example Output:
D:\> START "CPNM" /d C:
CPNMLAB — x CPNM — x
D:\> START "CPNM" /d C: C:\>

24. Command: FIND


Description: Searches for a text String in a file
Syntax: FIND [OPTION] "string" file
Example Output:
D:\IT_24> FIND "e" Santosh24.txt
---------- SANTOSH24.TXT
Name: Santosh
College: ANITS
D:\IT_24> FIND /v "e" Santosh24.txt [Display lines not containing string]
---------- SANTOSH24.TXT
Branch: IT
Rank: 22000
D:\IT_24> FIND /c "e" Santosh24.txt [Display count of lines containing string]
---------- SANTOSH24.TXT: 2
D:\IT_24> FIND /n "e" Santosh24.txt [Display lines with numbers containing string]
---------- SANTOSH24.TXT
[1] Name: Santosh
[4] College: ANITS
D:\IT_24> FIND /i "E" Santosh24.txt [Ignore case of characters]
---------- SANTOSH24.TXT
Name: Santosh
College: ANITS
25. Command: EXIT
Description: Quits the CMD.EXE program (command interpreter).
Syntax: EXIT
Example Output:
D:\> EXIT {closes the command window}
26. Command: ATTRIB
Description: Displays or changes file attributes.
Syntax: ATTRIB [+R | -R] [+H | -H] [drive:] [path][filename]
+ Sets an attribute
- Clears an attribute
H Hidden File Attribute
R Read-Only File Attribute

Dept. of M.E., NITA Page 9


Numerical Analysis and Computer Programming

Example Output:
D:\IT_24> ATTRIB +R Santosh24.txt
{Sets the read only attribute to the file and cannot be modified}
D:\IT_24> ATTRIB -R Santosh24.txt
{Clears the read only attribute to the file and can be modified}
D:\IT_24> ATTRIB +H Santosh24.txt
{Sets the hidden attribute to the file and cannot be viewed}
D:\IT_24> ATTRIB -H Santosh24.txt
{Clears the hidden attribute to the file and can be viewed}
27. Command: PROMPT
Description: Changes the cmd.exe command prompt.
Syntax: PROMPT [text] $G
Example Output:
D:\> PROMPT CPNM$G
CPNM>
{Use the DOS commands to execute, stays in current directory with given prompt}
{to come to actual prompt, type ‘prompt’ and then press enter}
28. Command: PATH
Description: Displays or sets a search path for executable files & used to provide access to files
located on other directories.
Syntax: PATH = “[drive:path]”;
Example Output:
D:\IT_24> PATH = “C:\WINDOWS\system32”;
{Sets the path for system32 directory to invoke the files required by DOS}
29. Command: SORT
Description: Sorts input in alphanumeric order.
Syntax: SORT [/r] (enter)
[input text {enter}]
Ctrl + Z
[output text {sorted order}]
Example Output:
D:\IT_24> SORT D:\IT_24> SORT /r
hat hat
cat cat
mat mat
bat bat
ant ant
^Z [Ctrl + Z] ^Z [Ctrl + Z]
ant mat
bat hat
cat cat
hat bat
mat {Displays in A To Z order} ant {Displays in Z To A order}

30. Command: TREE


Description: Displays the directories and subdirectories existing in a drive in a Tree diagram
without files. Only directories with <DIR> are displayed.
Syntax: TREE

Dept. of M.E., NITA Page 10


Numerical Analysis and Computer Programming

Example Output:
D:\network lab>TREE
Folder PATH listing
Volume serial number is 2475-5834
D:\NETWORK LAB
├───FTP
├───http
├───new tftp
├───remotehost
└───telnet
└───telnet

Dept. of M.E., NITA Page 11


Numerical Analysis and Computer Programming

C - Programs
(2) Write a program to print the message Hello World.
Algorithm:
Step 1: Start
Step 2: Display "Hello World"
Step 3: Stop
Flowchart: Program:
#include<stdio.h>
main()
{
printf("Hello World");
}

(3) Write a Program to perform the arithmetic operators of two numbers.


Algorithm:
Step 1: Start
Step 2: Read two numbers a,b
Step 3: Display "Addition = ", a+b
Step 4: Display "Difference = ", a-b
Step 5: Display "Product = ", a*b
Step 6: Display "Quotient = ", a/b
Step 7: Display "Remainder = ", a%b
Step 8: Stop
Flowchart:

Dept. of M.E., NITA Page 12


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
printf("Addition = %d",a+b);
printf("Difference = %d",a-b);
printf("Product = %d",a*b);
printf("Quotient = %d",a/b);
printf("Remainder = %d",a%b);
getch();
}

(4) Write a Program to find sum and average of 5 numbers.


Algorithm:
Step 1: Start
Step 2: Read five numbers a,b,c,d,e
Step 3: Compute the sum of 5 numbers and store the result in variable 'sum'
Step 4: Divide the sum by 5 and store the result in variable 'avg'
Step 5: Display "Sum of 5 numbers = ", sum
Step 6: Display "Average of 5 numbers = ", avg
Step 7: Stop
Flowchart:

Program:
#include<stdio.h>
main()
{
int a,b,c,d,e,sum,avg;

Dept. of M.E., NITA Page 13


Numerical Analysis and Computer Programming

clrscr();
printf("Enter five numbers: ");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
sum = a+b+c+d+e;
avg = sum/5;
printf("Sum of 5 numbers = %d",sum);
printf("Average of 5 numbers = %d",avg);
getch();
}
(5) Write a Program to find the simple interest.
Algorithm:
Step 1: Start
Step 2: Read p, t, r values
Step 3: Compute the product of p, t and r and divide the result by 100
Step 4: Store the result obtained in variable 'si'
Step 5: Display "Simple Interest = ", si
Step 6: Stop
Flowchart:

Program:
#include<stdio.h>
main()
{
int p,t;
float r,si;
printf("Enter principle, time and rate values: ");
scanf("%d %d %f",&p,&t,&r);
si = (p*t*r)/100.0;
printf("Simple Interest = %f",si);
}

Dept. of M.E., NITA Page 14


Numerical Analysis and Computer Programming

(6) Write a Program to evaluate the expression


i. (ax+by)/(ax-by)
ii. (a2+b2+ √(2ab)
Algorithm:
(i)
Step 1: Start
Step 2: Input a, b, x, y values
Step 3: Compute the product of a & x and b & y; compute sum of two values and store
the result in ‘num’ variable.
Step 4: Compute the product of a & x and b & y; compute difference of two values and
store the result in ‘den’ variable.
Step 5: Compute the division of num and den and store the result in variable ‘res’
Step 6: Display “Result = ”, res
Step 7: Stop
(ii)
Step 1: Start
Step 2: Input a, b, x, y values
Step 3: Compute the square of a, square of b; compute product of variables a, b with 2
and compute the square root of the product; compute sum of two values and store the
result in ‘c’ variable.
Step 6: Display “Result = ”, c
Step 7: Stop
Flowchart:
(i) (ii)

Program (i):
#include<stdio.h>
main()
{
float a,b,x,y;
float num,den,res;
printf("Enter a,b,x,y values: ");
scanf("%f %f %f %f",&a,&b,&x,&y);

Dept. of M.E., NITA Page 15


Numerical Analysis and Computer Programming

num = (a*x+b*y);
den = (a*x-b*y);
res = num/den;
printf("Result = %f",res);
}
Program (ii):
#include<stdio.h>
#include<math.h>
main()
{
float a,b;
float c;
printf("Enter a,b values: ");
scanf("%f %f %f %f",&a,&b);
c=(a*a)+(b*b)+sqrt(2*a*b);
printf("Result = %f",c);
}
(7) Write a program to check whether given number is even or odd.
Algorithm:
Step 1: Start
Step 2: Input a value
Step 3: If "a%2 == 0"
Step 3.1: Display a, "is an even number".
Step 4: Else
Step 4.1: Display a, "is an odd number".
Step 5: EndIf;
Step 6: Stop
Flowchart:

Program:
#include<stdio.h>
main()
{
int a;

Dept. of M.E., NITA Page 16


Numerical Analysis and Computer Programming

printf("Enter a number: ");


scanf("%d",&a);
if(a%2==0)
printf("%d is an even number",a);
else
printf("%d is not an even number",a);
}
(8) Write a program to find largest of given three numbers.
Algorithm:
Step 1: Start
Step 2: Read A, B, C values
Step 3: If "A>B"
Step 3.1: If "A>C"
Step 3.1.1: Display A
Step 3.2: Else
Step 3.2.1: Display C
Step 3.3: EndIf;
Step 4: Else
Step 4.1: If "B>C"
Step 4.1.1: Display B
Step 4.2: Else
Step 4.2.1: Display C
Step 4.3: EndIf;
Step 5: EndIf;
Step 6: Stop
Flowchart:

Program:
#include<stdio.h>
main()
{
int a,b,c;

Dept. of M.E., NITA Page 17


Numerical Analysis and Computer Programming

printf("Enter 3 numbers: ");


scanf("%d %d %d",&a,&b,&c);
if(a>b) {
if(a>c)
printf("%d",a);
else
printf("%d",c);
}
else {
if(b>c)
printf("%d",b);
else
printf("%d",c);
}
}
(9) Write a program to find roots of a quadratic equation.
Algorithm:
Step 1: Start
Step 2: Input a, b, c values
Step 3: If "a==0 AND b==0"
Step 3.1: Display "Roots are not possible"
Step 4: Else
Step 4.1: Compute square of b; compute product of 4, a, and c;
Compute the difference of two values and store the result in variable d
Step 4.2: If "d==0"
Step 4.2.1: Compute the division of -b with product of 2 and a
Store the result in variables r1 and r2
Step 4.2.2: Display "Root are real and equal"
Step 4.2.3: Display "Roots are ", r1, r2
Step 4.3: Else
Step 4.3.1: If "d>0"
Step 4.3.1.1: Compute the addition of -b and square root of d
Divide the result with product of 2 and a
Store the result in r1.
Step 4.3.1.2: Compute the difference of -b and square root of d
Divide the result with product of 2 and a
Store the result in r2.
Step 4.3.1.3: Display "Roots are real and unequal"
Step 4.3.1.4: Display "Roots are ", r1, r2
Step 4.3.2: Else
Step 4.3.2.1: Display "Roots are imaginary"
Step 4.3.3: EndIf;
Step 4.4: EndIf;
Step 5: Endif;
Step 6: Stop

Dept. ofM.E., NITA Page 18


Numerical Analysis and Computer Programming

Flowchart:

Program:
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float d,r1,r2;
printf("Enter a,b,c values: ");
scanf("%d %d %d",&a,&b,&c);
if(a==0 && b==0)
printf("Roots are not possible");
else
{
d=(b*b)-(4*a*c);
if(d==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are real and equal");
printf("\nRoots are %f %f",r1,r2);
}
else
{
if(d>0)

Dept. of M.E., NITA Page 19


Numerical Analysis and Computer Programming

{
r1 = (-b + sqrt(d))/(2*a);
r2 = (-b - sqrt(d))/(2*a);
printf("Roots are real and unequal");
printf("\nRoots are %f %f",r1,r2); }
else
printf("\nRoots are imaginary");
}
}
(10) Write a program to find grade of the student for his 5 subject marks out of 100.
Algorithm:
Step 1: Start
Step 2: Read 5 subjects marks s1, s2, s3, s4, s5
Step 3: If “s1<40 OR s2<40 OR s3<40 OR s4<40 OR s5<40”
Step 3.1: Print “STUDENT FAIL”
Step 4: Else
Step 4.1: Compute addition of 5 subjects s1, s2, s3, s4, s5 and store the result in “sum”
Step 4.2: Divide sum by 5 and store the result in “avg”
Step 4.3: Display “STUDENT PASS”
Step 4.4: Display “Sum = ”, sum, “Average = ”, avg
Step 4.5: If “avg>=90 AND avg<=100”
Step 4.5.1: Print “Grade A”
Step 4.6: Else
Step 4.6.1: If “avg>=80 AND avg<=89”
Step 4.6.1.1: Print “Grade B”
Step 4.6.2: Else
Step 4.6.2.1: If “avg>=70 AND avg<=79”
Step 4.6.2.1.1: Print “Grade C”
Step 4.6.2.2: Else
Step 4.6.2.2.1: If “avg>=60 AND avg<=69”
Step 4.6.2.2.1: Print “Grade D”
Step 4.6.2.2.2: Else
Step 4.6.2.2.2.1: If “avg>=50 AND avg<=59”
Step 4.6.2.2.2.1: Print “Grade E”
Step 4.6.2.2.2.2: Else
Step 4.6.2.2.2.2.1: Print “Grade F”
Step 4.6.2.2.2.3: EndIf;
Step 4.6.2.2.3: EndIf;
Step 4.6.2.3: EndIf;
Step 4.6.3: EndIf;
Step 4.7: EndIf;
Step 5: EndIf;
Step 6: Stop

Dept. of M.E., NITA Page 20


Numerical Analysis and Computer Programming

Flowchart:

Program:
#include<stdio.h>
main()
{
int s1,s2,s3,s4,s5;
float sum,avg;
printf("Enter 5 subject marks: ");
scanf("%d %d %d %d %d",&s1,&s2,&s3,&s4,&s5);
if(s1<40||s2<40||s3<40||s4<40||s5<40)
{
printf("STUDENT FAIL");
}
else
{
sum = s1+s2+s3+s4+s5;
avg = sum/5;
printf("STUDENT PASS\nSum = %f\nAverage = %f",sum,avg);
if(avg>=90 && avg<=100)
printf("\nGrade A");

Dept. of M.E., NITA Page 21


Numerical Analysis and Computer Programming

else if(avg>=80 && avg<=89)


printf("\nGrade B");
else if(avg>=70 && avg<=79)
printf("\nGrade C");
else if(avg>=60 && avg<=69)
printf("\nGrade D");
else if(avg>=50 && avg<=59)
printf("\nGrade E");
else
printf("\nGrade F");
}
}

(11) Write a program to find area of the triangle when


i. Sides are given
ii. Base and Height are given
iii. Co-ordinates are given; using switch statement
Algorithm:
Step 1: Start
Step 2: Display "Area of Triangle with \n 1) Base & Height\n 2) Sides\n 3) Coordinates"
Step 3: Display "Enter choice"
Step 4: Read choice
Step 5: if (choice == 1) goto Step 9;
Step 6: if (choice == 2) goto Step 10;
Step 7: if (choice == 3) goto Step 11;
Step 8: else goto Step 12;
Step 9:
Case 1:
Step 9.1: Read base and height
Step 9.2: Compute product of 0.5, base and height and store the result in variable "area"
Step 9.3: Display "Area is ", area
Step 9.4: goto Step 13
EndCase;
Step 10:
Case 2:
Step 10.1: Read a, b and c values
Step 10.2: Compute the addition of a, b, c values and divide the result by 2
Store the result in variable s
Step 10.3: Compute the product of s, s-a, s-b, s-c
Calculate square root of the result
Store the result in variable area
Step 10.4: Display "Area is ", area
Step 10.5: goto Step 13
EndCase;
Step 11:
Case 3:

Dept. of M.E., NITA Page 22


Numerical Analysis and Computer Programming

Step 11.1: Read x1, x2, x3, y1, y2, y3


Step 11.2: Compute area = (x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2
Step 11.3: Display "Area is ", area
Step 11.4: goto Step 13
EndCase;
Step 12:
Default Case:
Display "Invalid Option"
goto Step 13
EndCase;
Step 13: Stop
Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()

{
int a,b,c,s,base,h,x1,x2,x3,y1,y2,y3,choice;
float area;

Dept. of M.E., NITA Page 23


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int ar[10];
int i, j, temp;
clrscr();
for (i = 0 ; i < 10 ; i++)
{
printf("Enter number for [%d] element: ",i);
scanf("%d", &ar[i]);
}
/* sort array in descending order */
for ( i = 0 ; i < 9 ; i++)
{
for ( j = i+1; j < 10 ; j ++)
{
if ( ar[i] > ar[j])
{
/* interchange */
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
} /* end of j loop */
} /* end of i loop */
/* display sorted array */
printf("\nSorted Numbers in Descending \n");
for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
/* sort array in ascending order */
for ( i = 0 ; i < 9 ; i++)
{
for ( j = i+1; j < 10 ; j ++)
{
if ( ar[i] < ar[j])
{
/* interchange */
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
} /* end of j loop */
} /* end of i loop */
/* display sorted array */

Dept. of M.E., NITA Page 41


Numerical Analysis and Computer Programming

printf("\nSorted Numbers in Ascending \n");


for ( i = 0 ; i < 10 ; i++)
{
printf("%d\n", ar[i]);
}
getch();
}
(23) Write a program to display random integer numbers in the range 1-100 and find the average
of these by using for loop
Algorithm:
Step 1: Start
Step 2: Initialize sum to 0
Step 3: Display “Numbers are: ”
Step 4: For i = 0 to 100 do
Step 4.1: Set a[i] = random(100)
Step 4.2: Print a[i]
Step 4.3: Increment i by 1
Step 5: EndFor;
Step 6: For i = 0 to 100 do
Step 6.1: Compute sum = sum + a[i]
Step 6.2: Increment i by 1
Step 7: EndFor;
Step 8: Divide sum with 100 and store the result in variable “avg”
Step 9: Display “Average of numbers: ”, avg
Step 10: Stop
Flowchart:

Dept. of M.E., NITA Page 42


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,a[100],sum=0;
float avg;
clrscr();
printf("Numbers are \n");
for(i=0;i<100;i++)
{
a[i]=random(100);
printf("%d\t",a[i]);
}
for(i=0;i<100;i++)
{
sum=sum+a[i];
}
avg=sum/100.0;
printf("\nAverage of the numbers is %f",avg);
getch();
}
(24) Write a program to generate 100 random real numbers in the range 10 to 20 and sort
them.
Algorithm:
Step 1: Start
Step 2: For i = 0 to 100 do
Step 2.1: Array element a[i] = 10 + rand()/32767.0 + random(10)
Step 2.2: Display array element
Step 2.3: Increment i by 1
Step 3: EndFor;
Step 4: For i = 0 to 99 do
Step 4.1: For j = 0 to 100 do
Step 4.1.1: If “ar[i]>ar[j]”
Step 4.1.1.1: Swap the values of ar[i] and ar[j] using third variable
Step 4.1.2: EndIf;
Step 4.1.3: Increment j by 1
Step 4.2: EndFor;
Step 4.3: Increment i by 1
Step 5: EndFor;
Step 6: Display “Sorted order”
Step 7: For i = 0 to 100 do
Step 7.1: Display each element of array
Step 7.2: Increment i by 1
Step 8: EndFor;

Dept. of M.E., NITA Page 43


Numerical Analysis and Computer Programming

Step 9: Stop
Flowchart:

Program:
#include<stdlib.h>
#include<stdio.h>
main()
{
int i,j;
float a[100],temp;
clrscr();
printf("Random numbers in the range 10.0 to 20.0\n\n");
for(i=0;i<100;i++)
{
a[i]=10+rand()/32767.0+random(10);
printf("%.2f\t",a[i]);
}

Dept. ofM.E., NITA Page 44


Numerical Analysis and Computer Programming

for(i=0;i<99;i++)
{
for(j=i+1;j<100;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Sorting order\n\n");
for(i=0;i<100;i++) {
printf("%.2f\t",a[i]); }
getch();
}
(25) Write a program which determines largest and smallest number that can be stored in
different data types.
Algorithm:
Step 1: Start
Step 2: Print the Maximum value of Integer
Step 3: Print the Minimum value of Integer
Step 4: Print the Maximum value of Short Integer
Step 5: Print the Minimum value of Short Integer
Step 6: Print the Maximum value of Character
Step 7: Print the Minimum value of Character
Step 8: Print the Maximum value of Float
Step 9: Print the Minimum value of Float
Step 10: Print the Maximum value of Double
Step 11: Print the Minimum value of Double
Step 12: Stop
Flowchart:

Dept. of M.E., NITA Page 45


Numerical Analysis and Computer Programming

Program:
#include<stdlib.h>
#include<limits.h>
#include<float.h>
main()
{
clrscr();
printf("\nINT max - %d",INT_MAX);
printf("\nINT min - %d",INT_MIN);
printf("\nSHRT INT max - %d",SHRT_MAX);
printf("\nSHRT INT min - %d",SHRT_MIN);
printf("\nCHAR max - %d",CHAR_MAX);
printf("\nCHAR min - %d",CHAR_MIN);
printf("\nFLOAT max - %e",FLT_MAX);
printf("\nFLOAT min - %e",FLT_MIN);
printf("\nDOUBLE max - %e",DBL_MAX);
printf("\nDOUBLE min - %e",DBL_MIN);
getch();
}
(26) Write a program to multiply two matrices.
Algorithm:
Step 1: Start
Step 2: Read order of matrix A m, n values
Step 3: For i = 0 to m do
Step 3.1: For j = 0 to n do
Step 3.1.1: Read element of two dimensional array A
Step 3.1.2: Increment j by 1
Step 3.2: EndFor;
Step 3.3: Increment i by 1
Step 4: EndFor;
Step 5: Read order of matrix B p, q values
Step 6: For i = 0 to p do
Step 6.1: For j = 0 to q do
Step 6.1.1: Read element of two dimensional array B
Step 6.1.2: Increment j by 1
Step 6.2: EndFor;
Step 6.3: Increment i by 1
Step 7: EndFor;
Step 8: If “n==p”
Step 8.1: For i = 0 to m do
Step 8.1.1: For j = 0 to q do
Step 8.1.1.1: Initialize elements of matrix C to 0
Step 8.1.1.2: For k=0 to m do
Step 8.1.1.2.1: c[i][j] = c[i][j] + a[i][k] * b[k][j]
Step 8.1.1.2.2: Increment k by 1
Step 8.1.1.3: EndFor;

Dept. of M.E., NITA Page 46


Numerical Analysis and Computer Programming

Step 8.1.2: Increment j by 1


Step 8.2: EndFor;
Step 8.3: Increment i by 1
Step 9: EndFor;
Step 10: Display “Result Matrix ”
Step 11: For i = 0 to m do
Step 11.1: For j = 0 to q do
Step 11.1.1: Display elements of array C
Step 11.1.2: Increment j by 1
Step 11.2: EndFor;
Step 11.3: Increment i by 1
Step 12: EndFor;
Step 13: Stop

Flowchart:

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

Dept. of M.E., NITA Page 47


Numerical Analysis and Computer Programming

void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
clrscr();
printf("Enter order of matrix A for m & n\n");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter a[%d][%d] element: ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter order of matrix B for p & q\n");
scanf("%d%d",&p,&q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter b[%d][%d] element: ",i,j);
scanf("%d",&b[i][j]);
}
}
if(n==p) {
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
printf("Elements after multiplication\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%5d",c[i][j]);
}
printf("\n");
}
}
else
printf("Mutliplication Not Possible for given matrices!");
getch();
}

Dept. of M.E., NITA Page 48


Numerical Analysis and Computer Programming

(27) Write a program to find the transpose of a matrix.


Algorithm:
Step 1: Start
Step 2: Read matrix order r, c values
Step 3: For i = 0 to r do
Step 3.1: For j = 0 to c do
Step 3.1.1: Read element of two dimensional array A
Step 3.1.2: Increment j by 1
Step 3.2: EndFor;
Step 3.3: Increment i by 1
Step 4: EndFor;
Step 5: For i = 0 to c do
Step 5.1: For j = 0 to r do
Step 5.1.1: put the element of A matrix into matrix B
Step 5.1.2: Increment j by 1
Step 5.2: EndFor;
Step 5.3: Increment i by 1
Step 6: EndFor;
Step 7: Display “Transpose of a matrix is”
Step 8: For i = 0 to c do
Step 8.1: For j = 0 to r do
Step 8.1.1: Display element of matrix B
Step 8.1.2: Increment j by 1
Step 8.2: EndFor;
Step 8.3: Increment i by 1
Step 9: EndFor;
Step 10: Stop

Flowchart:

Dept. of M.E., NITA Page 49


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],b[10][10];
int i,j,r,c;
clrscr();
printf("Enter order of matrix\n");
scanf("%d %d",&r,&c);
printf("Enter the elements\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter [%d][%d] element\t",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
b[j][i]=a[i][j];
}
}
printf("\n\nThe transpose of the matrix is \n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}
(28) Write a program to find the trace and norm of a matrix.
Algorithm:
Step 1: Start
Step 2: For i = 0 to 3 do
Step 2.1: For j = 0 to 3 do
Step 2.1.1: Read element for matrix
Step 2.1.2: Increment j by 1
Step 2.2: EndFor;
Step 2.3: Increment i by 1
Step 3: EndFor;

Dept. of M.E., NITA Page 50


Numerical Analysis and Computer Programming

Step 4: Initialize trace = 0 and sum = 0


Step 5: For i = 0 to 3 do
Step 4.1: For j = 0 to 3 do
Step 4.1.1: Print element of matrix
Step 4.1.2: If “i==j”
Step 4.1.2.1: Compute trace as addition of array element and trace value
Step 4.1.2.2: Compute sum as addition of array element square and sum
Value
Step 4.1.3: EndIf;
Step 4.2: EndFor;
Step 6: EndFor;
Step 7: Set s as square root of sum value
Step 8: Display “Trace of the matrix:”, trace
Step 9: Display “Norm of the matrix:”, s
Step 10: Stop

Flowchart:

Dept. of M.E., NITA Page 51


Numerical Analysis and Computer Programming

Program:
#include<math.h>
main()
{
int a[3][3],i,j,trace=0;
float sum = 0,s;
clrscr();
printf("Enter elements of matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
if(i==j) {
trace = trace + a[i][j];

sum = sum + pow(a[i][j],2);


}

}
printf("\n");
}
s = sqrt(sum);
printf("Trace of the matrix: %d\n",trace);
printf("Norm of the matrix: %f",s);
getch();
}
(29) Write a program to implement a set of string handling functions using switch case.
Algorithm:
Step 1: Start
Step 2: while “TRUE”
Step 2.1: Display “MENU\n1.Length of string\n2.String uppercase\n3.String Lowercase”
Step 2.2: Display “\n4.String copy\n5.String concatenate\n6.String reverse”
Step 2.3: Display “\n7.Exit\nEnter choice: ”
Step 2.4: Read ch
Step 2.5: Read string s1
Step 2.6: if (choice == 1) goto Step 2.14;
Step 2.7: if (choice == 2) goto Step 2.15;
Step 2.8: if (choice == 3) goto Step 2.16;
Step 2.9: if (choice == 4) goto Step 2.17;

Dept. of M.E., NITA Page 52


Numerical Analysis and Computer Programming

Step 2.10: if (choice == 5) goto Step 2.18;


Step 2.11: if (choice == 6) goto Step 2.19;
Step 2.12: if (choice == 7) goto Step 2.20;
Step 2.13: else goto Step 2.21;
Step 2.14:
Case 1:
Step 2.14.1: Store result of strlen(s1) to variable len
Step 2.14.2: Display “Length of string ”, len
Step 2.14.3: goto step 2.22
EndCase;
Step 2.15:
Case 2:
Step 2.15.1: Display “String in uppercase is ”, strupr(s1)
Step 2.15.2: goto step 2.22
EndCase;
Step 2.16:
Case 3:
Step 2.16.1: Display “String in lowercase is ”, strlwr(s1)
Step 2.16.2: goto step 2.22
EndCase;
Step 2.17:
Case 4:
Step 2.17.1: Copy the string s1 to s3
Step 2.17.2: Display “Copied string is ”, s3
Step 2.17.3: goto step 2.22
EndCase;
Step 2.18:
Case 5:
Step 2.18.1: Read string s2
Step 2.18.2: Combine string s2 with s1 and store the result in s1
Step 2.18.3: Display “Concatenated string is ”, s1
Step 2.18.4: goto step 2.22
EndCase;
Step 2.19:
Case 3:
Step 2.16.1: Display “Reversed string is ”, strrev(s1)
Step 2.16.2: goto step 2.22
EndCase;
Step 2.20: goto step
Step 2.21:
Default Case:
Step 2.21.1: Display “Invalid Option”
Step 2.21.2: goto step 2.22
EndCase;
Step 2.22: Display “Do you wish to continue: (y/n)”

Dept. of M.E., NITA Page 53


Numerical Analysis and Computer Programming

Step 2.23: Read c


Step 2.24: if c == ‘n’ OR c == ‘N’
Step 2.24.1: goto step 3
Step 3: EndWhile;
Step 4: Stop

Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[50],s2[50],s3[50];
int len,ch;
char c;
clrscr();
while(1)
{
printf("MENU\n--------------------\n1.Length of String");
printf("\n2.String uppercase\n3.String lowercase\n4.String
copy");

Dept. of M.E., NITA Page 54


Numerical Analysis and Computer Programming

printf("\n5.String concatenate\n6.String reverse\n7.Exit");


printf("\nEnter choice: ");
scanf("%d",&ch);
fflush(stdin);
printf("Enter String: ");
gets(s1);
switch(ch)
{
case 1: len=strlen(s1);
printf("Length of String is %d",len);
break;
case 2:
printf("String in uppercase is %s",strupr(s1));
break;
case 3:
printf("String in lowercase is %s",strlwr(s1));
break;
case 4: strcpy(s3,s1);
printf("Copied String is %s",s3);
break;
case 5: fflush(stdin);
printf("Enter another String: ");
gets(s2);
strcat(s1,s2);
printf("Concatenated String: %s",s1);
break;
case 6: printf("Reversed String is %s",strrev(s1));
break;
case 7: exit(0);
break;
default: printf("Invalid Option");
break;
}
printf("\nDo you wish to continue? (y/n) ");
c=getchar();
if(c=='n'||c=='N')
break;
}
getch();
}

(30) Write a program to create a structure for a student with details {name, rollno, 5 subject
marks, total marks, percentage} and sort the records according to percentage.
Algorithm:
Step 1: Start
Step 2: Initialize t = 0
Step 3: For i = 0 to 5 do
Step 3.1: Read s[i].name, s[i].rn

Dept. of M.E., NITA Page 55


Numerical Analysis and Computer Programming

Step 3.2: Initialize s[i].tm = 0


Step 3.3: For j = 0 to 5 do
Step 3.3.1: Read s[i].marks[j]
Step 3.3.2: Compute s[i].tm as sum of s[i].tm value and s[i].marks[j] value
Step 3.3.3: Increment j by 1
Step 3.4: EndFor;
Step 3.5: Display “Total ”, s[i].tm
Step 3.6: Compute s[i].per as division of s[i].tm and 5.0
Step 3.7: Display “Percentage ”, s[i].per
Step 4: EndFor;
Step 5: For i = 0 to 4 do
Step 5.1: For j = 0 to 5 do
Step 5.1.1: If “s[i].per>s[j].per”
Step 5.1.1.1: Swap the values of s[i] and s[j] using third variable
Step 5.1.2: EndIf;
Step 5.1.3: Increment j by 1
Step 5.2: EndFor;
Step 5.3: Increment i by 1
Step 6: EndFor;
Step 7: For i = 0 to 5 do:
Step 7.1: Display s[i].name, s[i].per
Step 7.2: Increment i by 1
Step 8: EndFor;
Step 9: Stop
Flowchart:

Dept. of M.E., NITA Page 56


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
main()
{
struct student
{
char name[30];
long int rn;
int marks[5];
int tm;
float per;
};
struct student s[5],temp;
int i,j;
float tot = 500;
clrscr();
for(i=0;i<5;i++)
{
s[i].total=0;
fflush(stdin);
printf("Enter student name: ");
gets(s[i].name);
printf("Enter student number: ");
scanf("%ld",&s[i].rn);
printf("Enter 5 subject marks for 100: ");
for(j=0;j<5;j++){
scanf("%d",&s[i].marks[j]);
s[i].tm += s[i].tm[j];
}
printf(“Total: %d”,s[i].tm);
s[i].per = s[i].tm/5.0;
printf(“Percentage: %d”,s[i].per)
}
for(i=0;i<5;i++) {
for(j=i+1;j<4;j++) {
if(s[i].per>s[j].per)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for(i=0;i<5;i++)
{
printf("\n%-10s %5ld %5d %0.2f",s[i].name, s[i].per);
}
}

Dept. of M.E., NITA Page 57


Numerical Analysis and Computer Programming

(31) Write a program to illustrate how to use pointer to structure.


Algorithm:
Step 1: Start
Step 2: Read e.ename value
Step 3: Read e.eno value
Step 4: Read e.bs value
Step 5: Assign address of structure ‘e’ to pointer ‘emp’
Step 6: Print “Employee Details: ”
Step 7: Display values of structure with pointer ‘emp’ as emp -> eno, emp -> eno, emp -> bs
Step 8: Stop

Flowchart:

Program:
#include<stdio.h>
main()
{
struct employee {
int eno;
char ename[20];
float bs;
};
struct employee e,*emp;
clrscr();
printf("Enter employee name: ");
gets(e.ename);

Dept. of M.E., NITA Page 58


Numerical Analysis and Computer Programming

printf("Enter employee number: ");


scanf("%d",&e.eno);
printf("Enter employee salary: ");
scanf("%f",&e.bs);
emp = &e;
printf("\nEmployee Details:\n");
printf("%d\t%s\t%0.2f",emp->eno,emp->ename,emp->bs);
getch();
}

(32) Write a program to assign values to union members.


Algorithm:
Step 1: Start
Step 2: Read x.name value
Step 3: Read x.n2 value
Step 4: Read x.n1 value
Step 5: Display “Value of n1 = ”,x.n1
Step 6: Display “Value of n2 = ”,x.n2
Step 7: Display “Name = ”,x.name
Step 8: Stop

Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
main()
{

Dept. of M.E., NITA Page 59


Numerical Analysis and Computer Programming

(43) Write a program to merge two files into third auxiliary file and display the content.
Algorithm:
Step 1: Start
Step 2: Read file1, file2, file3
Step 3: Open file1 and file2 in read mode
Step 4: If “file1 is empty or file2 is empty”
Step 4.1: Goto Step 7
Step 5: Else
Step 5.1: Open file3 in write + mode
Step 5.2: While “character read from file1 is not End Of File”
Step 5.2.1: Put character in file3
Step 5.3: EndWhile;
Step 5.4: While “character read from file2 is not End Of File”
Step 5.4.1: Put character in file3
Step 5.5: EndWhile;
Step 5.6: Print “Two files were merged into ”, file3, “ file successfully”
Step 5.7: Print “Content in third file: ”
Step 5.8: Close file1 and file2
Step 5.9: Move the pointer of file3 to starting position
Step 5.10: While “character read from file3 is not End Of File”
Step 5.10.1: Print character
Step 5.11: EndWhile;
Step 6: EndIf;
Step 7: Stop

Flowchart:

Program:
#include <stdio.h>
main()
{
FILE *fs1, *fs2, *ft;

Dept. of M.E., NITA Page 77


Numerical Analysis and Computer Programming

char ch, file1[20], file2[20], file3[20];


printf("Enter name of first file\n");
gets(file1);
printf("Enter name of second file\n");
gets(file2);
printf("Enter name of file to store contents of two files\n");
gets(file3);
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
if( fs1 == NULL || fs2 == NULL )
exit(0);
ft = fopen(file3,"w+");
while( ( ch = fgetc(fs1) ) != EOF )
fputc(ch,ft);
while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);
printf("Two files were merged into %s file successfully.\n");
printf("Content in third file");
fclose(fs1);
fclose(fs2);
rewind(ft);
while( ( ch = fgetc(ft) ) != EOF )
printf(“%c”,ch);
fclose(ft);o
}

(44) Write a program to print every fifth character in a given file.


Algorithm:
Step 1: Start
Step 2: Open text file in read mode
Step 3: Initialize n=0
Step 4: While “c read from file is not End Of File”
Step 4.1: Print c
Step 4.2: Increment n by 5
Step 4.3: Move pointer of file to position n
from the beginning of the file
Step 5: EndWhile;
Step 6: End;

Flowchart:

Dept. of M.E., NITA Page 78


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
main()
{
int n=0;
char c;
FILE *fp;
clrscr();
fp = fopen("f1.txt","r");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
n=n+5;
fseek(fp,n,0);
}
getch();
}

(45) Write a program to convert uppercase letters into lowercase and vice versa for contents in a file
and display the result.
Algorithm:
Step 1: Start
Step 2: Read filename
Step 3: Open the file in write mode
Step 4: While "c read from console is not EOF"
Step 4.1: Put c in the file
Step 5: EndWhile;
Step 6: Close the file
Step 7: Open the file in read mode
Step 8: Print "Lowercase to Uppercase: "
Step 9: While "c read from file is not EOF"
Step 9.1: If " c>='a' && c<='z' "
Step 9.1.1: Print c-32
Step 9.2: Else
Step 9.2.1: Print c
Step 9.3: EndIf;
Step 10: EndWhile;
Step 11: Close the file
Step 12: Open the file in read mode
Step 13: Print "Uppercase to Lowercase: "
Step 14: While "c read from file is not EOF"
Step 14.1: If " c>='A' && c<='Z' "
Step 14.1.1: Print c+32
Step 14.2: Else
Step 14.2.1: Print c
Step 14.3: EndIf;

Dept. of M.E., NITA Page 79


Numerical Analysis and Computer Programming

Step 15: EndWhile;


Step 16: Close the file
Step 17: Stop

Flowchart:

Program:
#include <stdio.h>
main()
{
char fn[30];
char c;
FILE *fp;
clrscr();
printf("Enter filename: ");
gets(fn);
printf("Write data to file: ");
fp = fopen(fn,"w");
while((c=getchar())!=EOF)

Dept. of M.E., NITA Page 80


Numerical Analysis and Computer Programming

putc(c,fp);
fclose(fp);
fp=fopen(fn,"r");
printf("\nLowercase to Uppercase: ");
while((c=getc(fp))!=EOF)
{
if( c >= 'a' && c <= 'z' )
putchar(c-32);
else
putchar(c);
}
fclose(fp);
fp=fopen(fn,"r");
printf("\nUppercase to Lowercase: ");
while((c=getc(fp))!=EOF)
{
if( c >= 'A' && c <= 'Z' )
putchar(c+32);
else
putchar(c);
}
fclose(fp);
getch();
}

(46) Write a program to implement Lagrange's interpolation.


Algorithm:
Step 1: Start
Step 2: Read n value
Step 3: For i= 0 to n do
Step 3.1: Read xi[i], yi[i]
Step 3.2: Increment i by 1
Step 4: EndFor;
Step 5: Read x value at which value of y is to be calculated.
Step 6: For i = 0 to n do
Step 6.1: Set num = 1 and den = 1
Step 6.2: For j = 0 to n do
Step 6.2.1: If “j!=i”
Step 6.2.1.1: Compute num = num * (x - xi[j])
Step 6.2.1.2: Compute den = den * (xi[i] - xi[j])
Step 6.2.2: EndIf;
Step 6.3: EndFor;
Step 6.4: Compute y = y + (num/den) * yi[i]
Step 7: EndFor;
Step 8: Display “When x = ”, x, “y = ”, y
Step 9: Stop

Dept. of M.E., NITA Page 81


Numerical Analysis and Computer Programming

Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
main()
{
float xi[20],yi[20],num,den,x,y=0;
int i, j, n;
clrscr();
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the values of x and y: \n");
for(i=0;i<n;i++)
scanf("%f%f",&xi[i],&yi[i]);
printf("Enter value of x at which value of y is to be
calculated: ");
scanf("%f",&x);
for(i=0;i<n;i++)
{
num=1;

Dept. of M.E., NITA Page 82


Numerical Analysis and Computer Programming

den=1;
for(j=0;j<n;j++)
if(j!=i)
{
num *= x-xi[j];
den *= xi[i]-xi[j];
}
y+ = (num/den)*yi[i];
}
printf("When x=%4.1f y=%f\n",x,y);
getch();
}

(47) Write a program to find the square of given x2-25 by false-position method.
Algorithm:
Step 1: Start
Step 2: Read Initial approximate roots as x0 and x1
Step 3: Find f0 and f1 by passing the x0 and x1 values to the function fval(), respectively
Step 4: Check is f0*f1<0, if true goto step 4.1, otherwise goto step 4.3
Step 4.1: repeat until true (i.e., infinite loop)
Step 4.1.1: Calculate x2 i.e., x2=(xo*f1-x1*fo)/(f1-f0)
Step 4.1.2: Find f2 by passing the x2 value to the function fval()
Step 4.1.3: find the error i.e., e=|f2|
Step 4.1.4: Print stage,x0,x1,f0,f1,x2,f2,e
Step 4.1.5: Check is e<=eval, if true goto Step 4.1.5.1, otherwise goto Step 4.1.5.2
Step 4.1.5.1: Display “the solution converges at value” x2 “in” stage
“iterations”, goto step 5
Step 4.1.5.2: Check is f1*f2<0, if true goto Step 4.1.5.2.1, Otherwise goto
4.1.5.2.2
Step 4.1.5.2.1: set x1=x2 and f1=f2 goto step 4.1.6
Step 4.1.5.2.2: Check is f0*f2>0, if true goto Step 4.1.5.2.2.1,
Otherwise goto 4.1.5.2.2.2
Step 4.1.5.2.2.1: Check is f2==0, if true goto Step 4.1.5.2.2.1.1,
Otherwise goto 4.1.5.2.2.1.2
Step 4.1.5.2.2.1.1: goto step 4.1.5.1
Step 4.1.5.2.2.1.2: goto step 4.1.6
Step 4.1.5.2.2.2: set x0=x2 and f0=f2 goto step 4.1.6
Step 4.1.6: set stage=stage+1, goto step 4.1
Step 4.2: Display “Incorrect Initial Values”, goto step 5
Step 5: Stop

Function: fval(x)
Step 1: Begin
Step 2: return (x*x)-25
Step 3: End

Dept. of M.E., NITA Page 83


Numerical Analysis and Computer Programming

Flowchart:

Program:
#include<stdio.h>
#include<math.h>
float fval(float x);
main()
{
float x0,x1,x2,eval=0.001;
float f0,f1,f2,e;
int stage=1;
clrscr();

Dept. of M.E., NITA Page 84


Numerical Analysis and Computer Programming

printf("Enter the values where the root lies: ");


scanf("%f %f",&x0,&x1);
f0=fval(x0);
f1=fval(x1);
if(f0*f1<0)
{
printf("\nItr\tx0\tx1\tf0\tf1\tx2\tf2\te\n");
printf("---------------------------------------------\n");
while(1)
{
x2=(x0*f1-x1*f0)/(f1-f0);
f2=fval(x2);
e=fabs(f2);
printf("%d\t%.5f\t%.5f\t%.4f\t%.4f",
stage,x0,x1,f0,f1);
printf("\t%.5f\t%.4f\t%.f\n",x2,f2,e);
if(e<=eval)
break;
if(f0*f2<0)
{
x1=x2;
f1=f2;
}
else
if(f0*f2>0) {
x0=x2;
f0=f2;
}
else
if(f2==0)
break;
stage++;
}
}
else {
printf("Incorrect Initial Values.");
exit(0);
}
printf("\nRoot converges to a root %f in %d
iterations.",x2,stage);
getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}

Dept. of M.E., NITA Page 85


Numerical Analysis and Computer Programming

(48) Write a program to find the square of given x2-25=0 using bisection method.
Algorithm:
Step 1: Start
Step 2: Read initial approximate values x0,x1
Step 3: Substitute the values x0,x1 in the given function f(x) and calculate f(x0) and f(x1)
respectively
Step 4: If both f(x0) and f(x1) are of opposite signs go to step 5
Otherwise go to step 6
Step 4.1: repeat until true (infinite loop)
Step 4.1.1: Calculate the mean of x0, x1 ie., x2=(x0+x1)/2
Step 4.1.2: Substitute x2 in f(x) and find f(x2)
Step 4.1.3: if f(x2) has same sign as f(x1) then replace x1
with x2 go to step 4.1.6
Step 4.1.4: if f(x2) has same sign as f(x0) then replace x0
with x2 go to step 4.1.6
Step 4.1.5: if f(x2) is zero go to step 5
Step 4.1.6: end if
Step 5: Print x2 as root of the given function f(x)
Step 6: Stop

Flowchart:

Dept. of M.E., NITA Page 86


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<math.h>
float fval(float x);
main()
{
float x0,x1,x2,eval=0.001;
float f0,f1,f2,e;
int stage=1;
clrscr();
printf("Enter the values where the root lies: ");
scanf("%f %f",&x0,&x1);
f0=fval(x0);
f1=fval(x1);
if(f0*f1<0)
{
printf("\nItr\tx0\tx1\tf0\tf1\tx2\tf2\te\n");
printf("----------------------------------------------\n");
while(1)
{
x2=(x0+x1)/2;
f2=fval(x2);
e=fabs((x0-x1)/x1);
printf("%d\t%0.5f\t%0.5f\t%0.3f\t%0.3f",
stage,x0,x1,f0,f1);
printf("\t%0.5f\t%0.3f\t%f\n",x2,f2,e);
if(e<=eval)
break;
if(f0*f2<0) {
x1=x2;
f1=f2;
}
else if(f0*f2>0) {
x0=x2;
f0=f2;
}
else
if(f2==0)
break;
stage++;
}
}
else {
printf("Incorrect initial values");
exit(0);
}
printf("\nSolution converges to a root %f in %d
iterations.",x2,stage);

Dept. of M.E., NITA Page 87


Numerical Analysis and Computer Programming

getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}

(49) Write a program to implement Newton's Rapson method.


Algorithm:
Step 1: Start
Step 2: Read Initial approximate root as x0
Step 3: repeat until true (i.e., infinite loop)
Step 3.1: Find f0 by passing the x0 value to the function fval() and find f1 by
passing the x0 value to dfval()
Step 3.2: find x1=x0-(f0/f1)
Step 3.3: find e=|(x0-x1)/x0|
Step 3.4: display stage,xo,fo,f1,x1,e
Step 3.5: Check is e<=eval, if true goto Step 3.5.1, otherwise goto Step
Step 3.5.1: goto step 4
Step 3.5.2: set x0=x1 and stage=stage+1
Step 4: Stop
Function: fval(x)
Step 1: Begin
Step 2: return (x*x)-25
Step 3: End
Function: dfval(x)
Step 1: Begin
Step 2: return (2*x)
Step 3: End

Flowchart:

Dept. of M.E., NITA Page 88


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<math.h>
float fval(float x);
float dfval(float x);
main()
{
float x0,x1,eval=0.001;
float f0,f1,e;
int stage=1;
clrscr();
printf("Enter the initial value: ");
scanf("%f",&x0);
printf("\nItr\tx0\tf0\tf1\tx1\te\n");
printf("---------------------------------------------------\n");
while(1)
{
f0=fval(x0);
f1=dfval(x0);
x1=x0-(f0/f1);
e=fabs((x0-x1)/x1);
printf("\n%d\t%.4f\t%.3f\t%.3f\t%.4f",stage,x0,f0,f1,x1);
printf("\t%.4f\n",e);
if(e<=eval)
break;
x0=x1;
stage++;
}
printf("\nRoot of the equation is %.4f",x1);
getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}
float dfval(float x)
{
return (2*x);
}

(50) Write a program to implement Secant method.


Algorithm:
Step 1: Start
Step 2: Read Initial approximate roots as x0 and x1
Step 3: Find f0 and f1 by passing the x0 and x1 values to the function fval(), respectively
Step 4: Repeat until true (i.e., infinite loop)
Step 4.1: Calculate x2 i.e., x2=(xo*f1-x1*fo)/(f1-f0)
Step 4.2: Find f2 by passing the x2 value to the function fval()

Dept. of M.E., NITA Page 89


Numerical Analysis and Computer Programming

Step 4.3: find the error i.e., e=|f2|


Step 4.4: If "e<=eval OR f2==0"
Step 4.4.1: Goto Step
Step 4.5: Print stage, x0, x1, f0, f1, x2, f2, e
Step 4.6: Assign the value of x1 to x0 and x2 to x1
Step 4.7: Increment stage by 1
Step 4.8: Assign the value of f1 to f0 and f2 to f1
Step 4.9: Assign the value of x2 to rt
Step 5: EndRepeat;
Step 6: Display "Root of the equation is ", rt
Step 7: Stop

Function: fval(x)
Step 1: Begin
Step 2: return (x*x)-25
Step 3: End

Flowchart:

Dept. of M.E., NITA Page 90


Numerical Analysis and Computer Programming

Program:
#include<stdio.h>
#include<math.h>
float fval(float x);
main()
{
float x0,x1,x2,rt,eval=0.001;
float f0,f1,f2,e;
int stage=1;
clrscr();
printf("Enter the values where the root lies: ");
scanf("%f %f",&x0,&x1);
printf("\nItr\tx0\tx1\tf0\tf1\tx2\tf2\te\n");
printf("---------------------------------------------------\n");
f0=fval(x0);
f1=fval(x1);
while(1)
{
x2=(x0*f1-x1*f0)/(f1-f0);
f2=fval(x2);
e=fabs(f2);
if(e<=eval || f2==0)
break;
printf("\n%d\t%.4f\t%.3f\t%.3f\t%.4f",stage,x0,x1,f0,f1);
printf("\t%.4f\t%.4f\t%.4f\n",x2,f2,e);
x0=x1;
x1=x2;
stage++;
f0=f1;
f1=f2;
rt=x2;
}
printf("\nRoot of the equation is %.4f",rt);
getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}

(51) Write a program to get the integration of given function using Simpson's 1/3 rule.
Algorithm:
Step 1: Start
Step 2: Read x0, xn, n
Step 3: If “n%2==0”
Step 3.1: Compute h=(xn-x0)/n
Step 3.2: Compute sum of return value of y(x0), y(xn), 4*y(x0+h) from function f(x)
and store the result in s

Dept. of M.E., NITA Page 91


Numerical Analysis and Computer Programming

Step 3.3: For i=3 to n-1


Step 3.3.1: Compute sum of s value and return value of
h*[y(x0+i*h) + 2*y(x0+(i-1)*h)]
Step 3.3.2: Increment i by 2
Step 3.4: EndFor;
Step 3.5: Display (h/3)*s value
Step 4: Else
Step 4.1: Print “The Rule is not applicable”
Step 5: EndIf;
Step 6: Stop

Begin y(x)
Return 1/(1+x)
End

Flowchart:

Program:
#include<stdio.h>
#include<math.h>
double y(double x)
{
return 1/(1+x);

Dept. of M.E.,NITA Page 92

You might also like