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

CP lab mannual 2022-23

The document outlines several programming tasks, including finding factorials, roots of quadratic equations, determining prime numbers, generating Fibonacci series, constructing pyramids of numbers, calculating LCM and nCr values, and generating prime numbers using the Sieve of Eratosthenes algorithm. Each task includes an aim, problem description, algorithm, program code, and input/output examples. The programs are written in C and demonstrate various programming concepts and techniques.

Uploaded by

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

CP lab mannual 2022-23

The document outlines several programming tasks, including finding factorials, roots of quadratic equations, determining prime numbers, generating Fibonacci series, constructing pyramids of numbers, calculating LCM and nCr values, and generating prime numbers using the Sieve of Eratosthenes algorithm. Each task includes an aim, problem description, algorithm, program code, and input/output examples. The programs are written in C and demonstrate various programming concepts and techniques.

Uploaded by

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

WEEK-1:

PROGRAM : 1(A)
1.AIM :
Program that use non recursive function to find the factorial of a given integer.
2.PROBLEM DESCRIPTION:
Factorial of a number is nothing but the multiplication of numbers from a given number to 1
Ex: 5! =5*4*3*2*1= 120
3. ALGORITHM:
main program
Step 1: start
Step 2: read n
Step 3: call the sub program fact(n)
Step 4: print the f value
Step 5: stop
Sub program:
Step 1: initialize the f=1
Step 2: if n==0 or n=1 return 1 to main program. If not goto step 3
Step 3: perform the looping operation as follows
For i=1 i<=n; i++
Step 4: f=f*i
Step 5: return f value to the main program

4. PROGRAM:
#include<stdio.h>
int fact(int n) //starting of the sub program
{
int f=1,i;
if((n==0)||(n==1)) // check the condition for n value
return(1);
else
for(i=1;i<=n;i++) // perform the looping operation for calculating the factorial
f=f*i;
return(f);
}
void main()
{
int n;
printf("enter the number :");
scanf("%d",&n);
printf("factorial of number%d",fact(n));
}
5. INPUT/OUTPUT:
1.Enter the number: 7
Factorial of number: 5040
1
2. Enter the number: 6
Factorial of number: 720
3. Enter the number: 8
Factorial of number: -25216
PROGRAM:1(B)
1.AIM :
To find the roots of the quadratic equation
2.PROBLEM DESCRIPTION:
Nature of roots of quadratic equation can be known from the quadrant  = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary
3. ALGORITHM:
Step 1: start
Step 2: read the a,b,c value
Step 3: if b*b-4ac>0 then
Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a
Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a
Step 4: if b*b-4ac=0 then
Root1 = Root2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots. Goto step 7.
Step 6: print roots
Step 7: stop
4. PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
/* check the condition */
if(a==0)
printf("Enter value should not be zero ");
else
{
d=b*b-4*a*c;
/* check the condition */
if(d>0)
{
r1=(-b+sqrt(d)/(2*a));
r2=(-b-sqrt(d)/(2*a));
printf("roots are real and unequal\n");

2
printf("%f\n%f\n",r1,r2);
}
else
if(d==0)
{
r1=(float)-b/(2*a);
r2=(float)-b/(2*a);
printf("roots are real and equal\n");
printf("root=%f\n",r1);
printf("root=%f\n",r2);
}
else
printf("roots are imaginary");
}
}
5. INPUT/OUTPUT:
1. Enter the values for equation: 1, 6, 9
Roots are real and equal
Root= -3.0000
Root= -3.0000
2. Enter the values for equation: 2, 7, 6
Roots are real and unequal
Root= -6.75
Root= -7.25
3. Enter the values for equation: 1, 2, 3
Roots are imaginary

3
WEEK:2
PROGRAM :2(A)
1.AIM :
To determine if the given number is a prime number or not
2.PROBLEM DESCRIPTION:
Prime number is a number which is exactly divisible by one and itself only
Ex: 2, 3,5,7,………;
3. ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize c=0
Step 5: initialize j=1
Step 6: while j<=n do
if n%j==0 increment c
j=j+1
Step 7: if c== 2 print “number is prime”
Step 8: else print “number is not prime”
Step 9: stop

4. PROGRAM:
#include<stdio.h>
void main()
{
int n,fact,j;
printf("enter the number:");
scanf("%d",&n);
fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j=1;j<=n;j++)
{
if(n%j==0)
fact++;
}
if(fact==2)
printf("\n given number is prime");
else
printf(“\n given number not is prime”);
}
5. INPUT/OUTPUT:
Enter the number : 5
Given number is prime
Enter the number : 10
Given number is not prime

4
PROGRAM:2(B)
1.AIM :
To print the Fibonacci series of n terms as follows.
0,1,1,2,3,5,8,13, . . , n terms
2.PROBLEM DESCRIPTION:
A fibonacci series is defined as follows
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1,t2,…………tn be terms in fibinacci sequence
t1=0, t2=1
tn=tn-2+tn-1……where n>2
3. ALGORITHM:
Step 1: start
Step 2: initialize the a=0, b=1
Step 3: read n
Step 4: if n== 1 print a go to step 7. else goto step 5
Step 5: if n== 2 print a, b go to step 7 else print a,b
Step 6: initialize i=3
Step 7: if i<= n do as follows. If not goto step 7
c=a+b
print c
a=b
b=c
increment i value
goto step 7
Step 8: stop

