0% found this document useful (0 votes)
46 views34 pages

POP Lab Manual 2023 24

Uploaded by

kalyanjonna586
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)
46 views34 pages

POP Lab Manual 2023 24

Uploaded by

kalyanjonna586
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/ 34

BPOPS103-Principles of Programming using C 1

1. Simulation of a Simple Calculator

Algorithm: -

PURPOSE: To simulate a calculator using arithmetic expressions


INPUT: Enter num1 andnum2
OUTPUT: Print the result of addition or subtraction or multiplication or division or modulus.
START

Step 1: [Enter first number]


read num1
Step 2: [Enter Second number]
read num2
Step 3: [Enter Choice]
read choice
Step 4: [To perform addition]
if choiceis equal to plus add num1 and num2 print result
Step 5: [To perform subtraction]
if choiceis equal to minus subtract n u m 2 fromnum1 print result
Step 6: [To perform multiplication]
if choiceis equal to multiplication multiplynum1 and num2
printresult
Step 7: [To perform division]
if choiceis equal to division
dividenum1 bynum2 printresult
Step 8: [To perform Modulus]
if choiceis equal to modulus dividenum1 bynum2
printresult(remainder)

STOP

DEPT OF CSE, RYMEC, BALLARI 1


BPOPS103-Principles of Programming using C 2

/* A Program to solve simple computational problems using arithmetic expressions */


