BCA Lab Project
BCA Lab Project
Programming inc
Part - B
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
strcat(stl,st2):
printfn Concatenation String is osstl).
break.
Case 3
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
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
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)
for(j-0:j<nj+)
scanf(9%d",&a[iJGD:
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
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("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
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
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+)
st[i].tot=st[i).ml+st[i].m2+st[i].m3;
st[i].avg-st[i].tot/3.0;
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