4. PROGRAM:
#include<stdio.h>
void main()
{
int a,b,c,n,i;
printf("enter n value");
scanf("%d",&n);
a=0;
b=1;
if(n==1)
printf("%d",a);
else
if(n==2)
printf("%d%d",a,b);
else
{

5
printf("%d%d",a,b);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN
ADVANCE
for(i=2;i<n;i++)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
}
}
}
5. INPUT/OUTPUT:
1. Enter n value : 5
01 1 2 3
2. Enter n value : 7
0 1 1 2 3 5 8
3. Enter n value : -6
0 1

6
WEEK: 3
PROGRAM 3(A)
1.AIM :
To construct a pyramid of numbers
2.PROBLEM DESCRIPTION:
In this program the we have to construct output in the pyramid shape manner
3. ALGORITHM:
Step 1: Start
Step2: initialize the num,I,y, x=35
Step3: read the num
Step4:perform the loop operation
For(y=0;y<=num;y++)
Step5:Gotoxy(x,y+1)
Step6: perform the loop operation for displaying digits towards the left and right
For(i=0-y;i<=y;i++)
Step7: print abs(i);
Step8: x=x-2;
Step9: Stop

4. PROGRAM:
#include<stdio.h>
void main()
{
int num,i,y,x=45;
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);

for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);

/*for displaying digits towards the left and right of zero*/


for(i=0-y;i<=y;i++)

printf("%3d",abs(i));
x=x-3;
}
}

5. INPUT/OUTPUT:
1.enter the number: 0
4 101
21012
7
3210123
432101234
2.enter the number: 0
3 101
21012
3210123

PROGRAM:3(B)

1.AIM :
To calculate the sum. Sum=1-x2/2!+ x4/4!- x6/6!+ x8/8!- x10/10!

2.PROBLEM DESCRIPTION:
This program finds sum of 1 , -x2/2! , x4/4! , - x6/6! , x8/8! and - x10/10!

3. ALGORITHM:
main program:
Step 1: start
Step 2: declare x,i,n,s=0,c
Step 3: read x value
Step 4: for i=0 , n=0; i<=10; i=i+2, n++ goto step 5
Step 5: s=s+(pow(-1,n)*pow(x,i)/fact(i))
Step 6: print s value
Step 7: stop

Sub program:
Step 1: while x!=0 goto Step 2
Step 2: y=y+x; x—
Step 3: return y
Step 4: return to main program

4. PROGRAM:
#include<stdio.h>
#include<math.h>
long fact(int);
void main()
{
int x,i,n;
float s=0,c;
printf("\n enter the value of x\t");
scanf("%d",&x);
/*perform the looping operation*/
for(i=0,n=0;i<=10;i=i+2,n++)
s=s+(pow(-1,n)*pow(x,i)/fact(i));

8
printf("\n the result is %f",s);
}
/* calling sub program*/
long fact(int x)
{
long int y=1;
while(x!=0)
{
y=y*x;
x--;
}
return y;
}

5. INPUT/OUTPUT:
1.Enter the value of x : 1
The result is 0.540302
2 Enter the value of x: 2
The result is -0.416155