#include<stdio.h>
#include<conio.h>
void main ( )
{
float num1, num2, div, result;
char option;
clrscr();
printf ("Enter the first Integer:\n");
scanf ("%f”, &num1);
printf ("Enter the second Integer:\n");
scanf ("%f”, &num2);
printf ("Enter the your option:\n");
printf (“’+’ - Addition\n ‘-‘ - Subtraction\n ‘*’ -Multiplication\n ‘/’- Division\n ");
scanf (" %c", &option);
switch(option)
{
case ‘+’:printf (“Option selected is Addition \n”);
result=num1+num2;
printf ("\n Addition of %f and %f is: %f”, num1, num2, result);
break;
case ‘- ‘:printf (“Option selected is Substraction \n”);
result=num1-num2;
printf ("\n Subtraction of %f and %f is: %f", num1, num2, result);
break;
case ‘*’:printf (“Option selected is Multiplication \n”);
result=num1*num2;
printf ("\n Multiplicationof %f and %f is: %f", num1, num2, result);
break;
case ‘/’:if(num2==0)
{
printf ("Error in input since number1 is not divide by zero\n");
}
else
{
printf (“Option selected is division \n”);
div=num1/num2;
printf ("\nDivision of %f and %f is: %f", num1, num2, div);
}
break;
default:printf ("\n Invalid option entry\n");
}
getch();
}

DEPT OF CSE, RYMEC, BALLARI 2


BPOPS103-Principles of Programming using C 3

/* Flow chart to solve simple computational problems using arithmetic expressions */

Start

Read num1, num2

Read option

if
(option=?)

result=num1+num2

result=num1-num2

result=num1*num2

if(num2==0)
number1 is not divide by
zero

div=num1/num2

Invalid option entry

Result

Stop

DEPT OF CSE, RYMEC, BALLARI 3


BPOPS103-Principles of Programming using C 4

2. Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
Algorithm:
Step 1: Start
Step 2: Read a,b, c //Read Co-efficient
Step 3: if a=0, b=0, c=0
Write Invalid Input
Step 4: db*b-4a*c //calculate disc
Step 5: if d=0 //Roots are equal
Write Roots are equal
root1  -b/(2*a);
root2 -b/(2*a);
write root1, root2
[ Goto Step 8]
Write Roots are Real and Distinct
root1 -b +sqrt (d)/ (2*a);
root2 -b -sqrt (d)/ (2*a);
write root1, root2
[ Goto Step 8 ]
Step 7: if d<0 // Roots are imaginary
WriteRoots are imaginary
Real -b/(2*a);
ImaginarySqrt (fabs (d))/(2*a);
write root1, root2
Step 8: Stop

DEPT OF CSE, RYMEC, BALLARI 4


BPOPS103-Principles of Programming using C 5

/* A Program to find different roots of Quadratic Equation*/


#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float a,b,c,d,real, imaginary, root1,root2;
clrscr();
printf ("enter the value of a,b,c");
scanf ("%f%f%f”, &a,&b,&c);
if(a==0||b==0||c==0)
{
printf ("invalid input\n");
getch();
exit (0);
}
d=b*b-4*a*c;
printf (“d=%f\n”,d);
if(d==0)
{
printf ("roots are equal roots\n");
root1=-b/(2*a);
root2=-b/(2*a);
printf ("root1=%f\n”, root1);
printf ("root2=%f\n”, root2);
}
else if(d>0)
{
printf ("roots are real and distinct\n");
root1= (-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf ("root1=%f\n”, root1);
printf ("root2=%f\n”, root2);
}
else
{
printf ("roots are real & imaginary\n");
real=-b/(2*a);
imaginary=sqrt(fabs(d))/(2*a);
printf ("root1=%f+i%f\n",real, imaginary);
printf ("root2=%f-i%f\n",real,imaginary);
}
getch();
}

DEPT OF CSE, RYMEC, BALLARI 5


BPOPS103-Principles of Programming using C 6

/* Flow Chart to find different roots of Quadratic Equation */

Start

Read a,b,c

if (a==0||b==0||
c==0)

Write Invalid Input

d=b*b-4*a*c
A

if(d==0)

Write roots are Equal


if(d>0)

root1 -b/2a
Write roots are Img Write roots are Real root2 -b/2a

real -b(2*a) root1 -b+sqrt(d)/2*a Write root1, root2


img root2 -b-sqrt(d)/2*a
sqrt(fabs(d))/2*a

Write root1, root2 Write root1, root2

A
Stop

DEPT OF CSE, RYMEC, BALLARI 6


BPOPS103-Principles of Programming using C 7

3. An electricity board charges the following rates for the use of electricity: for the first 200
units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1
per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount
is more than Rs 400, then an additional surcharge of 15% of total amount is charged.
Write a program to read the name of the user, number of units consumed and print out
the charges.

ALGORITHM
PURPOSE: Read the Name of The User, Number of Units Consumed and Print
INPUT: name [10], unit
OUTPUT: Print the charges for total number of units consumed

START
STEP 1: [Input a name and units]
read name and unit
STEP 2: [Initialization] Metercharge=100
STEP3: [To check electricity unit is less than or equal to 200 and calculate metercharge]
If unit less than or equal to 200
metercharge= metercharge+(unit*.80)
STEP 4: [Else check unit is greater than 200 and greater than 300 and calculate metercharge]
If unit greater than 200 and unit greater than or equal to 300
metercharge= metercharge+(200*0.80)+((unit-200) *0.90))
STEP 5: [Else check unit is greater than 300 and calculate metercharge]
If unit is greater than 300
metercharge= metercharge+(200*0.80)+(300*0.90)+((unit-300)*1))
STEP 6: [To check and calculate if meter charge is greater than 400]
If metercharge greater than or equal to 400
metercharge=metercharge+(metercharge*0.15);
STEP 7: [Finished]
STOP

DEPT OF CSE, RYMEC, BALLARI 7


BPOPS103-Principles of Programming using C 8

/*A program to read the name of the user, number of units consumed & print out the charges*/

#include<stdio.h>
#include<conio.h>
void main()
{
int units, minimum=100;
float amount;
char name[20];
clrscr();
printf("Enter user name\n");
scanf("%s", name);
printf("Enter number of consumed units\n");
scanf("%d", &units);
if(units<=200)
{
amount=units * 0.8 + minimum;
}
else if(units>200 && units<=300)
{
amount=units * 0.9 + minimum;
}
else if(units>300)
{
amount=units*1.0 + minimum;
}
if(amount>400)
{
amount=amount + amount*0.15;
}
printf("Electricity Bill Details...............\n”);
printf (“User Name: %s\n", name);
printf ("Number of Units Consumed: %d\n”, units);
printf ("Amount is charged: Rs. %f", amount);
getch ();

DEPT OF CSE, RYMEC, BALLARI 8


BPOPS103-Principles of Programming using C 9

/* Flow Chart to read the name of the user, number of units consumed & print out the charges*/

DEPT OF CSE, RYMEC, BALLARI 9


BPOPS103-Principles of Programming using C 10

4. Write a C Program to display the following by reading the number of rows as input.
1
121
12321
1234321
---------------------------
nth row

/ A C Program to display the pattern */


#include<stdio.h>
#include<stdio.h>
void main()
{
int i,j,k,l,n;
clrscr();
printf("enter the number of rows\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf(" %d",k);
}
for(l=i-1;l>0;l--)
{
printf(" %d",l);
}
printf("\n");
}
getch();
}

DEPT OF CSE, RYMEC, BALLARI 10


BPOPS103-Principles of Programming using C 11

5. Implement Binary Search on Integers


Algorithm:
Step 1: Start
Step 2: readn
for i=0 to i<n [increment by 1]
read  a[i]
end for
read  key
end for
Step 3: low 0
high n-1
found=1
Step 4:while (low<=high &&! found)
mid=low+high/2
if(strcmp(arr[mid], key) ==0)
found=1
else if(strcmp(arr[mid], key) <0)
else
high=mid-1
end while
if(found==1)
writesearch successful
else
writeunsuccessful search
Step 5: Stop

DEPT OF CSE, RYMEC, BALLARI 11


BPOPS103-Principles of Programming using C 12

/* A Program to implement Binary search */


#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, low, high, mid, key, arr [15];
clrscr( );
printf(“enter no of elements you have\n”);
scanf(“%d”, &n);
printf(“enter the elements”);
for(i=0; i<n; i++)
scanf(“%d”, &arr[i]);
printf(“enter key element\n”);
scanf(“%d”, &key);
low=0;
high=n-1;
while(low<=high)
{
mid= (low+high)/2;
if (arr[mid] == key)
{
printf ("%d found at location %d \n", key, mid+1);
getch ();
exit (0);
}
else if (arr[mid] <key)
low = mid + 1;
else
high = mid - 1;
}
printf ("key element is not found\n");
getch ();
}

DEPT OF CSE, RYMEC, BALLARI 12


BPOPS103-Principles of Programming using C 13

/* Flow chart to implement Binary search */

Terminator
Start

Read n

i=0 F
i++
i<n

Read arr[i]

i=0 F
i++
i<n

Print arr[i]

Read key

Initialize
low = 0
high = n-1

DEPT OF CSE, RYMEC, BALLARI 13


BPOPS103-Principles of Programming using C 14

while F
low <= high

mid = (low+high)/2

F
(cond = strcmp(arr[mid], key)) == 0

Print "key is found"

F
cond < 0

T
low = mid + 1

high = mid - 1

Print "key is NOT found"

B Stop

DEPT OF CSE, RYMEC, BALLARI 14


BPOPS103-Principles of Programming using C 15

6. Implement Matrix multiplication and validate the rules of multiplication


Algorithm:

Step 1:Start Write matrix B.


Step 2: [Read sizes of the matrices] for i =0 to p
read  m, n. for j = 0 to q
read  p, q. writeb [ i ] [ j ]
Step 3:[ Check possibilities of multiplication ] end for j.
if ( n ! = p ) end for i.
Not possibility of matrix multiplication. write matrix c.
end if. for i =0 to m.
Goto step 6. for j = 0 to q
Step 4:[ Read the matrices] writec [ i ] [ j ]
read matrix A. end for j.
for i =0 to m end for i.
for j = 0 to n Step 7: stop.
read  a [ i ] [ j ].
end for j.
end for i.
read matrix B.
for i =0 to p.
for j = 0 to q
read  b [ i ] [ j ].
end for j.
end for i.
Step 5:[ compute matrices multiplication ]
for I =0 to m
for j = 0 to q
C [ i ][ j ] = 0
for k =0 to n.
c [ i ] [ j ] = c [ i ] [ j] + a [ i ] [ k ] * b [ k ]
[ j ].
end for k.
end for j.
end for i.
Step 6: [ Write matrices ]
Write matrix A.
for i =0 to m
for j = 0 to n
writea [ i ] [ j ]
end for j.
end for i.

DEPT OF CSE, RYMEC, BALLARI 15


BPOPS103-Principles of Programming using C 16

/* Flow chart to implement Matrix multiplication */


B
Start
i=0 F
i++
Read size of matrix A i<m
(m & n) T
j=0 F
j++
Read size of matrix B j<q
(p & q) T

F
c[i][j] = 0
n != p
k=0 F
T k++
k<n
Print "Product cannot T
be computed"
c[i][j]=c[i][j]+a[i][k]*b[k][j]
A

i=0 F
i++
i<m
T
i=0 F
j=0 F i++
j++ i<m
j<n
T
T
Read a[i][j] j=0 F
j++
j<n
T
Print a[i][j]
i=0 F
i++ B
i<p
T
j=0 F
j++
j<q C
T
Read b[i][j]

DEPT OF CSE, RYMEC, BALLARI 16


BPOPS103-Principles of Programming using C 17

i=0 F
i++
i<p
T
j=0 F
j++
j<q
T
Print b[i][j]

i=0 F
i++
i<m
T
j=0 F
j++
j<q
T
Print c[i][j]

A Stop

DEPT OF CSE, RYMEC, BALLARI 17


BPOPS103-Principles of Programming using C 18

/* A Program toimplement Matrix multiplication */

#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,k,m,n,p,q,a [10][10],b [10][10],c [10][10];
clrscr( );
printf ("enter the size of matrix A\n");
scanf ("%d %d", &m, &n);
printf ("enter the size of matrix B\n");
scanf("%d %d",&p,&q);

if(n! =p)
{
printf("the product cannot be computed");
getch( );
exit(0);
}

printf("enter the elements of matrix A\n");


for(i=0;i<m; i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf ("enter the elements of matrix B\n");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);

for(i=0;i<m;i++)
{
For(j=0;j<q;j++)
{
c[i][j] =0;

DEPT OF CSE, RYMEC, BALLARI 18


BPOPS103-Principles of Programming using C 19

for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

printf("\n matrix A is\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d \t",a[i][j]);
printf("\n");
}

printf("\n matrix B is\n");


for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t ",b[i][j]);
printf("\n");
}

printf("\n Resultant matrix C is\n");


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d \t",c[i][j]);
printf("\n");
}
getch( );
}

DEPT OF CSE, RYMEC, BALLARI 19


BPOPS103-Principles of Programming using C 20

7.Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
Algorithm:
Step 1: Start
Step 2:readn , degree // Read number of terms and x value in degree
Step 3: xdegree*3.142/180 //Convert x into radius.
termx
sumterm
Step 4: //Calculate Sin series
for i=3 to n [ increment by 2 ]
term -term*x *x/(i*(i-1))
sumsum+term
end for
Step 5://compare results
Write sum
Write sin(x )
Step 6: Stop

DEPT OF CSE, RYMEC, BALLARI 20


BPOPS103-Principles of Programming using C 21

/*Flow Chart to compute Sin(x) using Taylor series */

Start

Read n, degree

x degree*3.142/180
term x
sum term

i=3 F
i=i+2
i<=n

term -term*x*x/i*(i-1)
sum term+sum

Write sum, sin(x)

Stop

DEPT OF CSE, RYMEC, BALLARI 21


BPOPS103-Principles of Programming using C 22

/*A Program to compute Sin(x) using Taylor series */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float term,degree,x,sum;
int i,n;
clrscr( );
printf("\n enter the value in n\n");
scanf("%d",&n);
printf("\n enter the value in degree\n");
scanf("%f",&degree);
x=degree*3.142/180;
term=x;
sum=term;
for(i=3;i<=n;i=i+2)
{
term=-term*x*x/(i *(i-1));
sum=sum+term;
}
printf("the User define sin(x) value is=%f\n",sum);
printf("the built-in sin(x) value is=%f\n",sin(x));
getch( );
}

DEPT OF CSE, RYMEC, BALLARI 22


BPOPS103-Principles of Programming using C 23

8. Sort the given set of N numbers using Bubble sort.


Algorithm:
Step 1:Start
Step 2: [ Read Inputs]
read  n
for i =0 to n
read  a [ i ]
end for
Step 3: [given array]
for i =0 to n
Write a [ i ] .
end for
Step 4:[ sort the given array]
for i = 0 to n
for j = n-1 to i
if (a [j] < a [j-1])
exchange ( a[j],a[j-1])
end if
end for j
end for i
Step 5: [ sorted array ]
for i = 0 to n
writea [i]
end for
Step 6: Stop.

DEPT OF CSE, RYMEC, BALLARI 23


BPOPS103-Principles of Programming using C 24

/*Flow Chart to sort the give set of N numbers using Bubble Sort*/

Start

Read n
A

i=0 F
i++
i<n
Print "Sorted Array is"
T

Read a[i]

i=0 F
i=0 F i++
i++
i<n i<n

T T
print a[i]
print a[i]

i=0 F
i++
i<n
Stop
T
A
j=n-1 F
j--
j>i
T

F
a[j] < a[j-1]

temp= a[j]
a[j] = a[j-1]
a[j-1] = a[j]

DEPT OF CSE, RYMEC, BALLARI 24


BPOPS103-Principles of Programming using C 25

/*A Program to sort the give set of N numbers using Bubble Sort */

#include<stdio.h>
#include<conio.h>
void main()
{
int a [10], n,i, j, temp;
clrscr();
printf(“enter the size of an array\n”);
scanf(“%d”,&n);
printf(“\n enter the elements of an array\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
for(j=n-1;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf(“\n the elements of a sorted array are\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}

DEPT OF CSE, RYMEC, BALLARI 25


BPOPS103-Principles of Programming using C 26

9.Write functions to implement string operations such as compare, concatenate, and find string
length. Use the parameter passing techniques.
/*A Program to implement string operations such as compare, concatenate, string length*/
#include<stdio.h>
#include<conio.h>
int strlength(char s [50]);
int strcompare(char s1[50],char s2[50]);
void strconcatenate(char s1[50],char s2[50]);
void main()
{
char str1[150],str2[150];
int len;
clrscr();
printf("Enter two string\n");
gets(str1);
gets(str2);
len=strlength(str1);
printf("String length of first string=%d\n",len);

len=strlength(str2);
printf("String length of second string=%d\n",len);
strconcatenate(str1,str2);
if(strcompare(str1,str2)==0)
printf("\nCompared Strings are same");
else
printf("\nCompared Strings are not same");
getch();
}

int strlength(char s[50])


{
int i=0,length=0;
while(s[i]! ='\0')
{
i++;
length++;
}
return(length);
}

DEPT OF CSE, RYMEC, BALLARI 26


BPOPS103-Principles of Programming using C 27

int strcompare(char s1[50],char s2[50])


{
int i=0;
while(s1[i]! ='\0' && s2[i]! ='\0')
{
if(s1[i]==s2[i])
i++;

else
return 1;
}
return 0;
}
void strconcatenate(char s1[50],char s2[50])
{
int i=0;
int l=strlength(s1);

while(s2[i]! ='\0')
{
s1[l]=s2[i];
i++;
l++;
}
s1[l]='\0';
printf("\nConcatenation of strings are %s",s1);

DEPT OF CSE, RYMEC, BALLARI 27


BPOPS103-Principles of Programming using C 28

10. Implement structures to read, write, compute average- marks and the students scoring
above and below the average marks for a class of N students.

/*A Program to Implement structures to read, write, compute average- marks and the students
scoring above and below the average marks for a class of N students*/

#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rollno;
char name [20];
int marks;
}s[100];
int i,n;
float avg=0;
clrscr();
printf(“enter the N number of students\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n student %d details are:\n”,i+1);
printf(“\enter the roll no\n”);
scanf(“%d”,&s[i]. rollno);
printf(“enter the name \n”);
scanf(“%s”, s[i].name);
printf(“Marks of the student:\n”);
scanf(“%d”,&s[i]. marks);
avg=avg+s[i]. marks;
}
avg=avg/n;
printf(“\n the average marks for the class is %f:”,avg);

DEPT OF CSE, RYMEC, BALLARI 28


BPOPS103-Principles of Programming using C 29

for(i=0;i<n;i++)
{
printf(“\n student %d details are:”,i+1);
printf(“\n Roll no of the student:\n”);
printf(“%d”,s[i].rollno);
printf(“\n Name of the student:\n”);
printf(“%s”, s[i].name);
printf(“Marks of the student:\n”);
printf(“%d”,s[i].marks);
if(s[i].marks> avg)
printf(“ The student has scored above average Marks\n”);
else
printf(“ The student has scored below average Marks\n”);
}
getch();
}

DEPT OF CSE, RYMEC, BALLARI 29


BPOPS103-Principles of Programming using C 30

11. Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.

Algorithm:
Step 1: Start
Step 2: read n //number of elements.
Step 3: readx[i]
for i 0 to n [increment by 1 ]
read x[i]
end for
Step 4: assign array to pointer
ptr=x
Step 5: for i<0 to n
Sumsum + *ptr
End for
Step 6: calculate mean
meansum/n
Step 7: for i=0 to n [increment by 1 ]
Stddiv_sumstddivsum+ pow((*ptr-mean),2)
End for
Step 8: stddiv sqrt(stddiv_sum/n)
Step 9: writesum
writemean
writestddiv
Step 10:Stop

DEPT OF CSE, RYMEC, BALLARI 30


BPOPS103-Principles of Programming using C 31

/*A Program to using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers*/

#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float *ptr, average, variance, stddev, sum = 0, sum1 = 0;
printf("Enter the value of N \n");
scanf("%d", &n)
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
ptr = x;

/* Compute the sum of all elements */


for (i = 0; i < n; i++)
{
sum = sum + *ptr;
ptr++;
}
average = sum / n;
ptr = x;
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow ((*ptr - average), 2);
ptr++;
}

DEPT OF CSE, RYMEC, BALLARI 31


BPOPS103-Principles of Programming using C 32

variance = sum1 / n;
stddev= sqrt(variance);
printf(“\n Sum of all the numbers=%f\n”,sum);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", stddev);
getch();
}

DEPT OF CSE, RYMEC, BALLARI 32


BPOPS103-Principles of Programming using C 33

12. Write a C program to copy a text file to another, read both the input file name and target file
name.

Algorithm :
Step 1: Start
Step 2: FILE *fp1 , *fp2 ,*fp3
fp1 fopen(“name.text”,”r”)
fp2 fopen(“USN.text”,”r”)
fp3 fopen(“output.text”,”w”)
Step 3:if (fp1=NULL or fp2== NULL)
Write unable to open a file
Goto step 5
Step4: printf("students name \t\t USN \n");
fprintf(fp3,"STUDENT NAME\t USN\n");
Step 5:while(fgets(s1 , 80 ,fp1)!==NULL and fgets(s2 , 80 ,fp2)!==NULL))
fprintf(fp3,"%s\t%s\n",s1,s2);
printf("%s\t %s\n",s1,s2);

Step 6:Stop

DEPT OF CSE, RYMEC, BALLARI 33


BPOPS103-Principles of Programming using C 34

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char s1[80],s2[80];
clrscr();
fp1=fopen("name.txt","r");
fp2=fopen("usn.txt","r");
fp3=fopen("output1.txt","w");
if(fp1==NULL||fp2==NULL)
{
printf("unable to open");
getch();
exit(0);
}
printf("students name \t\t USN \n");
fprintf(fp3,"STUDENT NAME\t\t USN\n");
while((fscanf(fp1,"%s",s1)!=EOF)&&(fscanf(fp2,"%s",s2)!=EOF))
{
fprintf(fp3,"%s\t%s\n",s1,s2);
printf("%s\t %s\n",s1,s2);
}
fcloseall();
getch();
}

DEPT OF CSE, RYMEC, BALLARI 34

You might also like