0% found this document useful (0 votes)
9 views45 pages

PPS Lab Manual

The document outlines a series of programming exercises in C, covering various topics such as basic arithmetic operations, data structures, and algorithms. Each exercise includes an aim, algorithm, flowchart, and sample code, along with expected inputs and outputs. The exercises range from simple tasks like calculating sums and averages to more complex operations like managing student records and implementing data structures.

Uploaded by

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

PPS Lab Manual

The document outlines a series of programming exercises in C, covering various topics such as basic arithmetic operations, data structures, and algorithms. Each exercise includes an aim, algorithm, flowchart, and sample code, along with expected inputs and outputs. The exercises range from simple tasks like calculating sums and averages to more complex operations like managing student records and implementing data structures.

Uploaded by

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

CONTENTS

Page
Week Name of the Program No’s
a) Write a program to find sum and average of three numbers
1 b) Write a program to calculate simple interest(SI) for a given 01
principal (P), time (T), and rate of interest (R) (SI = P*T*R/100)
a) Write a program to swap two variables values with and
2 without using third variable 09
b) Write a program to find the roots of a quadratic equation.
a) Write a program to find the sum of individual digits of a given
positive integer.
b) Write a program, which takes two integer operands and one
3 20
operator from the user, performs the operation and then prints
the result.
(Consider the operators +,-,*, /, % and use Switch Statement)
a) Write a program to find both the largest and smallest number
in a list of integers.
4 30
b) Write a program to find the sum of integer array elements
using pointers
a) Write a program to perform addition of two matrices.
5 39
b) Write a program to perform multiplication of two matrices.
a) Write a program to find the length of the string using Pointer.
6 b) Write a program to count the number of lines, words and 49
characters in a given text..
a) Write a program to find factorial of a given integer using non-
recursive function and recursive function.
7 59
b) Write program to find GCD of two integers using non-recursive
function and recursive function.
a) Write a program using user defined functions to determine
whether the given string is palindrome or not.
8 73
b) Write a Program to swap the values of two variables using
i) Call by Value ii) Call by Reference
a) Write a program to find the sum of integer array elements
using pointers, use dynamic memory allocation to allocate
9 memory. 83
b) Write a program to perform subtraction of two matrices,
Design functions to perform read ,display and subtract
a) Write a program to create a structure named book and display
the contents of a book.
10 92
b) Write a Program to Calculate Total and Percentage marks of a
student using structure.
a)Write a program that uses functions to perform the following
operations:
i) Reading a complex number ii) Writing a complex number
11 iii) Addition of two complex numbers 100
iv) Multiplication of two complex numbers
b)Write a program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)
a) Write a program to copy the contents of one file to another.
b) Write a program to merge two files into a third file (i.e., the
12 113
contents of the first file followed by those of the second are put
in the third.
a) Write a program for static implementation of stack
13 122
b) Write a program for static implementation of Queue
14 Write a program to perform various operations on singly linked 137
list

a) Write a program for dynamic implementation of stack


15 150
b) Write a program for dynamic implementation of Queue

16 Case 1: Student Record Management System 165

17 Case 2: Library Management System 176


1. a) Write a program to find the sum and average of three numbers.

Aim: Find the sum and average of three numbers


Algorithm:
Step 1: Start
Step 2: Read values num1, num2, num3
Step 3: Add num1, num2, num3 and assign the result to sum.
sum←num1+num2 +num3
average ← sum/3
Step 4: Display sum and average
Step 5: Stop
Flow Chart:

Program:
#include<stdio.h>
int main( )
{
int a,b,c;
int sum,average;
printf("Enter any three integers: ");
scanf("%d%d %d",&a,&b,&c);
sum = a+b+c;
average=sum/3
printf("Sum and average of three integers: %d %d",sum,average);
return 0;
}

SAMPLE INPUT:
Enter any three integers: 2 4 5
EXPECTED OUTPUT:
Sum and average of three integers: 11 3
1. b) Write a program to calculate simple interest(SI) for a given principal (P), time
(T), and rate of interest (R) (SI = P*T*R/100)

Aim: To find the simple interest


Algorithm:
Step 1: Start.
Step 2 : Read Principal Amount, rate and time.
Step 3 : Calculate Interest using formula SI= ((amount*rate*time)/100)
Step 4 : Print Simple Interest SI.
Step 5 : Stop

Flow chart

Program:
#include<stdio.h>
int main()
{
int p,r,t,si;
printf("Input principle:");
scanf("%d",&p);
printf("Rate of interest:");
scanf("%d",&r);
printf("Enter time(in years):");
scanf("%d",&t);
si=(p*r*t)/100;
printf("Simple interest = %d",si);
return 0;
}

SAMPLE INPUT:
Input principle: 10000
Rate of interest: 12
Enter time(in years): 2
EXPECTED OUTPUT:
Simple interest = 2400
2 a)Write a program to swap two variables values with and without using third variable

AIM: To swap two variable values using a third variable


DESCRIPTION:
Swap the values of the variable using temporary variable
tt=a
a=b
b=t
ALGORITHM:
 using a third variable
