[go: up one dir, main page]

0% found this document useful (0 votes)
50 views18 pages

BCA Lab Project

BCA lab project

Uploaded by

rathikd143
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)
50 views18 pages

BCA Lab Project

BCA lab project

Uploaded by

rathikd143
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/ 18

266

Programming inc
Part - B

string withoutusing built in function


the length of a
1. Program to find
Description: andstore it in the
array
takes a string as input
Usingprogram
This for loop count the number of characters in the array and store the result in
avariable,
" Print the variable as output.
Algorithm:
Step 1: Start
Step 2: Declare variable a[80], I, C;
Step 3: Initialize c-0
Step 4: Input a
Step 5: Initialize i-0
Step 6: Repeat step 7 and 8until a[i]!=\0
Step 7: Increment c=c+1
Step 8: Increment i=i+1
Step 9: print c
Step 10: Stop
Program:
/* Length of a string without using built in function */
#include<stdio.h>
#include<conio.bh>
void main()

char a[80];
int i,c=0;
clrscr();
printf(Enter a string:n'");
gets(a);
for(i-0; a[i]!=\0";it+)
C++;
printf("Length of the string is %d",c);
getch);
Lab Manual
267
Output:
Trace I
Trace 2
Enter a string: Enter a string:
Computer Science BSCAND BCA

Length of the string is 16 Length of the string is 11 .

2. Program to demonstrate string


functions.
Description:
I nis program takes string as input and
in string functions. performs string operations using the built
This program demonstrate implementation of string built-in functions such
as
strlen), strcat(), strcpy), strcmpy) and strrev).
Algorithm:
Step 1: Start
Step 2: Declare variable stl[15],st2[15],ch,n;
Step 3: Input ch
Step 4: If ch match with case 1 goto step 9
Step 5: If ch mnatch with case 2 goto step 12
Step 6: If ch match with case 3 goto step 15
Step 7: If ch match with case 4 goto step 20
Step 8: If ch match with case 5 gotostep 23
Step 9: Input stl
Step 10:copy string using strcpy(st2,st2)
Step 11: Print stl, st2 goto 26
Step 12: Input stl, st2
Step 13: concatenate string using strcat(st2,st2)
Step 14: Print stl, st2 goto 26
Step 15: Input stl, st2
Step 16: n=strcmp(st1,st2)
Step 17: If (n=-0), Print ("string I and string 2 are equal") goto 26
Step 18: if(n<0), Print ("string 1 is smaller than string 2") goto 26
Step 19: if(n>0),Print ("string 1 is greater than string 2") goto 26
Step 20: Input st1
Step 21: n=strlen(st1)
Step 22:Print n goto 26
Step 23: Input st1
208

Step 24 Revose a Ntting using stnev(stl) Proqrammtng ln e


Ste 20. Sto
Progam:
Popmn to demonstate string lunctiony */
Minclude stulio.h
Wncude- string.h
Wincudev0nlo.h
vod oain()

char stl||S],st2| 1S):


int ch,n;
clrser):
print ("***** String Manipulation ***** \n'"):
print".String Copy \n"):
printl"2.String Concatenation n");
printl"3.String Compare \n'"):
print"4.String Length \n");
print"5.String Reverse \n'"):
print("Enter the choice...n");
scanf("%d",&ch);
switch(ch)
case 1:

printf("Enter the string: \n "):


fMush(stdin);
gets(stl);
strcpy(s2,st1);
printf("\n Stringl is %s \t String2 is %s",st1 ,st2);
break;
case 2:

printf("Enter the string I\n");


flush(stdin);
gets(st1 );
printf("Enter the string 2\n"):
Mush(stdin);
gets(st2);
Lab Manual
269

strcat(stl,st2):
printfn Concatenation String is osstl).
break.
Case 3