9
WEEK:4
PROGRAM: 4(A)
1.AIM :To find the LCM of two given integers
2.PROBLEM DESCRIPTION:
LCM means least common multiple. i.e is the smallest integer that is evenly divisible by both a
and b.The lcm(a, b) function should calculate the least common multiple by calling the gcd
(a,b) function and using the following relation:
LCM (a,b) = ab / gcd (a,b)
3. ALGORITHM:
Main program:
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the (a*b)/g
Step 5: stop
Sub program:
Step 1: while p!=q do
Step 2: begin
Step 3: if( p>q) then p=p-q else q=q-p
Step 4: end
Step 5: return p
4. PROGRAM:
#include<stdio.h>
#include<math.h>
int gcdnonrecursive(int m,int n)
{
while(m!=n){
if( m<n)
n=n-m;
else
m=m-n;
} return n;
}
void main()
{ int a,b, gcd,lcm;
printf("enter the two numbers whose lcm is to be found:");
scanf("%d%d",&a,&b);
gcd= gcdnonrecursive(a,b);
printf("GCD of %d and %d is %d\n",a,b,gcd);
lcm=(a*b)/gcd;
printf("LCM of %d and %d is %d",a,b,lcm);
}
5. INPUT/OUTPUT:
enter the two numbers whose lcm is to be found:5 25
10
GCD of 5 and 25 is 5
LCM of 5 and 25 is 25
PROGRAM: 4(B)
1.AIM :To compute the ncr value
2.PROBLEM DESCRIPTION:
the ncr value is computed using the following relation:
ncr (n,r) = n! / r! (n-r)! . Use a function for computing the factorial value of an integer.
3. ALGORITHM:
main program
Step 1: start
Step 2: read n,r
Step 3: r=fact(n)/(fact(r)*(fact(n-r))
Step 4: write r
Step 5: stop
Sub program:
Step 1: f=1
Step 2: while x!=0 do
Step 3: begin
Step 4: f=f*x
Step 5: x=x-1
Step 6: end
Step 7: return f
4. PROGRAM:
#include<stdio.h>
int fact(int x) // starting of the sub program
{
int f;
f=1;
while(x!=0){
f=f*x;
--x;
}
return f;
}
void main()
{
int n,r,ncr;
printf("enter n and r:");
scanf("%d%d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf(“ncr is %d”,ncr);
}
5. INPUT/OUTPUT:
enter n and r: 4 3

11
Ncr is 4

WEEK :6
PROGRAM :6(A)
1.AIM :
To generate all the prime numbers between 1 and n, where n is a value supplied by the user
using Sieve of Eratosthenes algorithm
2.PROBLEM DESCRIPTION:
Sieve of Eratosthenes algorithm is used to print prime numbers. The basic idea is as follows:
1. Create a list with all positive integers (starting from 2 as 1 is not considered prime).
2. Start at the first valid number (at this point all are valid) and eliminate all its multiples
from the list. So start with 2, and eliminate 4, 6, 8, 10, 12 and so on.
3. Once all the multiples are eliminated advance to the next valid number (one that has not
been eliminated) and repeat the process, until there are no more valid numbers to
advance to.
3. ALGORITHM:

Step 1. start
Step 2. Read n
Step 3. Call sieve(n, v)
Step 4. For i=0 to n do
Step 5. begin
Step 6. if v[i] == 1 then print i
Step 7. endfor
Step 8. Stop
SUB-ALGORITHM: sieve(n, p)
Step 1. For i=1 to n do
Step 2. p[i]=1
Step 3. endfor
Step 4.p[0]=0, p[1]=0
Step 5. For i=2 to sqrt(n) do
Step 6. For j=i*i to n do
Step 7. begin
Step 8. P[j]=0
Step 9. endfor
Step 10.endfor
Step 11. return

4. PROGRAM:
#include <stdio.h>
#include <math.h>
void sieve(int n, int primes[]);
void main()
12
{
int i, n;
int v[50];
printf("enter n");
scanf("%d", &n);
sieve(n, v);
for (i=0;i<n;i++)
if (v[i] == 1)
printf("%d\n",i); // this just prints out each value if it's Prime
}
void sieve(int n, int primes[])
{
int i, j;
for (i=0;i<n;i++)
primes[i]=1;
primes[0]=0,primes[1]=0;
for (i=2;i<sqrt(n);i++)
for (j=i*i;j<n;j+=i)
primes[j] = 0;
}
5. INPUT/OUTPUT:

Enter n: 10
2
3
5
7

13
PROGRAM 6(B)
1.AIM :
To write a C program that uses non recursive function to search for a Key value in a given list
of integers. Use linear search method
2.PROBLEM DESCRIPTION:
The linear search is most simple serching method. It does not expect the list to be sorted. The
key which is to be searched is compared with each element of the list one by one. If a match
exists, the search is terminated. If the end of list is reached it means that the search has failed
and key has no matching in the list.
3. ALGORITHM:
Main Program:
Step 1: Start
Step 2: Read the value of n
Step 3: for i=1 to n increment in steps of 1
Read the value of ith element into array
Step 4: Read the element(x) to be searched
Step 5: search<--linear(a,n,x)
Step 6: if search equal to 0 goto step 7 otherwise goto step 8
Step 7: print unsuccessful search
Step 8: print successful search
Step 9: stop

Linear Search Function


Step 1: start
Step 2: for i=1 to n increment in steps of 1
Step 3: if m equal to k[i] goto step 4 otherwise goto step 2
Step 4: return i
Step 5: return 0
Step 6: stop
4. PROGRAM:
#include<stdio.h>
void lsearch(int[],int,int);
void main()
{
int a[30],n,i,loc,key;
printf("enter n value:");
scanf("%d",&n);
if(n>30)
{
printf("invalid size…!!!!!");
}
else

14
{
printf("enter array %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter the element for search:");
scanf("%d",&key);
loc=lsearch(a,n,key);

}
}
void lsearch(int a[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
{

printf("\nelement found at a[%d] location of the array",i+1);

}
}
if(i==n)
printf("element not found……!!!!!\n");
}

5. INPUT/OUTPUT:
1: enter range for array:4
enter elements into array:56
43
12
88
9
enter the search element:9
element 9 found at 4
2: enter range for array:5
enter elements into array:23
12
56
34
3
8

15
enter the search element:3
element 3 found at 4
WEEK-7
PROGRAM:7(A)
1.AIM :
Trite a menu-driven C program that allows a user to enter n numbers and then choose
between finding the smallest, largest, sum, or average.
2.PROBLEM DESCRIPTION:
The user has to enter n numbers and then choose between finding the smallest, largest, sum,
or average
The menu and all the choices are to be functions. Use a switch statement to determine what
action to take. Display an error message if an invalid choice is entered
3. ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: for i=1 to n do
Step 4: Read a[i]
Step 5: endfor
Step 6: Read option
Step 7: if the option is ‘1’ then
Big=a[1]
For i=2 to n do
If big<a[i] then big=a[i]
Endfor
Print big
Break
Step 8: if the option is ‘2’ then
small=a[1]
For i=2 to n do
If small >a[i] then small=a[i]
Endfor
Print small
Break
Step 9: if the option is ‘3’ then
sum=0
For i=1 to n do
Sum=sum+a[i]
Endfor
Print sum
Break
Step 10: if the option is ‘3’ then
sum=0
For i=1 to n do
Sum=sum+a[i]
16
Endfor
Avg=sum/n
Print Avg
Break
Step 11:stop
4. PROGRAM:
#include<stdio.h>
void main()
{
int op;
int i,n,a[30],big,small,sum;
float avg;
printf("enter n:");
scanf("%d ",&n);
printf("enter %d values:",n);
for(i=0;i<n;++i)
scanf("%d ",&a[i]);
printf("enter your choice:\n");
printf("1. Biggest\n 2.smallesst\n 3.sum\n 4.avg\n");
scanf(" %d",&op);
switch(op) // used to select particular case from the user
{
case 1:big=a[0];
for(i=0;i<n;i++)
if(big<a[i])
big=a[i];
printf(“biggest is %d”,big);
break;
case 2:small=a[0];
for(i=;i<n;i++)
if(small>a[i])
small=a[i];
printf(“smallest is %d”,small);
break;
case 3:sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
printf(“sum is %d”,sum);
break;
case 4:sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
avg=(float)sum/n;
printf(“average is %f”,avg);

17
break;
default:printf("please enter valid option.");
break;
}
}

5. INPUT/OUTPUT:
Enter n: 3
Enter 3 values: 4 5 6
Enter your choice
1. Biggest
2.smallesst
3.sum
4.avg
1
Biggest is 6

18
PROGRAM 7(B):
1.AIM :
To write a C program that uses non recursive function to search for a Key value in a given
sorted list of integers. Use binary search method
2.PROBLEM DESCRIPTION:
Binary search is a vast improvement over the sequential search. For binary search to work, the
item in the list must be in assorted order. The approach employed in the binary search is divid
and conquer. If the list to be sorted for a specific item is not sorted, binary search fails.
3. ALGORITHM:
Main Program:
Step 1: Start
Step 2:Read the value of n
Step 3:for i=1 to n increment in steps of 1
Read the value of ith element into array
Step 4: Read the element(x) to be searched
Step 5: search<--binary(a,n,x)
Step 6: if search equal to 0 goto step 7 otherwise goto step 8
Step 7: print unsuccessful search
Step 8: print successful search
Step 9: stop

Binary Search Function:


Step 1: start
Step 2: initialise low to 1 ,high to n, test to 0
Step 3: if low<= high repeat through steps 4 to 9 otherwise goto step 10

Step 4: assign (low+high)/2 to mid


Step 5: if m<k[mid] goto step 6 otherwise goto step 7
Step 6: assign mid-1 to high goto step 3
Step 7: if m>k[mid] goto step 8 otherwise goto step 9
Step 8: assign mid+1 to low
Step 9: return mid
Step 10: return 0
Step 11:stop

4. PROGRAM:
#include<stdio.h>
void main()
{
int i,n,key,a[10];

19
printf("enter range for array:");
scanf("%d",&n);
printf("enter elements into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the search element:");
scanf("%d",&key);
binarysearch(a,n,key);
}
void binarysearch (int a[],int n,int key)
{
int low,high,mid;
low=0;
high=n-1;
for(i=0;i<n;i++)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("element %d found at %d",key,mid);
break;
}
if(key<a[mid])
high=mid;
else
low=mid+1;
if(i==n-1)
printf("element %d not found in array",key);
}
getch();
}
5. INPUT/OUTPUT:
1.
enter range for array:4
enter elements into array:12 23 34 45
the search element:45
element 45 found at 3
2.
enter range for array:5
enter elements into array:1 34 56 78 88
the search element:45
element 45 not found in array