Step 1 : Start
Start 2 : READ num1, num2
Start 3 : temp = num1
Start 4 : num1 = num2
Start 5 : num2 = temp
Start 6 : PRINT num1, num2
Start 7 : Stop
 without using a third variable
Step 1 : Start
Start 2 : READ num1, num2
Start 3 : num1 = num1 + num2
Start 4 : num2 = num1 - num2
Start 5 : num1 = num1 - num2
Start 6 : PRINT num1, num2
Start 7 : Stop
FLOWCHART:

PROGRAM:
using a third variable
#include<stdio.h>
int main()
{
int x, y, t;
printf("Enter two integers: ");
scanf("%d%d", &x, &y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
t = x;
x = y;
y = t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);
return 0;
}

SAMPLE INPUT:
Enter two integers: 10 20
EXPECTED OUTPUT:
Before Swapping
First integer = 10
Second integer = 20
After Swapping
First integer = 20
Second integer = 10

PROGRAM: Without using a third variable


#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", a, b);
return 0;
}
SAMPLE INPUT:
Enter two integers: 23 45

EXPECTED OUTPUT:
Before Swapping
First integer = 23
Second integer = 45
After Swapping
First integer = 45
Second integer = 23
2 b) Write a program to find the roots of a quadratic equation.

AIM: To find the roots of a quadratic equation. √2

2a

ALGORITHM:
Step 1: Start
Step 2: Read a,b,c
Step 3: calculate disc = b*b-4*a*c
Step 4: if(disc>0)
Begin
Step 5: root1=(-b+sqrt(disc))/(2*a)
Step 6: root2=(-b-sqrt(disc))/(2*a)
Step 7: Print “Root1” , “Root2”
End
Step 8: else if(disc=0)
Begin
Step 9: root1=-b/(2*a)
Step 10: root2=root1;
Step 11: Print “Root1” , “Root2”
End
Step 12: else
Step 13: Print Roots are imaginary
Step 14: Stop

Flow Chart

PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float disc, root1, root2;
float img,real;
printf("ENTER VALUES FOR a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
disc=(float)b*b-4*a*c;
if(disc>0)
{ printf("THE ROOTS ARE REAL & UNEQUAL:\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}

else if(disc==0)
{ printf("THE ROOTS ARE REAL AND EQUAL:\n");
root1=-b/(2*a);
root2=root1;
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else
{
printf("THE ROOTS ARE IMAGINARY:\n");
}
return 0;
}
SAMPLE INPUT:
ENTER VALUES FOR a, b, c
1 4 4
EXPECTED OUTPUT:
THE ROOTS ARE EQUAL AND THEY ARE.. Root1=-2 Root2=-2
3 a)Write a C program to find the sum of individual digits of a
given positive integer.

AIM: To find the sum of individual digits of positive integer.


Description:
Summation of digits of a
number Ex: 1234
Summation =1+2+3+4=10

ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0)
Begin
Step 5: r←n%10
Step 6: sum← sum + r
Step 7: n←n/10
End
Step 8: Print “sum”
Step 9: Stop
PROGRAM:
#include<stdio.h>
int main()
{
int n,r,sum=0;
printf("ENTER A POSITIVE INTEGER \
n");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUMOF INDIVIDUAL
DIGITS OF A POSITIVE INTEGER IS..
%d", sum);
return 0;
}
SAMPLE INPUT:
ENTER A POSITIVE INTEGER
5321
EXPECTED OUTPUT:
THE SUM OF INDIVIDUAL DIGITS OF A
POSITIVE INTEGER
3 b) Write a C program, int main()
which takes two integer {
operands and one operator int a,b,c;
from the user, performs the char ch,t;
operation and then prints printf("ENTER TWO VALUES FOR a &
b\n");
the result. (Consider the
scanf("%d %d",&a,&b);
operators +,-,*, /, % and use scanf(“%c”,&t); // to
Switch Statement) skip the newline character
printf("MENU OPTIONS \n");
AIM:To perform arithmetic operations printf("************\n");
using switch statement. printf("Addition\n");
Algorithm: printf("Subtraction\n");
Step 1: Read a,b printf("Multiplication\n");
Step 2: Print “Menu Options” printf("Division\n");
Step 3: do printf("Modulus\n");
Begin Step 4: Read ch printf("\n");
Step 5: switch(ch) printf("ENTER Operator : \n");
Begin Step 6: scanf("%c",&ch);
case ‘+’: Begin switch(ch)
Calculate c = a+b {
Print “c” case ‘+’:c=a+b;
break; p
End rintf("
case ‘-‘: Begin The
Calculate c = a-b additi
Print “c” on of
break; %d
End and
%d is..
case ‘*’:Begin
%d\
Calculate c = a*b n",a,b
Print “c” ,c);
break; break;
End case
case ‘/’:Begin ‘-‘:c=a
Calculate c = a/b -b;
Print “c” pr
break; intf("The
End subtracti
case ‘%’:Begin on of %d
Calculate c = a%b and %d
is..%d\
Print “c”
n",a,b,c);
break; break;
End case
default: ‘*’:c=a*b
Print “Invalid choice” ;
End prin
tf("The
multiplicati
Program: on of %d
and %d is..
#include<stdio.h>
%d\
n",a,b,c); break; smallest number in a
case ‘/’:c=a/b; list of integers
prin
tf("The AIM: To find the largest and smallest
division of number in a list of integers.
%d and %d
is..%d\
n",a,b,c);
ALGORITHM:
break; case Step 1: start
‘%’:c=a%b; Step 2: read n
prin
tf("The Step 3: initialize i=0
modulus of Step 4: if i<n do as follows. If not
%d and %d
goto step 5
is..%d\
n",a,b,c); Read a[i]
break;
default:prin Increment i
tf("INVALID goto step 4
CHOICE\
n"); } Step 5: small=a[0], large=a[0]
}
Step 6: initialize i=0
return 0;
} Step 7: if i<n do as follows. If not
goto step 8
SAMPLE INPUT: If a[i]<small
ENTER TWO VALUES FOR a & b: 20 Assign small=a[i]
16 If a[i]>large
EXPECTED OUTPUT:
MENU OPTIONS Assign large=a[i]
************* Increment i goto Step 7
Addition
Subtraction Step 8: print small, large
Multiplication Step 9: stop
Division
Modulus Program:
#include<stdio.h>
ENTER Operator : + int main()
The addition of 20 and 16 is..36 {
int a[10],i,n,small,large;
printf("Enter The Array Size:");
scanf("%d",&n);
printf("Enter The Array
elements:");
for(i=0;i<n;i++)// read the
elements of an array
scanf("%d",&a[i]);
small=a[0];
large=a[0];
for(i=1;i<n;i++)// read the
4 a) Write a program to find elements of an array
both the largest and {
if(a if(a[i]>lar
[ g
i e
] )
< /
s /
m c
a h
ll e
) c
/ k
/
c t
h h
e e
c
k c
t o
h n
e d
c i
o t
n i
d o
i n
t f
i o
o r
n
m
f a
o x
r i
m m
i u
n m
i
m v
u a
m l
u
v e
a
l l
u a
e r
s g
m e
a =
ll a
= [
a i
[ ]
i ;
] }
;
printf("largest value is:%d\n",large);
printf("smallest value is:%d\
n",small); 4 b) Write a C
return 0; program to find the
} sum of integer array
SAMPLE INPUT: elements using
Enter The Array Size: 10
ENTER THE ELEMENTS OF ARRAY
pointers
7 10 9 8 6 5 AIM: To find the sum of integer
EXPECTED OUTPUT: array elements using pointers.
largest value is : 10 Description: Consider an
smallest value is : 1 integer array. By considering
the name of the array as
initial address of the first
element, addition will be
performed.
ALGORITHM:
Start
Step 1:start
Step 2: Read n elements into array
Step 3: initialize sum=0
Step 4: for(i=0;i<n;i++)
Step 5: sum=sum+*(A+i)
step 6:print sum
for i =0 to n-1
step 7: stop

Flow chart:
Read A[i]

Sum=0
for i=0 to n-
for i = 0 to n

sum=sum+*

Print sum
Stop

PROGRAM:
#include<stdio.h>
int main( )
{ 5 a)Write a program to
int A[50],sum=0,i,n; perform addition of two
printf("Enter how many values to matrices.
read");
scanf("%d",&n); AIM: To perform addition of two
printf("enter elements into array"); matrices.
for(i=0;i<n;i++) Description: Consider two matrices
scanf("%d",&A[i]); and their order is R1xC1 and R2XC2.
for(i=0;i<n;i++) The condition is R1==R2 and C1==C2,
sum=sum+*(A+i); then only the addition is possible.
printf("the addition of array ALGORITHM:
elements is %d",sum); Step 1: Start
return 0; Step 2: Read the order of the two
} matrices R1, C1 and R2, C2
SAMPLE INPUT: Step 3: if R1 != R2 or C1 != C2
Enter the array elements : 1 2 3 4 5 6 7 8 9 Print "Addition not
1 possible"
EXPECTED OUTPUT: goto step 8
the addition of array elements is : 46 Step 4: for i is 0 to R1 by step 1
for j is 0 to C1 by step 1
read a[i][j]
Step 5: for i is 0 to R2 by step 1
for j is 0 to C2 by step 1
read b[i][j]
Step 6: for i is 0 to R1 by step 1
for j is 0 to C1 by step 1
calculate
c[i][j]=a[i][j]+b[i][j]
S
t
e
p

7
:

f
o
r
i
i
s

t
o

R
1

b
y
PROGRAM:
s
t #include<stdio.h>
e int main()
p {
int a[5][5],b[5][5],c[5][5];
1 int i,j,p,q,r,s;
printf("ENTER ORDER OF A
f
o MATRIX\n");
r scanf("%d%d",&p,&q);
printf("ENTER ORDER OF B
j MATRIX\n");
scanf("%d%d",&r,&s);
i if(p==r&&q==s)
s
{
0 printf("ENTER A
MATRIX\n");
t for(i=0;i<p;i++)
o for(j=0;j<q;j++)
scanf("
C
1 %d",&a[
i][j]);
b printf("ENTER B
y MATRIX\n");
for(i=0;i<p;i++)
s
for(j=0;j<q;j++)
t
scanf("
e
p %d",&b[
i][j]);
1 for(i=0;i<p;i++)
print c[i][j] for(j=0;j
Step 8: Stop <q;j++)
c
Flowchart: [
i
]
[
j
]
=
a[i][j] Step 2: Read order of two matrices
+b[i] R1, C1 and R2, C2
[j]; Step 3: if C1!=R2
printf(" After Addition of print "Multiplication not
two matrices :\n"); possible"
for(i=0;i<p;i++) goto Step 8
{ Step 4: for i is 0 to R1 by step 1
for(j=0;j<q;j++) for j is 0 to C1 by step 1
{ read a[i][j]
printf("%d\ Step 5: for i is 0 to 21 by step 1
t",c[i][j]); for j is 0 to C2 by step 1
} read b[i][j]
printf("\n"); Step 6: for i is 0 to R1 by step 1
} for j is 0 to C2 by step 1
} c[i][j]=0;
else for k is 0 to C1 by step 1
{ c
printf("Addition not a
possible"); l
} c
return 0; u
} l
a
SAMPLE INPUT: t
ENTER ORDER OF A MATRIX 2 2 e
ENTER ORDER OF B MATRIX 2 2
ENTER A MATRIX c
12 [
34 i
ENTER B MATRIX ]
[
12
j
34 ]
EXPECTED OUTPUT: =
After Addition of two matrices : c
2 4 [
6 8 i
]
[
j
]
+
a
5 b)Write a C program to perform [
multiplication of two matrices. i
]
[
AIM: To perform multiplication of two
k
matrices. ]
Description: Consider two matrices and *
their order is R1xC1 and R2XC2. b
The condition is C1==R2 ,then only the [
multiplication is possible. k
]
ALGORITHM:
[
Step 1: Start
j] ,
St p
e ,
p q
7: ;
fo
ri i
is n
0 t
to
R i
1 ,
b j
,
y
k
st
;
e
p p
1 r
for j is 0 to C2 by step 1 i
n
print c[i][j]
t
Step 8: Stop f
Program: (
#include<stdio.h> "
int main() E
{ n
t
i e
n r
t
t
a h
[ e
5
] s
[ i
5 z
] e
,
b o
[ f
5
] A
[
M
5
t
]
r
,
c
i
[ x
5 (
] R
[
o
5
w
]
, a
m n
, d
n
f
C
o B
l
) M
: t
r
\ i
n x
"
) (
; R
o
s w
c
a a
n n
f d
(
" C
% o
d l
% )
d :
" \
, n
& "
m )
, ;
&
n s
) c
; a
p n
r f
i (
n "
t %
f d
( %
" d
E "
n ,
t &
e p
r ,
&
t q
h )
e ;
if(n!=p)
s
i {printf("Multi
z plicati
e on
Not
o Possi
ble\ r
n
Ple (
ase i
re- =
ent 0
er\ ;
n"); i
prin <
tf(" m
corr ;
ect i
size +
and +
try )
aga for(j=0;j<n;j++)
in ..
... \ scanf("
n"); %d",&a
} [i][j]);
else printf("Enter Matrix B
{printf(" Values Row by Row\
E n");
n for (i=0;i<p;i++)
t for(j=0;j<q;j++)
e
scanf("
r
M %d",&b
a [i][j]);
t //logic for
r multiplication
i for (i=0;i<m;i++)
x {
A for(j=0;j<q;j++)
{
V
c[i][j]=0;
a
l for(k=0;
u k<n;k+
e +)
s c
R [
o i
w ]
[
b
y j
R ]
o +
w =
\
n a
" [
); i
f ]
o [
k]*b Enter Matrix Value Row by Row
[k] 3 4
[j]; 4 2
} EXPECTED OUTPUT:
} A matrix is:
printf("A Matrix is :\n"); 1 0
for (i=0;i<m;i++) 2 6
{ B Matrix is:
for(j=0;j<n;j++) 3 4
{ 4 2
printf("%5d", C matrix is:
a[i][j]); 2 4
} 24 20
printf("\n");
}
printf("B Matrix is :\n");
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%5d",b[i][j]);
}
printf("\n");
}
printf("C Matrix is :\n");
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%5d
",c[i][j]);
}
printf("\n");
}
}
r
e
t
u
r
n

0
;

SAMPLE INPUT:
Enter the size of A Mtrix (Row and Col): 2 2
Enter the size of B Mtrix (Row and Col): 2
Enter Matrix Value Row by Row
1 0
2 6
while (*p != '\0')
{c
o
u
n
t
+
6 a)Write a program to find the +
length of the string using Pointer. ;

p
AIM: To find the length of the string using
+
Pointe +
;
}
Algorithm: return count;
Step 1:start }
Step 2:read string
Step 3: count=0;i=0 SAMPLE INPUT:
Step 4: if string*i+!=’\0’ Enter the String : pritesh
count:= count +1
EXPECTED OUTPUT:
i:=i+1
Length of the given string
goto step 4
pritesh is : 7
step 5:print count Record at least 2 results
step 6 stop

Program:
#include<stdio.h>
int string_Len(char*);
int main() 6b )Write a C program to
{ count the number of
char str[20]; lines, words &characters
int length; in a given text.
printf("\nEnter any string : ");
gets(str); AIM:
length = string_Len (str);
To count the number of lines,
printf(" words and characters in a given
The
list.
length
of the ALGORITHM:
given Step 1: Initialize charactercount,
string
%s is :
wordcount and linecount to 0.
%d", Step 2: Read a character ch
str, Step 3: check if given character
length); is $, if so goto step 7
return
0; Step 4: otherwise, if ch = ' ' ,
} increment wordcount by 1, goto
int string_Len (char *p) /* p=&str[0] */ step 2
{ Step 5: else if ch
int count = 0; =
'\n' }
(newli
ne
charac SAMPLE INPUT:
ter)
incre Enter text at end $abc def
ment ghi jkl
lineco mno pqr
unt $
and
wordc EXPECTED OUTPUT:
ount
by 1, No. of Characters : 18
goto No. of Words : 6
step 2 No. of Lines : 3
Step 6: else increment
charactercount by 1, goto step 2
Step 7: Print charactercount,
wordcount and linecount
Step 8: Stop

PROGRAM:
#include <stdio.h>
int main()
{
char ch;
int i=0,wc=0,lc=0,cc=0;
printf("Enter text at end $");
while((ch=getchar())!='$')
{
if(ch==' ')
wc++;
else if(ch=='\n')
{
lc++;
wc++;
}
else
cc++;

}
printf("No. of Characters : %d\n",cc);
printf("No. of Words : %d\n",wc);
printf("No. of Lines : %d\n",lc);

return 0;
AIM: To find the factorial of a given
integer using recursive function.
7 a) Write a program to find
factorial of a given integer using Description: n!=n*(n-1)*(n-2)
non-recursive function and …..*1
recursive function.
ALGORITHM:
AIM: To find the factorial of a given number Step 1: start
using non-recursive function Step 2: read n
Step 3: call sub program
Description: n!=n*(n-1)*(n-2)…..*1 f=fact(n)
Step 4: print the f value
ALGORITHM: Step 5: stop
Step 1: Start Sub program fact(n):
Step 2: Read n Step 1: if n=0 return 1
Step 3: Call fact(n) goto step 6
to main program
Step 4: Store result in “f”
Step 2: return
Step 5: Print “f” goto step 10
Step 6: Begin //sub program n*fact(n-1) to main
Initialize f ← 1 program
Step 7: for i is 1 to n by step 2
Step 8: Calculate f = f*i PROGRAM:
Step 9: return “f” #include<stdio.h>
End
Step 10: Stop int fact(int);
PROGRAM: write a program int main()
for factorial of a given integer {
using non-recursive function. int n,res;
#include<stdio.h> printf("ENETR A NUMBER:");
int fact(int); scanf("%d",&n);
int main() res=fact(n);
{ p
int n,i,f; rintf("
printf("ENTER A VALUE FOR n:"); THE
scanf("%d",&n); FACTO
f=fact(n); RIAL
printf("THE FACTORIAL OF A GIVEN NO OF A
IS..%d",f); GIVEN
return 0; NUMB
} ER IS..
int fact(int n) %d",re
{ s);
int i,f=1; return
for(i=1;i<=n;i++) 0;
f=f*i; }
return(f); int fact(int n)
} {
SAMPLE INPUT: if(n==0)
ENTER A VALUE FOR n: 5 return(1);
EXPECTED OUTPUT: else
THE FACTORIAL OF A GIVEN NUMBER return(n*fact(n-1));
IS..120 }
SAMPLE INPUT:
ENTER A VALUE FOR n 5 Program:
#include<stdio.h>
EXPECTED OUTPUT: int gcd(int a,int b);
THE FACTORIAL OF A GIVEN NUMBER IS..120 int main()
{
int a,b;
int r,t;
printf("Enter any two
integers");
scanf("%d%d",&a,&b);
r=gcd(a,b);
printf("GCD=%d",r);
return 0;
}
int gcd(int a,int b)
{
int t,rem;
while(1)
{
if(b>a)
{
t=a;
a=b;
b=t;
}
7 b) Write a program to find GCD if(b==0)
of two integers using non- return a;
recursive function and recursive else
function. {
rem=a%b;
Aim:To find the GCD of two given integers by
a=rem;
using the non recursive function
Description: }
GCD means Greatest }
Common Divisor. i.e the }
highest number which SAMPLE INPUT:
divides the given number enter the two numbers whose gcd is to
Ex: GCD(12,24) is 12 be found:5,25
Formula: GCD= product of numbers/
LCM of numbers
EXPECTED OUTPUT:
GCD of a,b is : 5
Algorithm:
Step 1: start
Step 2: read a,b
Aim: To find the Gcd of two given
Step 3: call sub program g=GCD(a,b) integers by using the recursive
Step 4: print the g value function
Step 5: stop Description: The greatest
Sub program: common divisor (gcd) of two or
Step 1: if b=0 return a to main more integers, when at least one
program of them is not zero, is the largest
Step 2: remainder=a%b positive integer that divides
Step 3: a=b, b=remainder goto thenumbers without a remainder.
Step 1 For example, the GCD of 8 and 12 is 4.
Algorithm:
Main program:
Step 1: start // return to the main program
Step 2: read a,b
Step 3: call the sub program GCD(a,b)
for print the value
Step 4: stop
Sub program: GCD(n,m)

Step 1: if n>m return GCD(n,m)


Step 2: if n==0 return m else
goto step 3
Step 3: return GCD (n,m%n)
Step 4: return to main program
Program:
#include<stdio.h>
int gcdrecursive(int m,int n)
{if(n>m)
return gcdrecursive(n,m);
if(n==0)
return m;
else
return gcdrecursive(n,m%n);
}
int main()
{
int a,b;
printf("enter the two numbers whose gcd is to be
found:"); scanf("%d%d",&a,&b);
printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub
program return 0;
}
SAMPLE INPUT:
Enter the two numbers whose gcd is to be found: 5 25
EXPECTED OUTPUT:
GCD of a,b is : 5

8a) Write a C program using user defined functions to determine whether


the given string is palindrome or not.

Aim: To determine if the given string is palindrome or not.


Description : Palindrome means string on reversal should be same as original
Ex: madam on reversal is also madam
Algorithm:
Step 1: start
Step 2: read string A
Step 3: copy string A into B
Step 4: reverse string B
Step 5: compare A &B
If A equals B to got step 6
else goto step 7
Step 6:print given string A is palindrome
Step 7:print given string is not palindrome
Step 8: stop
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (j=0,i = length - 1; i >= 0 ; i--,j++) {

reverse_string[j] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
{
flag = 0;
break;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
return 0;
}
SAMPLE INPUT:
Enter a string
madam
EXPECTED OUTPUT:
The length of the string 'madam' = 5
madam is a palindrome
8) b) Write a C Program to swap the values of two variables using
i) Call by Value ii) Call by Reference

Aim: To Write a C Program to swap the values of two variables


using i) Call by Value ii) Call by Reference
Algorithm:
Main Program:
Step 1: start
Step 2: read a,b
Step 3: c a l l s w a p ( a , b )
Step 4: Stop
Sub Program:
Step 5: t=a;
Step 6: a =b;
Step 7: b=t;
Step 8: print a ,b values
Step 9: return to main program

Program: write a program to swap using call by value


#include<stdio.h>
void swap(int , int); // Declaration of function
int main( )
{ int a,b;
printf(“Enter any two integers:”);
// call by value
swap(a,b); // a and b are actual parameters
}
void swap( int x, int y ) // x and y are formal parameters
{ int t ;
t=x;
x=y;
y=t;
printf ( "\nx = %d y = %d", x, y ) ;
}
SAMPLE INPUT:
Enter any two integers: 10 20
EXPECTED OUTPUT:
x=20 y=10

ii) Program to swap two number using call by