printf(Enter the string I\n").


fllush(stdin):
gets(st l):
printf("Enter the string 2 \n"):
fHush(stdin);
gets(st2):
n-strcmp(st1,st2);
if(n=-0)
printf("\n string I and string 2 are equal'");
if(n<0)
printf("\n string l is smaller than string 2"):
if(n>0)
printf(n string Iis greater than string 2"):
break;
case 4:

printf(Enter the string \n");


flush(stdin);
gets(st1 );
n=strlen(st1 );
printf(n Length of the string is %d",n);
break;
case 5:

printf("Enter the string \n");


fflush(stdin);
gets(stl);
strrev(stl);
printf("n Reverse string is %s",stl);
break;
getch();
270

Output: Trace l
P
Trace 2
rograIrming n
***** String Manipulation String
L.String Copy
Manipulation
LString Copy 2.String Concatenation
2.String Concatenation
3.String Compare 3.String Compare
4. String Length 4.String Length
S.String Reverse 5.String Reverse
Enter thc choice... Enter the choice..
2
Enter the string l Enter the string
Computer Computer
Enter the string2
Science Reverse string is retupmoC
Concatenation String is Computer Science
3. Program to demonstrate pointers in C.
Description:
" Thepointer is a variable which stores the
address of another variahle
This program demonstrates creating, initializing and using
Algorithm: pointer variables.
Step 1: Start
Step 2: Declare variable x, *p, n
Step 3: Initialize x=300
Step 4: Let *p=&x
Step 5: Let n=*p
Step 6: Print address of x, P, n
Step 7: Print value of x, *p, n
Step 8: Stop
Program:
|/*Accessingaddress and value of variablesusing pointers */
#include<stdio.h>
#include<conio.h>
void main()
{
int x=300;
int *p=&x;
Lgb
Manual

int n=*p; 271


clrscr():

* Accessing Address of variables */


Drintf("\n The address of x =%u",&x):
printf("n The address of p = %u",&p):
printf(\n The address of n = %u"&n):
* Accessing value of variables */
printf(n The value of x=%d"x);
printf("n The value ofp =%d"*p);
printf(""n The value of n=%d'.n):
getch);

Output:
Trace 1
The address of x=6487580
The address of p=6487568
The address of n=6487564
The value of x=300
The value of p=300
The value of n=300

4. Program tocheck a number for prime by defîning isprime( ) function.


Description:
Anumber which is only divisible by itself and 1is known as primne number.
This program takes the input number and checks whether the number is prime
number or not using isprime) function.
Algorithm:
Step 1: Start
Step 2: Declare variable n
Step 3:Call function isprime() using c=isprime(n)
Step 4: if (c==2)
Print "Prime number"
else
Print "Not a prime number)
Step 5: Stop
Programming
272
Algorithm: | Function]
ic
Step 1: Declare variable
Step 2: Initialize c-0
Step 3: Initialize i-1
Step 4: Repcat step 5and step 6until i
Step 5: if (n%i)-0. Increment c-c1
Step 6: Increment i-i+|
| Step 7: return (c)
Program:
Program to check prime number or not using
#include<stdio.h> function *
#include<conio.h>
int isprime(int n);
void main()

int c,n;
clrscr():
printf(** \n Enter a number:");
scanf(%d",&n);
c-isprime(n);
if(c==2)
printf("n
else
%d is a prime
number",n);
printf("n %d is not a prime
getch); number",);
int isprime(int n)

int i,c-0;

for(i-1;i<=n;it )
if(n%i=-0)
C++;

return(c);
273
Lab Manual

Output: Trace 2
Trace
Enter anumber :10
Enter anumber :5 number
10 is not a prime
5is aprime number
functions.
read, display and add two MxN matrices using
5. Program to same
Deseription: another matrix if the two matrices have the
added to
" Amatrix can only be
dimensions
corresponding entries, and place this sum in the
the
To add two matrices, add matrix which results.
corresponding position in the
Algorithm:
Step 1: Start
10].ij,n,m
Step 2: Declare variable a[ 10][ 10].b[10][
Step 3: Input n, m
Step 4: Initialize i=0
until i<m
Step 5: Repeat step 6through step 10
Step 6: Initialize j-0
Step 7: Repcat step 8 and step 9 until j<n
Step 8: Inputa[i][)
Step 9: Increment j=j+1
Step 10: Increment i=j+1
Step 11: Initialize i=0
Step 12: Repeat step 13 through step 17 until i<mn
Step 13: Initialize j=0
Step 14: Repeat step 15 and step 16 until j<n
Step 15: Input b[iJ]
Step 16: Increment jj+1
Step 17: Increment i-i+1
Step 18: Callfunction using sum(a,b,n,m)
Step 19: Stop
Algorithm: (Function]
Step 1:Declare variable int c[10][ 10],ij
Step 2: Initialize j=0
Step 3: Repeat step 4 and step 8 until i<m
Step 4: Initialize j=0
Step 5: Repeat step 6 through step 7 until j<n
Step 6: Calculate c[][]=a[iJL]+b[i]ü]
274
Step 7: Increment
jj+1
Incrementi=i+1
Programming nc
Step 8:
i-0
Step 9: Initialize
step 15 until i
Step 10: Repeat step 1Ithrough
j=0
Step 1l: Initialize
step 12 until /<n
Step 12: Repeat step 13 and
Step 13: Print c[i][j]
j-j+1
Step 14: Increment
i=i+1
Step 15: Increment
Step 16: Return
s/
Program:
read, display and add two M XN matrices using functione
/* Program to
#include<stdio.h>
#include<conio.h>
a[10][10],int b[10][10],intn, intm);
voidsum (int
voidmain)

int af10]I[10],b[1 0J[10];


int ij,n,m;
clrscr(); Column: n"):
printf( Enter the Row and
scanf("%d%d,&m,&n);
\n);
printf(Enter the A matrix:
for(i-0;i<m;it+)

for(j-0:j<nj+)
scanf(9%d",&a[iJGD:

the B matrix: \n");


printf("Enter
for(i-0;i<m;it)
{
for(j-0:jnjtt)
canf("%d",&b[i][);
Lab Manual

void sum (int


a[10|[10],.int b[10][10],int n, int m)
int c[10][10].4j:
for(i-0;i<m;itt)
for(j-0;j<n;j++)
c[illil-a[i]i+b[i):
printf("Matrix Addition: \n");
|for(i=0;i<m;it+)
for(j-0j<njt+)
printf(t %d ",c[i]]);
printf("n");

Output:
Trace 1 Trace 2
Enter theRow and Column: Enter the Row and Column:
33
23
Enter the A matrix: Enter the A matrix:
3 4 5 4 5 6
67 8 7 89
9 10 11
Enter the B matrix: Enter the B matrix:
123 12 3
4 5 6 4 5 6
789 Matrix Addition:
Matrix Addition : 5 7 9
4 6 8 11 13 15
10 12 14
16 18 20
276

Programto read,
Description:
display and multiplytwo My
|x N matrices

multiplication indicates a row-by-column


Pfrougnctrar noinn,g In
using
matrix
The definition of are multiplied by the
cor espondimunlgfipliceantirnies
the i" row of A
where the entriesin then adding the results.
and
B
the ith column of
Algorithm: iN
Step 1:Start
Step 2: Declare
Step 3:Input
variable
ml.n1,m2,n2 10][|10],ij,ml,nl,m2,n2.
a[10][10],b[

Step 4: if (n|==m2) goto step5, otherwise print "Invalid


Step 5: Initialize i=0
matrix"
Step 6: Repeat step 7 through step 11 until i<m
Step 7: Initialize j=0
Step 8: Repeat step 9 and step 10 until jsnl
Step 9: Input a[i][i[
Step 10 Increment jjtl
Step 11: Increment i-i+1
Step 12: Initialize i-0
Step 13: Repeat step 14 through step 18 until i<m2
Step 14: Initialize j-0
Step 15: Repeat step 16and step 17 until j<n2
Step 16: Input b[i][;[
Step 17: Increment iit1
Step 18: Increment i=i+1
Step 19: Call function using multiply(a,b,m1,nl,n2);
Step 20: Stop
Algorithm: (Function]
Step 1: Declare variable int c[10][10],i,j,k
Step 2: Initialize i-0
Step 3: Repeat step 4 and step 12 until i<m1
Step 4: Initialize j=0
Step 5: Repeat step 6through step 11 until j<n2
Step 6: Initialize c[i]i]=0
Step 7: Initialize k=0
Step 8: Repeat step 9 and step 10until k<nl
Step 9: Calculate c[ilil=c[i][i] +a[i][k]*b|kJD]
277
gramy Step l0: Inerement kk+|
Step 1l: Increment j itI
Step 12: Increment i-it|
Step 13: Initialize i-O
Step 14: Repcat step 15 through step I9 until i<ml
Step 15: Initialize j=0
Step 16: Repeat step 17 and step 18 until /<n2
Step 17: Print c[iJi]
Step 18: Increment j=j+ 1
Step 19: Increment ii+1
Step 20: Return
Program:
* Programto read, aisplay and multiply two M x N matrices using functions *
#include<stdio.h>
#include<conio.h>
void multiply (int a[10][10],int b[10[10],int m1, int nl, int n2);
void main)

int a[ 10][ 10],b[10][10]:


int i,j,ml,nl,m2,n2;
clrscr);
printf(Enter First Matrix Row and Column: \n'):
scanf(%d%d",&m1,&nl );
printf(Enter Second Matrix Row and Column: n"):
scanf(%d%d",&m2, &en2);
if (nl=-m2)

printf(Enter first matrix: \n");


for(i=0;i<ml;itt)
for(j=0:j<nlj++)
scanf("%d",&a[i][i):
printf(Enter second matrix: \n"):
|for(i=0;i<m2;i++)
278
Programming in c
for(j-0:j<n2;j++)
scanf("%d",&b[i][i):
multiply(a,b,ml,nl,n2);

else
printf(\n Invalid Matrix");
getch();

void multiply (int a[10][10],int b[10][10], int ml, int nl, int n2)
{
int c[10][10],ij,k;
for(i-0;i<ml;it+)

for(j-0;j<n2;j++)

c[Ji-0;
for(k-0;k<n1;k++)

c[i]i]-c[ij[]ta[i][k]*b[k][j1:

printf("Matrix Multiplication: n");


for(i=0;i<ml;it+)
{
for(i-0;j<n2:j++)

printf("t %d ",c[ilD;

printf(n'");
Output:
Trace 1
Matrix Row and Column: Trace 2
Enter First Enter First Matrix Row
23 and Column:
3 3
Enter Second Matrix Row and Column:
Enter Second Matrix Row
2 3 and Column:
3 3

Enterfirst matrix:
Invalid Rows and Columns
1 23
4 5 6

789
Enter second matrix:
2 2 2
22 2
2 2 2
Matrix Multiplication:
12 12 12
30 30 30
48 48 48

7. Program to read a string and to find the number of


alphabets, digits, vowels,
consonants, spaces and special characters.
Description:
" In this program, find the number of vowels, consonants, digits, and white-spaces
in astring entered by the user is counted.
For example, consider the string, Kolar - 563101
" Here, Vowels=2, Consonants-3, Digits-6, Blank Spaces-2 and Special Characters=1
Algorithm:
Step 1: Start
Step 2: Declare st[80], i, v, C, d, b, s
Step 3: v-0,c-0,d--0,b-0,s-0
Step 4: Input st
Step 5: nitialize i=0
Step 6: Repeat step 7 and step 8 until st[i]!='\0'
Step 7: If(st[i]==A|st[i]=a'||st[i]=E'|st[i]==e||st[i]==T|st[i]=i|s[i]-=O||
st[i]==o' llst[i]==U'lst[i]-=")
v++;
282

Program:
Reverse of a string using pointer */
Programmlng Inc,
#include <stdio.h
#include< string.h>
#include<conio.h>
void main()

char strl[80],str2[80):
char *pl,*p2:
clrscr();
printf("Enter a String: \n");
gets(strl);
pl=strl+strlen(str l)-1;
p2-str2;
while(pl>=strl)

*p2=*pl;
p2++;
pl--;

*p2=\0';
printf("n Original string is %s",strl );
printf("n Reverse string is %s",str2);
getch();
}
Output: Trace 2
Trace 1
Enter a string: Enter a string:
Computer College
Originalstring is Computer Original string is College
Reverse string is retupmoC Reverse string isegelloC
using Pointers.
9. Program to Swap Two Numbers
Description: exchanging the values of two variables.
Swapping two number mneans of a is 10& value of b is
20. After
b. Value
two variables a & value ofb will become 10.
O Let us assume will become 20 &
Swapping the value of a
Lab
Manual
283
Algorithm:
Step 1: Start
Step 2: Declare variable x,y,z, *a, *b
Step 3: Input x,y
Step 4: Let a=&x
Step 5: Let b=&y
Step 6: Print *a, *b
Step 7: Let t-*a
Step 8: Let *a=*b
Step 9: Let *b=t
Step 10: Print *a, *b
Step 11: Stop
Program:
* Program for swapping */
#include<stdio.h>
#include<conio.h>
void main()

int x,y,t;
int *a,*b;
clrscr);
printf("Enter two integer numbers \n");
scanf("%d%d",&x,&y);
a-&x;
b-&y;
printf("\n Before Swapping a-%d b-%d" *a,*b);
t=*a;
*a=*b;
*b=t;
*a,*b);
printf("n After Swapping a=9%d b=%d"
getch():
}
Output: Trace 2
Trace 1
Enter two integer numbers
Enter twointeger numbers
50 100
10 20

Before Swapping a=50 b=100


Before Swapping a=10 b-20
After Swapping a=100 b=50
After Swapping a-20 b=10
284 read & displaa
ming in C
student structurc to records of n
10.Programto demonstrate
" In this program, a structurestudent is created, The structure has 7 members: name
Description:
students.
(int), tot (int) and avg
(string), ml (int), m2(int), m3 (float).
(string), rno structurcsst having 10 elements to store
Then,we created an
ofUsing
array of
information
a for loop, the programtakes the information off students from the user and
10 students.

structure.
stores it in the array of calculate the total and avg using the
loop,
Then using another fordisplayed on the screen.
entered by the user and
information
Algorithm:
Step 1: Start
name<10], rno[20], m1, m)
Step 2: Declare struct student with the members
tot, avg
Step 3: Declare variable st[10], i, n
Step 4: Input n
Step 5: Initialize i-0
i<n
Step 6: Repeat step 7 and step 8until
st[i].name,st[i].rno,st[i].ml,st[i].m2,st[i].m3
Step 7:Input
Step 8: Increment i=i+1
Step 9: Initialize i-0
Step 10: Repeat step 11 through step 13 until i<n
Step 11: calculate st[i].tot-st[i].m1+st[i].m2+st[i].m3
Step 12: Calculate st[i].avgst[i].tot/3.0;
Step 13: Increment i-it+1
Step 14: Initialize j-0
Step 15: Repeat step 16 and step 17 until i<n
Step 16: Print st[i].name, st[i].rno,st[i].m1, st[i].m2,st[i].m3,st[i].tot,st[i].avg
Step 17: Increnment i=i+1
Step 18: Stop
Program:
|/* Read & Display records of N students */
#include<stdio.h>
#include<conio.h>
struct student

char name[20],rno[10]:
int m1,m2,m3,tot;
float avg;
}st[10];
void main()
int n,i;
LabManual 285

clrser():
printf("Enter the Number of Students:ln'"):
scant("%d",&n);
for(i-0;i<n;it+)

nrintf("Enter the Name,Regno,Markl,Mark2, Mark3: \n");


scanf("%s%s%d%d%d"st[i].name,st[i].rno,&st[i].m l,&st[i].m2,&st[i].m3):
for(i=0;i<n;it+)

st[i].tot=st[i).ml+st[i].m2+st[i].m3;
st[i].avg-st[i].tot/3.0;

printf("t ***** Students Details *****"):


for(i-0;i<n;itt)

printf(""n%s \t%s \t %d \t %d \t %d \t od \t %5.2f",st[i].name, st[i.mo,st[i].ml,


st[i].m2,st[i].m3,st[i].tot,st[i].avg);
}
getch();

Output: Trace 2
Trace 1
Enter the Number of Students:
Enter the Number of Students:
2
Regno, Markl, Mark2, Enter the Name, Regno, Markl, Mark2,
Enter the Name, Mark3:
Mark3:
Sherly
Charles S10003
S10001 99
99 97
100 99
Markl, Mark2.
98 Markl, Mark2, Enter the Name, Regno,
Regno,
Enter the Name, Mark3:
Mark3: Vedha
James S10004
S10002 100
100
99
98 Students Details *****
95 sk sk * k *
****k *
100 295 98.33
*****Students Details
99.00 Sherly SI0003 99 97 99
98 297 99.33
99 100 Vedha S10004 100 100 98 298
Charles S1000I 99 95 100 294 98.00
SI0002
James

You might also like