NACP Lab Manual
NACP Lab Manual
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
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
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.
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}
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
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");
}
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();
}
Program:
#include<stdio.h>
main()
{
int a,b,c,d,e,sum,avg;
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);
}
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);
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;
Program:
#include<stdio.h>
main()
{
int a,b,c;
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)
{
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
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");
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;
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 */
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;
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]);
}
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:
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;
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
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();
}
Flowchart:
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;
Flowchart:
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];
}
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;
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");
(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
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);
}
}
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);
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
main()
{
(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;
Flowchart:
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;
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)
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();
}
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;
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
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();
(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:
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);
getch();
}
float fval(float x)
{
return (pow(x,2)-25);
}
Flowchart:
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);
}
Function: fval(x)
Step 1: Begin
Step 2: return (x*x)-25
Step 3: End
Flowchart:
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
Begin y(x)
Return 1/(1+x)
End
Flowchart:
Program:
#include<stdio.h>
#include<math.h>
double y(double x)
{
return 1/(1+x);