reference. #include <stdio.h>
void swap(int *a, int
*b) { int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{ int num1,num2;
printf("Enter any Two Integers:");
scanf("%d%d",&num1,&num2);
swap(&num1,&num2);
printf("Number1 = %d\n",num1);
printf("Number2 = %d",num2);
return 0;
}
SAMPLE INPUT:
Enter any two integers: 2 3
EXPECTED OUTPUT:
Number1 = 3
Number2 = 2

9 a)Write a program to find the sum of list of elements read from


keyboard using dynamic memory allocation function malloc().

AIM: To find the sum of list of elements read from keyboard using dynamic memory
allocation.
Description: Consider an integer array, by considering the name of the array as
initial address of the first element, addition will be performed.
ALGORITHM:
Step 1:start
Step 2: read no. of elements N
Step 3:Dynamically allocate memory p=(int*)malloc(n*sizeof(int));
Step 4:read values into array
Step 6:sum=0
Step 3: for(i=0;i<N;i++)
Step 4: sum=sum+*(p+i)
step 5:print sum
step 6 stop

PROGRAM:
#include<stdio.h>
#include<malloc.h>
int main( )
{
int *p,sum=0,i,n;
printf("Enter How many elements to read:");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("enter elements into array");
for(i=0;i<n;i++)
scanf("%d",p+i);
for(i=0;i<n;i++)
sum=sum+*(p+i);
printf("the addition of array elements is %d", sum);
return 0;
}
SAMPLE INPUT:
Enter How many elements to read: 5
enter elements into array 1 2 3 4 5
EXPECTED OUTPUT:
the addition of array elements is 15
9 b)Write a program to perform subtraction of two matrices,
Design functions to perform read ,display and subtract