20
WEEK 8:
PROGRAM 8(A)
1.AIM :
Program that implements the bubble sort method
2.PROBLEM DESCRIPTION:
Bubble sort is the simplest and oldest sorting technique. This method takes two elements at a
time. It compare these two elements. If first elements is less than second one, they are left
undistrurbed. If the first element is greater then second one then they are swapped. The
procedure continues with the next two elements goes and ends when all the elements are
sorted. But bubble sort is an inefficient algorithm. The order of bubble sort algorithm is
O(n2).
3. ALGORITHM:
Main Program:
1. start
2. read the value of n
3. for i= 1 to n increment in steps of 1
Read the value of ith element into array
4. call function to sort (bubble_sort(a,n))
5. for i= 1 to n increment in steps of 1
print the value of ith element in the array
6. stop

Bubble Sort Function:


1. start
2. initialise last to n
3. for i= 1 to n increment in steps of 1
begin
4. initialise ex to 0
5. for i= 1 to last-1 increment in steps of 1
begin
6. if k[i]>k[i+1] goto step 7 otherwise goto step 5
begin
7. assign k[i] to temp
assign k[i+1] to k[i]
assign temp to k[i+1]
increment ex by 1
end-if
end inner for loop
11. if ex!=0
assign last-1 to last
end for loop
12. stop
21
4. PROGRAM:

