PART-B
1.Programtofindthelengthofastringwithoutusingbuiltinfunction.
Algorithm:
Step1: START
Step 2: [declare and initial the variables, if required]str[50]:character
i,len<— 0:integer
Step 3: [input value to the string using gets()]
readstr
Step4:[compute/calculate thelength]FOR(i<—
0tostr=NULL)DO
Len <— len+1
NEXTi
Step 5: [display the output i.e., length of string]
print“Lengthofstringis”len
Step6: END
Flowchart:
Program 1:
#include<stdio.h>
void main()
{
char str[50];
inti,len=0;
clrscr();
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
len++;
printf("The length of the string is %d",len);
getch();
}
Output:
Enterastring
C-Programming
Thelengthofthestringis13
2.Program to demonstrate string functions.
Algorithm:
Step 1: START
Step 2: [declare the variables]
str11[20], str2[20], str3[20] : character
len, K : integer
Step 3:[input 1st string ] read STR1
Step 4 :[input 2nd string ] read STR2
Step 5 :[display the string lengths]
print “Length of a string is “,strlen(STR1)
print “Length of a string is “, strlen(STR2)
Step 6 :[compare two strings] K= strcmp (STR1, STR2)
IF (K = 0) THEN
print “both are equal “ ELSEIF (K>0)
print str1 “>” str2
ELSE
print str1 “<” str2
ENDIF
Step 7 : [display the duplicate]
strcat (STR1,STR2)
print “concatenation of two strings“
STR1 strcpy(STR3,STR2)
print “duplicate string is “ STR3
Step 6: STOP
Flowchart:
Program:
#include <stdio.h>
# include <string.h>
void main()
{
char str1[80],str2[80],str3[80];
intlen,k;
clrscr();
printf("\n Enter first string :");
gets(str1);
printf("\n Enter second string :");
gets(str2);
printf("\n Length of %s is %d",str1,strlen(str1));
printf("\n Length of %s is %d",str2,strlen(str2));
k=strcmp(str1,str2);
if(k==0)
printf("\n Both are equal");
else if(k>0)
printf("\n %s > %s ",str1,str2);
else
printf("\n %s < %s ",str1,str2);
strcat(str1,str2);
printf("\n Concatenation of two string = %s",str1);
strcpy(str3,str2);
printf("\n Duplication of second string = %s", str3);
getch();
}
Output-1:
Enter first string : BCA
Entersecondstring:BSc
Length of BCA is 3Length
of BSc is 3BCA<BSc
Concatenation of two string =
BCABScDuplicationofsecond string=BSc
Output-2:
Enter first string :
computerEntersecondstring:Scie
nce
Lengthofcomputeris8Length
of Science is
7computer>Science
Concatenationoftwostring=computerScience
Duplicationofsecond string=Science
3.Program to demonstrate pointers in C
Algorithm
Step 1: START
Step 2 : [declare variables]
A, *P, **Q: integer
Step 3 : [Assigning initial values]
P <- &A
Q <- &P
**Q <- 10
Step 4 : [display values and address of variables]
print “ value of a “ A
[display using address operator]
print “value of a using ampersand “, &A
[display using pointer variable]
print “value of a using pointer“, *P
[display using pointer to a pointer variable]
print “value of a using pointer to a pointer“, **Q
[display using address of operator to a pointer pointing another pointer variable]
print “value of a “, **(&P)
[display address using address of operator with a variable]
print “address of a “, &A
[display address using pointer variable]
print “address of a “, P
[display address using pointer to another pointer variable ]
print “address of a “, *Q
[increment the value using pointer] (*P )++
print “increase the value of a by 1”, *p
Step 5: STOP
Flowchart:
Program:
#include <stdio.h>
void main()
{
inta,*p,**q;
clrscr();
p=&a;
q=&p;
**q=10;
printf("\n Value of a = %d",a);
printf("\n Value of a = %d",*(&a));
printf("\n Value of a = %d",*p);
printf("\n Value of a = %d",**q);
printf("\n Value of a = %d",**(&p));
printf("\n Address of a = %u",&a);
printf("\n Address of a = %u",p);
printf("\n Address of a = %u",*(&p));
printf("\n Address of a = %u",*q); (*p)++;
printf("\n Increase the value of a by 1 = %d",*p);
getch();
}
Output:
Value of a =
10Value of a =
10Value of a =
10Value of a =
10Valueofa=1
0
Address of a =
65524Address of a =
65524Address of a =
65524Addressofa=6
5524
4.Program to check a number for prime by defining isprime( ) function
.Algorithm:main()
Step1: START
Step2:[inputanumber]
readn
Step 3:
[Callsubroutine
]isprime(N)
Step4:
[endofmainprocedure]
STOP
Algorithm:isprime (intn)
Step1:START[Subroutinestarts]
Step2: [declarevariable]
FLAG<-0:integer
Step 3: [computation to find
prime]FOR(I<-
2TON/2)DO
IF(N%I=0)THEN
FLAG<-1
GOTO Step4
ENDIF
NEXTI
Step4:IF(n=1)THEN
Print“1isneitherprimeorcomposite”
ELSEIF(flag=0)
THEN
printn“isaprimenumber”
ELSE
ENDIF
Step5 :STOP[EndofSubroutine]
Flowchart:
Program:
#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
isprime(n);
getch();
}
isprime(int n)
{
int flag=0,i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(n==1)
printf("1 is neither prime nor composite");
else
{
if(flag==0)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
}
}
5.Programtoread,displayandtofindthetraceofasquarematrix
Algorithm:
Step1: START
Step2: [declareandinitialize]
A[10][10],R,C,I, J,SUM<-0:integer
Step3:
[inputvaluesforrowsandcolsofmatrix
]readR,C
Step4: [checkwithaconditionandinputvaluesformatrix]
IF(R=C)THEN
a. FOR(I<-0TO R)DO
FOR(J<-0TO C )DO
readA[I][J]
NEXT
JNEXTI
b. [now display the
matrix]FOR(I <-TO
R)DO
FOR(J<-TO R)DO
print“\t”a[i][j]
ElseEndif
NEXTJ
print “\n”NEXTI
c. [Find the trace of a square matrix]For(I<-
0TOR)DO
SUM<-SUM+A[I][I]
NextI
d. print “the trace of the matrix is“ SUM
print“Itisnot asquarematrix “
Step5:STOP
Flow chart
Program
#include<stdio.h>
#include<conio.h>
void main()
{
inta[10][10],r,c,i,j,sum=0;
clrscr();
printf("Enter the number of rows and columns:\n");
scanf("%d%d",&r,&c);
if(r==c)
{
printf("Enter %dX%d elements in the matrix\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("The matrix entered is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
printf("%6d",a[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
sum=sum+a[i][i];
printf("The trace of the matrix is %d",sum);
}
else
printf("It is not a square matrix!");
getch();
}
Output-1:
Enterthenumberofrowsandcolumns:2 2
Enter 2X2 elements in the
matrix1 2 3 4
The matrix entered
is:1 2
3 4
Thetraceofthematrixis5
Output-2:
Enter the number of rows and
columns:3 3
Enter 3X3 elements in the
matrix123456789
The matrix entered
is:1 2 3
4 5 6
7 8 9
Thetraceofthematrixis15
6. Program to read, display and add two mxn matrices using functions
Algorithm: main()
Step 1: STRAT
Step 2: [declare and initialize]
M1[10][10], M2[10][10], M3[10][10], ROW, COL :integer
Step 3: [input values for rows and columns of matrices] read ROW, COL
Step 4: [call a subroutine to input matrix-1 values] call readarray(M1, ROW, COL)
Step 5: [call a subroutine to input matrix-2 values] call readarray(M2, ROW, COL)
Step 6: [call a subroutine to add two matrix] call addarray(M1, M2, M3, ROW, COL)
Step 7: [display matrix-1 using subroutine] call printarray(M1, ROW, COL)
Step 8: [display matrix-2 using subroutine] call printarray(M2, ROW, COL)
Step 9: [display matrix-3 using subroutine] call printarray(M3, ROW, COL)
Step 10: [end of the main procedure] END
Algorithm:readarray(A[10][10]: integer, ROW: integer, COL: integer)
Step 1: START
Step 2: [initial input variables] I <- 0, J <- 0: integer
Step 3: [read the value to the martix] FOR (I <- 0 TO ROW) DO
FOR (J <- 0 TO COL) DO
read A[I][J]
NEXT J
NEXT I
Step 4: END
Algorithm:addarray(M1[10][10]: integer, M2[10][10]: integer, M3[10][10]: integer, ROW: integer, COL:
integer)
Step 1: START
Step 2: [initial and declare variables] I <-0, J <-0: integer
Step 3: [add the matrices]
FOR (I <- 0 TO ROW) DO
FOR (J <- 0 TO COL) DO
M3[I][J]=M1[I][J]+M2[I][J]
NEXT J
NEXT I
Step 4: END
Algorithm:printarray(A[][]: integer, ROW: integer, COL: integer)
Step 1: START
Step 2: [initial variables]
I <- 0, J <- 0: integer
Step 3: [display the values in matrix- form] FOR (I <- 0 TO ROW) DO
FOR (J <- 0 TO COL) DO
print “\t” A[I][J]
NEXT I
Step 4: END
Program
#include<stdio.h>
#include<conio.h>
void readarray(int a[10][10],introw,int col)
{
inti,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}}}
void addarray(int m1[10][10],int m2[10][10],int m3[10][10],int row, int col)
{
inti,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j]=(m1[i][j]+m2[i][j]);
}}}
void printarray(int m[10][10],introw,int col)
{
inti,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%6d",m[i][j]);
}
printf("\n");
}}
main() Output:
{
int m1[10][10],m2[10][10],m3[10][10],row,col; Enterorderofrows:
clrscr(); 2
printf("Enter order of rows:\n"); 2
scanf("%d%d",&row,&col); Enter4 elementsformartix1:
printf("Enter %d elements for martix 1: \n",row*col); 1234
readarray(m1,row,col); Enter4 elementsformatrix2:
printf("Enter %d elements for matrix 2: \n",row*col); 1234
readarray(m2,row,col); addarray(m1,m2,m3,row,col); Theenteredmatrix1is:1
printf("The entered matrix 1 is:\n"); 2
printarray(m1,row,col); 3 4
printf("The entered matrix 2 is:\n"); Theenteredmatrix2is:1
printarray(m2,row,col); 2
printf("The Addition of matrix is:\n"); 3 4
TheAdditionofmatrixis:
printarray(m3,row,col);
2 4
getch();
6 8
}
7.Program to read, display and multiply two mxn matrices using functions
Algorithm: main()
Step 1: START
Step 2: [declare variables]
A[10][10], B[10][10], C[10][10], M, N, P, Q: integer
Step 3: [input values for the variables] read M, N, P, Q
Step 4: [input values for matrix- A] call readarray (A, M, N)
Step 5: [input values for matrix- B] call readarray (B, P, Q)
Step 6: [multiply two matrices]
IF(N != P) THEN
print “matrix multiplication not possible”
ELSE
END IF
call multiarray(A, B, C, M, N, P, Q)
Step 7: [display Matrix- A]
call printarray(A, M, N)
Step 8: [display Matrix-B]
call printarray(B, P, Q)
Step 9: [display Matrix- C]
call printarray(C, M, Q)
Step 10: END
Algorithm: multiarray(A[][]: integer, B[][]: integer, C[][]: integer, M: integer, N: integer,
P: integer, Q: integer)
Step 1: START
Step 2: [declare variable]
I <- 0, J <- 0, K <- 0: integer
Step 3: [multiply two matrices] FOR (I <- 0 TO P) DO
FOR (J <- TO Q) DO
C[I][J] <- 0
FOR (K <- 0 TO N) DO
C[I][J] <- C[I][J] + A[I][K] *B[K][J]
NEXT I
Step 4: END
NEXT J
NEXT K
Algorithm: printarray(M[10][10]: integer, P: integer, Q: integer)
Step 1: START
Step 2: [declare the variable] I, J: integer
Step 3: [display the matrix in matrix form] FOR (I <- 0 TO P) DO
FOR (J <- 0 TO Q) DO
print “\t” M[I][J]
NEXT J
print “\n”
NEXT I
Step 4: END
Algorithm: readarray(M[10][10]: integer, P: integer, Q: integer)
Step 1: START
Step 2: [declare of the variables] I, J: integer
Step 3: [input values into a matrix] FOR (I <- 0 TO P) DO
FOR (J <- 0 TO Q) DO
read M[I][J]
NEXT J
NEXT I
Step 4: END
Flow chart
Program
#include<stdio.h>
#include<conio.h>
void readarray(int a[10][10],introw,int col)
{
inti,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}}}
void mularray(int m1[10][10],int m2[10][10],int m3[10][10],int row, int col)
{
inti,j,k;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j]=0;
for(k=1;k<=col;k++)
{
m3[i][j]=m3[i][j] + m1[i][k] * m2[k][j];
}}}}
void printarray(int m[10][10],introw,int col)
{
inti,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%6d",m[i][j]);
}
printf("\n");
}} Output:
main()
{ Enterorderofrows:
int m1[10][10],m2[10][10],m3[10][10],row,col; 33
clrscr(); Enter9 elementsformatrix1:
printf("Enter order of rows:\n"); 12345678 9
scanf("%d%d",&row,&col); Enter9 elementsformatrix2:
printf("Enter %d elements for matrix 1: \n",row*col); 98765432 1
readarray(m1,row,col); Theenteredmatrix1is:
printf("Enter %d elements for matrix 2: \n",row*col); 1 2 3
readarray(m2,row,col); 4 5 6
printf("The entered matrix 1 is:\n"); 7 8 9
printarray(m1,row,col); Theenteredmatrix2is:
printf("The entered matrix 2 is:\n"); 9 8 7
printarray(m2,row,col); 6 5 4
mularray(m1,m2,m3,row,col); 3 2 1
TheProductofmatrixis:
printf("The Product of matrix is:\n");
30 24 18
printarray(m3,row,col);
84 69 54
getch();
13811490
}
8. Program to read a string and to find the number of alphabets, digits, vowels, consonants, spaces
and special characters.
Algorithm:
Step 1: START
Step 2: [declare and initialize]
ALPHABET <- 0, DIGIT <- 0, VOWEL <- 0, CONS <- 0, BLANK <- 0, SPECIAL <- 0: integer
S[100]: character
Step 3 :[input a string using gets( )] read S
Step 4 :[logical examine each character of a given string ] FOR (I <- 0 TO S[I] != “\0” ) DO
IF ((S[I] >=65) AND (S[I] <= 90)) OR ((S[I] >= 97) AND (S[I] <=122)) ALPHA ++
IF (S[I] =’A’ OR S[I] = ‘A’ OR S[I] = ‘E’ OR S[I] = ‘E’ S[I] =’I’ OR S[I] = ‘I’ S[I] = ‘O’ OR S[I] =
‘O’ S[I] = ‘U’ OR S[I] = ‘ U)
VOWEL ++
ELSE
END IF
CONS ++
ELSE IF (S[I] >= 48)AND(S[I] <=57) THEN DIGIT ++
ELSE IF (S[I] =32)
BLANK ++
ELSE
END IF NEXT I
SPECIAL ++
Step 5 :[ display the value stored in variable ]
print “ total number of alphabets is “ ALPHA print “ total number of digit is “ DIGIT
print “ total number of vowels is “ VOWEL print “ total number of consonants is “ CONS print “ total
number of blankspaces is “ BLANK
print “ total number of special character is “ SPECIAL
Step 6: STOP
Flowchart
Program
#include<stdio.h>
#include<string.h>
void main()
{
char s[1000];
inti,alpha=0,digits=0,special=0;
int blank=0,vowels=0,cons=0;
clrscr();
printf("Enter the string:\n");
gets(s);
for(i=0;s[i]!=0;i++)
{
if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
{
alpha++;
if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||
s[i]=='U')
vowels++ ;
else
cons++;
}
else if(s[i]>=48&&s[i]<=57)
digits++;
else if(s[i]==32)
blank++;
else
special++;
}
printf("The number of alphabets are %d\n", alpha);
printf("The number of vowels are %d\n",vowels);
printf("The number ofconsonents are %d\n", cons);
printf("The number of digits are %d\n",digits);
printf("The number of blankspaces are %d\n", blank); Output-1:
printf("The number of special characters are %d\n", special); Enterthe string: C-Lab1
getch(); Thenumberofalphabetsare4Thenu
} mberofvowelsare1
Thenumberofconsonantsare3Then
umberofdigitsare1
Thenumberofblankspacesare1
Thenumberofspecialcharactersare1
Output-2:
Enter thestring: 10:30AM
Thenumberofalphabetsare2Th
enumberofvowelsare1
Thenumberofconsonantsare1Th
enumberofdigitsare4
Thenumberofblankspacesare1
Thenumberofspecialcharactersare1
9. Program to Reverse a String using Pointer
Algorithm : main()
Step 1: START
Step 2: [declare a string variable] S[100] : character
Step 3 : [input a string] read S
Step 4 : [jump to a subroutine reverse() ] call reverse(S)
Step 5: STOP
Algorithm : reverse (char *s)
Step 1: START
Step 2: [declare variable]
C, LEN: integer
*BEGIN, *END ,TEMP: character
Step 3: [assign to variable and call a subroutine] LEN<- strlength(S)
Step 4: [assigning]
BEGIN <- S END <- S
Step 5 :FOR (C <- 0 TO LEN) DO
END ++ NEXT LEN
Step 6: FOR (C <- 0 TO LEN/2)DO
TEMP <- *END
*END <- *BEGIN
*BEGIN <- TEMP BEGIN ++
END --
NEXT C
Step 7: STOP reverse( )
Algorithm :strlength (*PTR: character)
Step 1: START
Step 2: [declare variable]
C <- 0
WHILE ( *(PTR + C) != “NULL” ) DO BEGIN
C++ ENDWHILE
Step 3: STOP return C
Flowchart
Program
#include<stdio.h>
intstrlength(char*);
void reverse(char*);
main()
{
char s[100];
clrscr();
printf("Enter a string\n");
gets(s);
reverse(s);
printf("The reverse of the string entered is\n %s\n",s);
getch();
}
void reverse(char *s)
{
intlen,c;
char *begin,*end, temp;
len=strlength(s);
begin=s;
end=s; for(c=0;c<len-1;c++)
end++; for(c=0;c<len/2;c++)
{
temp=*end;
*end=*begin;
*begin=temp;
begin++;
end--;
}
}
intstrlength(char *ptr)
{
int c=0; while(*(ptr+c)!='\0')
c++;
return c;
}
Output-1:
Enter a stringC-Lab
ThereverseofthestringenteredisbaL-C
Output-2:
Enter a stringProgram
ThereverseofthestringenteredismargorP
10 Program to Swap Two Numbers usingPointers
Algorithm : main( )
Step 1: START
Step 2 : [declare]
A, B : integer
Step 3: [input values for variable] read A, B
Step 4: [display before swapping] print A, B
Step 5: [jump to the subroutine] call swap ( &A, &B)
Step 6 : [display after swapping] print A, B
Step 7 : STOP
Algorithm :swap (int *x, *y) Step 1: START
Step 2 :[declare variable]
TEMP : integer
Step 3: [swap values]
TEMP<- *X
*X<- *Y
*Y<- TEMP
Step 4: STOP
Flowchart
Program
#include<stdio.h>
void swap(int*,int*);
void main()
{
inta,b;
clrscr();
printf("Enter values for a and b\n");
scanf("%d%d",&a,&b);
printf("Before swapping: a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("After swapping :a=%d and b=%d\n",a,b);
getch();
} Output:
void swap(int *x,int *y)
{ Entervaluesforaandb10
int temp; 20
Beforeswapping:a=10andb=20A
temp=*x;
fterswapping:a=20andb=10
*x=*y;
*y=temp;
}
11.Programtodemonstratestudentstructuretoread&displayrecordsofnstudents.
Algorithm:
Step 1: START
Step 2: [assigning to a variable] ch<- ‘Y’
Step 3: [Accept records using a loop] WHILE (ch = ‘Y’ or ch = ‘y’) DO BEGIN
read rno read name read ch
ENDWHILE
Step 4: [Display the information]
print “Student Information”
Step 5: [loop]
FOR (I <- 0 TO N) DO BEGIN
print rno, name ENDFOR
Step 6: STOP
Flowchart
Program
#include<stdio.h>
struct student
{
char name[50];
int roll;
}s[2];
void main()
{
int i=0,n;
char ch='y';
clrscr();
printf("Enterstudentinformation\n");
while(ch=='y'||ch=='Y')
{
printf("Enter the roll number of the student:\n");
scanf("%d",&s[i].roll);
printf("Enterthenameofthestudent:\n");
scanf("%s",s[i].name);
i++;
printf("\nDoyouWantto continue[y/n]:");
fflush(stdin);
scanf("%c",&ch);
}
n=i;
printf("\nThestudentsinformationenteredare:\n");
printf("\nRollNumber \tNAME\n");
for(i=0;i<n;i++)
printf("%-5d\t\t%-20s\n",s[i].roll,s[i].name);
getch();
}
Output:
Enterstudentinformation
Entertherollnumberofthestudent:100
Enterthenameofthestudent:Dhanya
Do you Want to continue [y/n] :y
Entertherollnumberofthestudent:200
Enterthenameofthestudent:Asha
DoyouWanttocontinue[y/n]:n
Thestudentsinformationenteredare:
Roll Number NAME
100 Dhanya
200 Asha
12 Program to demonstrate the difference between structure & union.
Algorithm:
Step1: START
Step2:[inputvalues]
:readrno,name,sem
Step3: [displaythevalues]
:printrno,name,sem
Step4: [inputenroll number]
:readeno
Step5:[displayenroll number]
:printeno
Step6:[inputnamevalue]
:readname
Step7:[displaynamevalue]
:printname
Step8:[readsemvalue]
:readsem
Step9:[displaysem values]
:printsem
Step10:STOP
Flowchart
program
#include <stdio.h>
void main()
{
struct student
{
int rno;
char name[20];
int sem;
};
union emp
{
int eno;
char name[20];
long int sal;
};
Struct student p;
union emp q;
clrscr();
printf("\nEnterstudentrollNo,NameandSem");
scanf("%d%s%d",&p.rno,p.name,&p.sem);
printf("\nStudent detailsis:");
printf("\n %d \t %s \t %d\n", p.rno, p.name,p.sem);
printf("\n Enter Employee Number :");
scanf("%d",&q.eno);
printf("\n Employee Number = %d",q.eno);
printf("\n Enter Employee name :");
scanf("%s",&q.name);
printf("\n Employee name = %s",q.name);
printf("\n Enter Employee Salary :");
scanf("%ld",&q.sal);
printf("\n Employee Salary = %ld",q.sal);getch();
}
Output:
EnterstudentrollNo,NameandSem10
0
Niyata
1
Studentdetailsis:
100 Niyata 1
EnterEmployeeNumber:200
Employee Number =
200EnterEmployeename:As
ha
Employeename=Asha
Enter EmployeeSalary:39283