AIM: To perform addition of two matrices.


ALGORITHM:
Step 1: Start
Step 2: Read row size and column size of two matrices
Step 3: if row size and column size of matrices are not equal goto Step 8
Step 4: call read function to read matrix A
Step 5: call read function to read matrix B
Step 6: call subtract function to perform subtraction
Step 7: call display function to print the resultant matrix
Step 8: Stop
PROGRAM:
#include<stdio.h>
void read_matrix(int a[5][5],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
void sub_matrix(int a[5][5],int b[5][5],int c[5][5],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
c[i][j]=a[i][j]-b[i][j];
}
void display_matrix(int a[5][5],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int main( )
{
int a[5][5],b[5][5],c[5][5], i,j,p,q,r,s;
printf("ENTER ORDER OF A MATRIX\n");
scanf("%d%d",&p,&q);
printf("ENTER ORDER OF B MATRIX\n");
scanf("%d%d",&r,&s);
if(p==r&&q==s)
{
printf("ENTER A MATRIX\n");
read_matrix(a,p,q);
printf("ENTER B MATRIX\n");
read_matrix(b,p,q);
sub_matrix(a,b,c,p,q);
printf(" After Subtraction :\n");
display_matrix(c,p,q);
}
else
printf("Subtraction not possible");
}
SAMPLE INPUT:
ENTER ORDER OF A MATRIX 2 2
ENTER ORDER OF B MATRIX 2 2
ENTER A MATRIX
1 2
3 4
ENTER B MATRIX
01
2 3
EXPECTED OUTPUT:
After Subtraction:
1 1
1 1

10) a) Write a program to create book structure and display the contents
of a book.