#include<stdio.h>
void main()
{
int i,j,temp,a[5],n;
clrscr();
printf("enter the range of array:");
scanf("%d",&n);
printf("enter elements into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("the sorted order is:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}
5. INPUT/OUTPUT:
enter the range of array:3
enter elements into array:3
2
1
the sorted order is: 1 2 3

enter the range of array:5


enter elements into array:56
23
34
12
8
the sorted order is: 8 12 23 34 56

22
PROGRAM:8(B)(I)
1.AIM :
To perform the addition of two matrices
2.PROBLEM DESCRIPTION:
program takes the two matrixes of same size and performs the addition
3.ALGORITHM:
Step 1: start
Step 2: read the size of matrices A,B – m,n
Step 3: read the elements of matrix A
Step 4: read the elements of matrix B
Step 5: compute sum of A and B if m=n
Step 6: print Sum of matrix A and B
Step 7: goto step 9
Step 8: if m and n are not equal, print matrices can not be added
Step 9: Stop

4.PROGRAM:
#include<stdio.h>
int read_matrix(int a[10][10],int m,int n);
int write_matrix(int a[10][10],int m,int n);
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
printf("Input rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix A:\n");
read_matrix(a,r1,c1);
printf("Enter elements of matrix B:\n");
read_matrix(b,r1,c1);
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];

}
write_matrix(c,r1,c1);
//getch();
}
/*Function read matrix*/
int read_matrix(int a[10][10],int m,int n)
{

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

/*Function to write the matrix*/


int write_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
5.INPUT/OUTPUT:
Input rows and columns of A & B Matrix:2
2
Enter elements of matrix A:
2
2
3
4
Enter elements of matrix B:
3
1
2
3

=====Matrix Addition=====
5 3
5 7

PROGRAM 8(B)(II):
1.AIM :
To perform the multiplication of two matrices
2.PROBLEM DESCRIPTION:
program takes the two matrixes of different sizes and checks for possibility of multiplication
and perform multiplication if possible.
24
3.ALGORITHM:
Step 1: start
Step 2: read the size of matrices A and B – m,n and p,q
Step 3: read the elements of matrix A
Step 4: read the elements of matrix B
Step 5: compute multiplication of A and B if n=p
Step 6: print multiplication of matrix A and B
Step 7: goto step 9
Step 8: if n and q are not equal, print matrices can not be multiplication
Step 9: Stop

4.PROGRAM
#include<stdio.h>
int read_matrix(int a[10][10],int m,int n);
int write_matrix(int a[10][10],int m,int n);
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
printf("Input rows and columns of A matrix:");
scanf("%d%d",&m,&n);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be multiplied\n");
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B matrix\n");
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two matrices*/
printf("\n =====Matrix Multiplication=====\n");
for(i=0;i<m;++i)
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}

printf("Resultant of two matrices:\n");


write_matrix(c,m,q);
}

25
/*end if*/
else
{
printf("Matrices cannot be multiplied.");
}
/*end else*/
getch();
}

/*Function read matrix*/


int read_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
return 0;
}

/*Function to write the matrix*/


int write_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
5.INPUT/OUTPUT:
1.
Input rows and columns of A matrix:3
2
Input rows and columns of B matrix:2
2
matrices can be multiplied
resultant matrix is 3*2
Input A matrix
242532
Input B matrix
2456

26
=====Matrix Multiplication=====
Resultant of two matrices:
24 32
29 38
16 24
2.
Input rows and columns of A matrix:3 2
Input rows and columns of B matrix:3
2
Matrices cannot be multiplied.

WEEK:9
PROGRAM :9(A)(I)
1.AIM :
Functions to insert a sub string into given main string from a given position
2.PROBLEM DESCRIPTION:
In this program we need to insert a string into another string from a specified position.
3. ALGORITHM:
Step 1: start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string(n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string( p)
Step 7: copy sub string into main string from position p-1
Step 8: copy temporary string into main string from position p+n-1
Step 9: print the strings
Step 10: stop

4. PROGRAM:
27
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}

28
5. INPUT/OUTPUT:
1.enter first string:
computer
2.enter second string:
gec
3.enter the position where the item has to be inserted:3
comgecputer

PROGRAM:9(A)(II)

1.AIM :
To delete n characters from a given position in a given string

2.PROBLEM DESCRIPTION:
in this program we need to delete a string from the given string at a specified position.

3. ALGORITHM:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and (position+number of
characters to end)
Step 6: stop

4. PROGRAM:

29
#include <stdio.h>
#include <conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
// Function to delete n characters
void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}
5. INPUT/OUTPUT:
1.enter the string
nagaraju
Enter the position from where to delete:4
Enter the number of charcters to be deleted3
nagju
2. enter the string
kaliraju
Enter the position from where to delete:0
Enter the number of charcters to be deleted4
Raju

30
PROGRAM: 9(B)

1.AIM :
To determine if the given string is a palindrome or not

2.PROBLEM DESCRIPTION:

if the reverse of a string is equal to original string then it is called palindrome

3. ALGORITHM:
Step 1:start
Step 2: read the string
Step 3: store reverse of the given string in a temporary string
Step 4: compare the two strings
Step 5: if both are equal then print palindrome
Step 6: otherwise print not palindrome
Step 7: stop