AIM: Write a program to create book structure and display the contents of a book .
Program:
#include<stdio.h>
struct book
{
char bname[50];
int ssn;
int pages;
int rate;
};

int main()
{
struct book b1;
printf("Enter Book SSN Number:");
scanf("%d",&b1.ssn);
printf("Enter Number of pages:");
scanf("%d",&b1.pages);
printf("Enter price:");
scanf("%d",&b1.rate);
fflush(stdin);
printf("Enter Book Name:");
gets(b1.bname);
printf("\nName of the Book : %s\n ",b1.bname);
printf("\nSSN of the Book : %d\n ",b1.ssn);
printf("\nPages in the Book : %d\n ",b1.pages);
printf("\nPrice of the Book : %d\n",b1.rate);

return(0);
}
SAMPLE INPUT:
Enter Book SSN Number:123
Enter Number of pages:200
Enter price:100
Enter Book Name:c programming
EXPECTED OUTPUT:
Name of the Book : c programming
SSN of the Book : 123
Pages in the Book : 200
Price of the Book : 100

10) b) Write a C Program to Calculate Total and Percentage marks of


a student using structure.

Program:
#include<stdio.h>
struct student
{
int rl;
char nm[20];
int m1;
int m2;
int m3;
int t;
float per;
};
int main()
{
struct student a;
printf(" Enter RollNo, Name amd three sub marks\n");
scanf("%d%s%d%d%d",&a.rl,&a.nm,&a.m1,&a.m2,&a.m3);
a.t=a.m1+a.m2+a.m3;
a.per=a.t/3.0;
printf("rollno=%d\n",a.rl);
printf("Name=%sk\n",a.nm);
printf("m1=%d\n",a.m1);
printf("m2=%d\n",a.m2);
printf("m3=%d\n",a.m3);
printf("total=%d\n",a.t);
printf("per=%f\n",a.per);
return 0;
}
SAMPLE INPUT:
Enter RollNo, Name and three sub marks
12 rama 30 40 50
EXPECTED OUTPUT:
rollno=12
Name=rama
m1=30
m2=40
m3=50
total=120
per=40.000000

11) a)Write a C program that uses functions to perform the


following operations:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complex
AIM: To perform arithmetic operations on complex numbers
Complex numbers of type a+ib
Addition: (a+ib)+(x+iy)=a+x+i(b+y)
Subtraction: (a+ib)-(x+iy)=a-x+i(b-y)
Multiplication: (a+ib)*(x+iy)= ax-by+i(ay+bx)
Division ( +i )*( −i ) ( + )+i( − )
+i −i + −

(a+ib)/(x-iy) = * = 2 2
= 2 2
= 2
+i 2 2

+i −i + + + 2 +

ALGORITHM:

Step 1:start
Step 2: Read Two complex numbers c1, c2
Step 3: c3=c1+c2
Step 4: print c3
Step 5: c3=c1-c2
Step 6: print c3
Step 7: c3=c1*c2
Step 8: print c3
Step 9: c3=c1/c2
Step 10: print c3
Step 11: print c
Step 12: stop

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct complex
{
float real,img;
};

/*code for reading complex number*/


struct complex read_complex()
{ struct complex c;
printf("enter real part of complex number");
scanf("%f",&c.real);
printf("enter Imaginary part of complex number");
scanf("%f",&c.img);
return c;
}

/*code for adding complex numbers*/


struct complex add_complex(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}

/*code for subtraction of complex numbers*/


struct complex sub_complex(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real-c2.real;
c3.img=c1.img-c2.img;
return c3;
}

/*code for multiplication of complex numbers*/


struct complex mul_complex(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real*c2.real-c1.img*c2.img;
c3.img= c1.img*c2.real+c2.img*c1.real;
return c3;
}

/*code for division of complex numbers*/