4. PROGRAM:
#include<stdio.h>
#include<string.h>
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
enum Boolean matched=true;
if(len==0)
return 0;
left=0;
right=len-1;
/* Compare the first and last letter,second & second last & so on */
while(left<right&&matched)
{
if(string[left]!=string[right])
matched=false;
else
{
left++;
right--;
}
}
return matched;
}
int main()
{
31
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;

5. INPUT/OUTPUT:
1. Enter the string:malayalam
The given string malayalam is a palindrome
2. Enter the string:india
The given string india is not a palindrome

32
WEEK :10
PROGRAM :10(A)
1.AIM :
Write a C program to replace a substring with another in a given line of text
2.PROBLEM DESCRIPTION:
Use string handling functions to replace substring with new string.
3. ALGORITHM:
Step 1. start
Step 2. Read main string
Step 3. Read sub string
Step 4. Read new string
Step 5. If substring is found in main string, then replace with new string
Step 6. stop
4. PROGRAM:
#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig)))
return str;
strncpy(buffer, str, p-str);
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
int main(void)
{
char str[100],str1[50],str2[50];
printf("Enter a one line string..\n");
gets(str);
printf("Enter the sub string to be replaced..\n");
gets(str1);
printf("Enter the replacing string....\n");
gets(str2);
puts(replace_str(str, str1, str2));
return 0;
}
5. INPUT/OUTPUT:
Enter a one line string.. hello tkr
Enter the sub string to be replaced..tkr
Enter the replacing string....tkrcet
Hello tkrcet
33
PROGRAM 10(B)
1.AIM :
Write a C program that reads 15 names each of up to 30 characters, stores them in an
array, and uses an array of pointers to display them in ascending (ie. alphabetical) order
2.PROBLEM DESCRIPTION:
Bubble sort is the simplest and oldest sorting technique. This method takes two elements at a
time. It compare these two elements. If first elements is less than second one, they are left
undistrurbed. If the first element is greater then second one then they are swapped. The
procedure continues with the next two elements goes and ends when all the elements are
sorted. But bubble sort is an inefficient algorithm. The order of bubble sort algorithm is O(n2).
3. ALGORITHM:
Main Program:
1. start
2. read the value of n
3. for i= 1 to n increment in steps of 1
Read the value of ith element into array
4. call function to sort (bubble_sort(a,n))
5. for i= 1 to n increment in steps of 1
print the value of ith element in the array
6. stop
Bubble Sort Function:
1. start
2. initialise last to n
3. for i= 1 to n increment in steps of 1
begin
4. initialise ex to 0
5. for i= 1 to last-1 increment in steps of 1
begin
6. if k[i]>k[i+1] goto step 7 otherwise goto step 5
begin
7. assign k[i] to temp
assign k[i+1] to k[i]
assign temp to k[i+1]
increment ex by 1
end-if
end inner for loop
11. if ex!=0
assign last-1 to last
end for loop
12. stop

34
4. PROGRAM:
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[15][30],temp[25];
puts("How many strings u are going to enter?: ");
scanf("%d",&count);
puts("Enter Strings one by one: ");
for(i=0;i<count;i++)
scanf("%s",str[i]);
for(i=0;i<count;i++)
for(j=i+1;j<count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("Order of Sorted Strings:");
for(i=0;i<count;i++)
puts(str[i]);
return 0;
}
5. INPUT/OUTPUT:
How many strings u are going to enter?:
3
Enter Strings one by one:
tkrcet
tkrec
tkrem
Order of Sorted Strings:tkrcet
tkrec
tkrem

35
WEEK:11
PROGRAM 11(A)
1.AIM :
To convert the given binary number to 2’s complement
2.PROBLEM DESCRIPTION:
In this program the given binary number is first covert the numbers 0 to1 and 1 to 0. And
finally add the 1 to the converted number. Then we will get the 2’s complement number.

3. ALGORITHM:
main program
Step 1: Start
Step 2: declare the subprogram “complement(char *a)”
Step 3: initialize the variable i
Step 4: read the binary number
Step 5: perform the loop operation. if it is true then follows. if not goto step 7
i) for(i=0;a[i]!=’\0’;i++)
ii) if(a[i]!=’0’&&a[i]!=’1’) then displayed the number is not valid. enter the correct number.
iii) Exit the loop

Step 6: call sub program ‘complemt(a)’


Step 7: stop
Sub program:
Step 1: initialize the variable I,c=0,b[160
Step 2: 1=strlen(a)
Step 3: perform the loop operation. if it is true then follows. if not goto
i)for(i=l-1;i>=0;i--)
ii)if(a[i]==’0’) then b[i]=’1’ else
iii)b[i]=’0’
Step 4: for(i=l-1;i>=0;i--) is true
i) if(i==l-1) then
ii) if(b[i]==’0’) then b[i]=’1’ else
iii) b[i]=’0’,c=1 if not goto step 5

Step 5: if(c==1&&b[i]==’0’) is true then


i) b[i]=’1’, c=0 if not goto Step 6

Step 6: if(c==1&&b[i]==’1’) then b[i]=’0’,c=1


Step 7: displayed b[l]=’\0’
Step 8: print b and return to main program

4. PROGRAM:
#include <stdio.h>
#include<conio.h>
36
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
for(i=l-1; i>=0; i--)
{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
37
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}
5. INPUT/OUTPUT:

1.Enter the binary number101010


The 2's complement is 010110
Enter the binary number11111
The 2's complement is 00001
Enter the binary number2222
The number entered is not a binary number. Enter the correct number