struct complex div_complex(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real= (c1.real*c2.real+c1.img*c2.img)/(c2.real*c2.real+c2.img*c2.img);
c3.img= (c1.img*c2.real-c1.real*c2.img)/(c2.real*c2.real+c2.img*c2.img);
return c3;
}
/*code for display of complex number*/
void display_complex(struct complex c)
{
char sign;
printf("The result is:");
if(c.img<0)
{
sign='-';
c. img=-c.img;
}
else
sign='+';
printf("%5f%ci%5f",c.real,sign,c.img);
}
int main()
{
int choice;
struct complex a,b,c;
while(1)
{
printf("\n \n");
printf("|Menu for operation complex numbers|\n ");
printf(" \n");
printf("1.Addition \n ");
printf("2.Subtraction \n ");
printf("3.Multiplication \n ");
printf("4.Division \n ");
printf("5.Clear Screen \n ");
printf("6.Exit Menu \n ");
printf("Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("You Have Selected Addition operation on complex Numbers\n");
printf("Enter First complex number\n"); a=read_complex();
printf("Enter Second complex Number\n");
b=read_complex();
c=add_complex(a,b);

display_complex(c);
break;
case 2:printf("You Have Selected Subtraction operation on complex Numbers\n");
printf("Enter First complex number\n");
a=read_complex();
printf("Enter Second complex Number\n");
b=read_complex();
c=sub_complex(a,b);
display_complex(c);
break;
case 3:printf("You Have Selected Multiplication operation on complex Numbers\n");
printf("Enter First complex number\n");
a=read_complex();
printf("Enter Second complex Number\n");
b=read_complex();
c=mul_complex(a,b);
display_complex(c);
break;

case 4:printf("You Have Selected Division operation on complex Numbers\


n"); printf("Enter First complex number\n");
a=read_complex();
printf("Enter Second complex Number\n");
b=read_complex();
c=div_complex(a,b);
display_complex(c);
break;
case 5: system(“cls”);
break;
case 6: exit(0);
default: printf("Invalid choice");
}
}
}
SAMPLE INPUT:

|Menu For Operation Complex Numbers|

1. Addition
2.Subtraction
3.Multiplication
4.Division
5.Clear Screen
6.Exit Menu
Enter Your Choice:
Enter Your Choice: 1
You Have Selected Addition Operation On Complex Numbers
Enter First Complex Number
Enter Real Part Of Complex Number1
Enter Imaginary Part Of Complex Number2
Enter Second Complex Number
Enter Real Part Of Complex Number1
Enter Imaginary Part Of Complex Number2

EXPECTED OUTPUT:
THE RESULT IS:2.000000+I4.000000

11b ) Write a C program to reverse the first n characters in a file.


(Note: The file name and n are specified on the command line.)

Aim: To reverse the first n characters in a file

Algorithm:
Step 1: Start
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 cannot 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
Program:
#include <stdio.h>
#include <string.h>
#include <process.h>
#include <stdlib.h>
int 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)
{
puts("Improper number of arguments.");
exit(0);
}
fp = fopen(argv[1],"r");
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
k=atoi(argv[2]);
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';
return 0;
}
SAMPLE INPUT:
Input text file:
source.txt:
this is source
EXPECTED OUTPUT:
Command line arguments
C:\TURBOC~1\Disk\TurboC3\BIN>week11b source.txt 14
ecruos si siht
12)a) Write a C program to copy the contents of one file to another.

Aim: Program which copies one file to another


Algorithm:
Step 1: Start
Step 2: read file1,file2
Step 3: open source file in read mode
Step 4: if NULL pointer, then print source file can not be open
Step 5: open destination file in write mode
Step 6: if NULL pointer, then print destination file can not be open
Step 7: read a character from source file and write to destination file until EOF
Step 8 : Close source file and destination file
Step 9: Stop
Program:
#include<stdio.h>
#include<process.h>
int main()
{
FILE *ft,*fs;
int c=0;
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
{
printf("Source file opening error\n");
exit(1);
}
else
if(ft==NULL)
{
printf("Target file opening error\n");
exit(1);
}
while(!feof(fs))
{
fputc(fgetc(fs),ft);
c++;
}
printf("%d bytes copied from 'a.txt' to 'b.txt'",c);
c=fcloseall();
printf("%d files closed",c);
return 0;
}

SAMPLE INPUT:
a.txt
An array is a collection of elements of similar datatypes

EXPECTED OUTPUT:
57 bytes copied from ‘a.txt’ to ‘b.txt’
2 files closed
12 b) Write a program to merge two files into a third file.

Algorithm:
Step 1: Start
Step 2: read file1,file2,file3
Step 3: open file1 in read mode
Step 4: if NULL pointer, then print source file cannot be open
Step 5: open file3 in write mode
Step 6: if NULL pointer, then print file3 cannot be open
Step 7: read a character from file1 and write to file3 until EOF
Step 8 : Close file1
Step 9: open file2 in read mode
Step 10: if NULL pointer, then print source file can not be open
Step11: read a character from file2 and write to file3 until EOF
Step 12: Close file2 and file3
Step 13:Stop

Program :
#include<stdio.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char file1[20],file2[20],file3[20],ch;
puts("Program to merge two files. ..\n");
puts("Enter first file name:");
gets(file1);
puts("Enter Second file name:");
gets(file2);
puts("Enter Destination file name:");
gets(file3);
fp1=fopen(file1,"r");
fp2=fopen(file2,"r");
fp3=fopen(file3,"w");
if(fp1==NULL&&fp2==NULL)
printf("Error opening file1 and file2 ..... \n");
else
{
if(fp3==NULL)
printf("Error in creating destination file. .. \n");
else
{
while((ch=fgetc(fp1))!=EOF)
putc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
putc(ch,fp3);
}
printf("File Merging Sucessfull ...");
fcloseall();
}
return 0;
}

You might also like