38
PROGRAM 11(B)
1.AIM :
To convert roman number to it’s decimal equivalent
2.PROBLEM DESCRIPTION:
In this program we have to take the roman value. This value is converted into a it’s equivalent
decimal number.
Ex: X=10
3. ALGORITHM:
Step 1: Start
Step 2: read the roman numerical as string
Step 3: find length of roman numerical
Step 4: for each charcter in the string
i) if(char=I) then decimal=1
ii) if(char=V) then decimal=5
iii) if(char=X) then decimal=10
iv) if(char=L) then decimal=50
v) if(char=C) then decimal=100
vi) if(char=D) then decimal=500
vii) if(char=M) then decimal=1000
viii) otherwise invalid character

Step 5: repeat step 4 until the length of the string


Step 6: k=char[length-1]
Step 7: for each character of decimal string
i) if(decimal[i]>dec[i-1]) then k=k-decimal[i-1]
ii) else if(decimal[i]=decimal[i-1 or decimal[i]<decimal[i-1) then k=k+decimall[i-1]

Step 8: repate step 7 until the length of decimal string


Step 9: print decimal value
Step 10: Stop
4. PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
39
for(i=0;i<len;i++) // loop will continue until I is not graterthan length.
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--) // loop will continue until I lessthan zero
{
if(a[i]>a[i-1]) // check the condition
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
5. INPUT/OUTPUT:
Enter the Roman Numeral:D
Its Decimal Equivalent is:500
Enter the Roman Numeral:X
Its Decimal Equivalent is:10
Enter the Roman Numeral:23
Invalid Value

40
WEEK:12
PROGRAM 12(A)
1.AIM :
Program to display the contents of a file.
2.PROBLEM DESCRIPTION:
In this program we have to use the file functions to display the contents of a file.

3. ALGORITHM:
Step 1: Start
Step 2: open the source file in write mode.
Step 3: Read the data character by character from the user and write it in to the file.
Step 4: close the file.
Step 5: open the source file in read mode.
Step 8: read a character from source file and print on the screen until EOF
Step 9: close the file.
Step10: Stop.

4. PROGRAM:
#include<stdio.h>
void main()
{
FILE *f1;
char c;
printf(“Data input press cntrl+z at the end”);
f1=fopen(“Input.txt”,”w”); /*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“\nData output \n”);
f1=fopen(“Input.txt”,”r”); /*Reopen the file input*/
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f1);
}

5. INPUT/OUTPUT:
Data input press cntrl+z at the end
Hello welcome TKRCET ^Z
Data output
Hello welcome TKRCET

41
PROGRAM 12(B)
1.AIM :
Program which copies one file to another , replacing all lowercase characters
with their uppercase equivalents.
2.PROBLEM DESCRIPTION:
In this program we have to use the file functions to perform the copy operation from one file to
another file.
3. ALGORITHM:
Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments =3 or not. If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print source file can not be open
Step 6: open destination file in write mode
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file
Step 9: convert lower case letter to upper case and write to destination file until EOF
Step 9: Close source file and destination file
Step 10: Stop

4. PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(argc!=3)
{
puts("Invalid number of arguments.");
exit(0);
}
fs = fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
exit(0);
}
ft = fopen(argv[2],"w");
if (ft==NULL) // check the condition if the file pointer is NULL or not
{
puts("Target file cannot be opened.");
fclose(fs);
exit(0);
42
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF) // check the condition if the file is end or not
break;
else{
ch=islower(ch)?ch+32:ch;
fputc(ch,ft);
}
}
fclose(fs);
fclose(ft);
getch();
}
5. INPUT/OUTPUT:
source.c
this is source text
ouput.c
Command line arguments
source.c ouput.c
source.c
this is source text
ouput.c
THIS IS SOURCE TEXT
Command line arguments
source.c
Invalid number of arguments.

43
WEEK:13
PROGRAM 13(A)
1.AIM : To Write a C program to count the number of times a character occurs in a text file
2. ALGORITHM:
Step 1: Start
Step 2: open the source file in write mode.
Step 3: Read the data character by character from the user and write it in to the file.
Step 4: close the file.
Step 5: Read given character x from keyboard
Step 6: open the source file in read mode.
Step 7: read a character y from source file and print on the screen until EOF
Step 8: if character x is equal to character y, then count no. of times it occurs
Step 9: close the file.
Step 10: Print count
Step11: Stop.
3. PROGRAM:
#include<stdio.h>
void main()
{
FILE *f1;
int count=0;
char c,x;
printf(“Data input press cntrl+z at the end”);
f1=fopen(“Input1.txt”,”w”); /*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“Enter a character:\n”);
scanf(“%c”, &x);
f1=fopen(“Input1.txt”,”r”); /*Reopen the file input*/
while((c=getc(f1))!=EOF)
{
if(c==x)
count++;
}
printf(“count is %d”,count);
fclose(f1);
}
4. INPUT/OUTPUT:
Data input press cntrl+z at the end
Hello welcome TKR College of Engineering and Technology ^Z
Enter a character
e
count is 9
44
Input1.txt
Hello welcome TKR College of Engineering and Technology

PROGRAM 13(B)
1.AIM :
To Write a C program to compare two files, printing the first line where they differ.

2. ALGORITHM:
Step 1:Start
Step 2:open two files in read mode for comparing
Step 3:read next line from first file
Step 4:read next line from second file
Step 5:if two lines are not equal, then print the line and goto step 7
Step 6:if two lines are equal, then goto step 4
Step 7:print the line which differs
Step 8:stop

3. PROGRAM:
#include <stdio.h>
#include<string.h>
int main()
{
char c1[1000],c2[1000];
FILE *f1;
FILE *f2;
f1=fopen("output.txt","r");
f2=fopen("output_copy.txt","r");
printf("\n");
while((fgets(c1,1000,f1)!=NULL)&&(fgets(c2,1000,f2)!=NULL))
{
if(strcmp(c1,c2)!=0)
printf("%s",c1);
printf("%s",c2);
break;
}
printf("\n");
fclose(f1);
fclose(f2);
return 0;
}
4. INPUT/OUTPUT:
output.txt
hellow tkrcet

45
output_copy.txt
HOW are you
Output:
hellow tkrcetHOW are you

WEEK-14
PROGRAM: 14(A)
1.AIM :
Write a C program to change the nth character (byte) in a text file
2. ALGORITHM:
Step 1: Star
Step 2: read the command line arguments
Step 3: check if arguments=3 or not
If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print file can not be open
Step 6: read n
Step 7: read the nth item from file stream using fseek
Step 8: change nth item in file
Step 9: stop
3. PROGRAM:
#include <stdio.h>
#include <string.h>
#include <process.h>
void main(int argc, char *argv[])
{
char ch;
FILE *fp;
if(argc!=3)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r+");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
fseek(fp,argv[2],0);
ch=fgetc(fp);
ch++;
fseek(fp, argv[2],0);
46
fputc(fp,ch);
fclose(fp);
}
4. INPUT/OUTPUT:
Command line arguments
source.c 4

PROGRAM: 14(B)
1.AIM :
To reverse the first n characters in afile
2.PROBLEM DESCRIPTION:
This program perform the reverse operation of n characters in the file
3. ALGORITHM:
Step 1: Star
Step 2: read the command line arguments
Step 3: check if arguments=3 or not
If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print file can not be open
Step 6: Store no of chars to reverse in k
K= *argv[2]-48
Step 7: read the item from file stream using fread
Step 8: Store chars from last position to initial position in another string(temp)
Step 9: print the temp string
Step 10: Stop

4.PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
void main(int argc, char *argv[])
{
char a[15];
char s[20];
char n;
int k;
int j=0;
int i;
int len;
FILE *fp;
if(argc!=3)

47
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=*argv[2]-48;
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}

5. INPUT/OUTPUT:
source.c
this is source
ouput.c
Command line arguments
source.c ouput.c
source.c
this is source
ecruos si siht
Command line arguments
source.c
Invalid number of arguments.

48
WEEK-15
PROGRAM 15(A)
1.AIM :
Program to merge two files into third file
2.PROBLEM DESCRIPTION:
In this program we have to use the file functions to perform the merging two files into
third file.
3. ALGORITHM:
Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments =4 or not. If not print invalid no of arguments
Step 4: open source file1, file2 in read mode
Step 5: if NULL pointer, then print source files can not be open
Step 6: open destination file in append mode
Step 7: if NULL pointer, then print destination file can not be open
Step 8: read a character from source file1 and write to destination file until EOF
Step 9: read a character from source file2 and write to destination file until EOF
Step 9: Close source file1, file2 and destination file
Step 10: Stop

4. PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fp1,*fp2,*ft;
char ch1,ch2;
if(argc!=4)
{
printf("\n enter correct no fo arguments");
return;
}
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"r");
ft=fopen(argv[3],"a");
while(!feof(fp1))
{
ch1=getc(fp1);
putc(ch1,ft);
}
fclose(fp1);
49
while(!feof(fp2))
{
ch2=getc(fp2);
putc(ch2,ft);
}
fclose(fp2);
fclose(ft);
printf("\n files merged successfully");
return;
}
5. INPUT/OUTPUT:
Source1.c
this is source1 text

Source2.c
this is source2 text

Command line arguments


Source1.c Source2.c ouput.c

Ouput:
files merged successfully

ouput.c
this is source1 text this is source2 text

50
PROGRAM : 15(B)
1.AIM :
Write a C program that uses the macro and prints the maximum of two numbers
2.PROBLEM DESCRIPTION:
Define a macro that finds the maximum of two numbers
3. ALGORITHM:
Step 1: start
Step 2: read a,b
Step 3: big=max(a,b)
Step 4: print big
Step 5: stop

4. PROGRAM:
#include<stdio.h>
#define MAX(x,y) (x)<(y)?(y):( x);
main()
{
int a,b,big;
printf("enter the vaue for a and b:\n");
scanf("%d%d",&a,&b);
big=MAX(a,b);
printf("big is %d",big);
}
}
5. INPUT/OUTPUT:
Enter the value for a and b: 33 53
big is 53

51
WEEK-16:
PROGRAM : 16(A)
1.AIM :
Write a C Program to calculate the sum of n numbers entered by the user using
malloc( ) and free() functions.
2.PROGRAM :
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory free(ptr);
}
3.INPUT/OUTPUT:
Enter number of elements: 5
Enter elements: 1
2
3
4

52
PROGRAM : 16(B)
1.AIM :
Write a C Program to calculate the sum of n numbers entered by the user using calloc( ) and
free() functions.
2.PROGRAM :
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}
3.INPUT/OUTPUT:
Enter number of elements: 5
Enter elements: 1
2
3
4
5
Sum = 15

53

You might also like