DS Exp 1-35
DS Exp 1-35
DS Exp 1-35
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int n;
cin>>n;
if (n%2 ==0)
else
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 02
WAP FIND GREATEST NUMBER AMOUNG THREE NUMBERS
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"enter the three values";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"a is greatest");
}
else
if(b>=a && b>=c)
cout<<"b is greatest";
}
else
cout<<"c is greatest";
getch();
}
Output:-
CGC LANDRAN
EXPERIMENT 03
WAP TO CALCULATE FACTORIAL OF A NUMBER
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
cout<<"enter the no.";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<fact;
getch();
}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 04
WAP TO SWAP TWO NUMBER WITHOUT USING THREE VARIABLE
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,b;
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<a<<endl<<b;
getch(); }
OUTPUT:-
CGC LANDRAN
EXPERIMENT 05
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,b,temp;
cin>>a>>b;
temp=a;
a=b;
b=temp;
getch();}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 06
WAP TO FIND FABONACCI SERIES.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a=0,b=1,c,n,i;
cin>>n;
cout<<a<<endl<<b<<"\n";
for(i=0;i<n-2;i++)
{c=a+b;
cout<<c<<endl;
a=b;
b=c;}
getch();}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 07
WAP TO FIND THE GIVEN NUMBER IS PRIME OR NOT.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int n,i,count=0;
cin>>n;
for(i=1;i<=n;i++)
{if(n%i==0)
count++;}
if(count==2)
else
getch();}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 08
WAP TO FIND REVERSE OF THE NUMBER.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
long n,rev=0,d;
cin>>n;
while(n!=0)
{d=n%10;
rev=(rev*10)+d;
n=n/10;}
getch();}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 09
WAP TO FIND THE NUMBER IS PALINDROME OR NOT.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a,num,rem,sum=0;
cin>>num;
a=num;
while(num!=0)
{rem=num%10;
sum=(sum*10)+rem;
num=num/10;}
if(a==sum)
cout<<"number is palindrome";
getch();}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 10
WAP OF CALCULATOR USING SWITCH CASE.
SOURCE CODE::-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a,b,op;
cin>>a>>b;
cout<<"\n1.addition\n2.substraction\n3.multiplication";
cin>>(op);
switch(op)
case 1:
cout<<"\n addition="<<a+b;
break;
case 2:
cout<<"\n substraction="<<a-b;
break;
case 3:
cout<<"\n multiplication="<<a*b;
CGC LANDRAN
break;
case 4:
cout<<"\n division="<<a/b;
break;
default:
cout<<"default operation";
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 11
WAP TO INSERT AND DISPLAY ELEMENTS OF AN ARRAY.
ALGORITHM:-
[Initialize counter] Set K:=LB
Repeat steps 3 & 4 while K<=LB
[Visit element] apply
[Increment counter] Set K:=K+1
[End of step 2 loop]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int arr[5],n,i;
cout<<"enter the element of array";
cin>>n;
for(i=0;i<5;i++)
{
cin>>arr[i];
}
for(i=0;i<5;i++)
{
cout<<"\n elements of array"<<i<<" "<<arr[i];
}
getch();
}
CGC LANDRAN
OUTPUT:-
CGC LANDRAN
EXPERIMENT 12
WAP TO FIND MAX. ELEMENT & ITS LOCATION IN AN ARRAY.
ALGORITHM:-
Enter an elements of an array.
[Initialize] max:=arr[i] & loc:=0
Repeat step 3 & 4 while loc:=0 & max< arr[i]
For(i=1;i<=n;i++)
If (max<arr[i])
Then min=arr[i] & loc:=i
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int arr[5],i,max,loc=0,n;
cin>>n;
for(i=1;i<=n;i++)
cin>>arr[i];
max=arr[1];
CGC LANDRAN
for(i=1;i<=n;i++)
{if (max<arr[i])
max=arr[i];
loc=i;
cout<<"\n max="<<max;
cout<<"\n loc="<<loc;
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 13
WAP TO FIND MIN. ELEMENT & ITS LOCATION IN AN ARRAY.
ALGORITHM:-
Enter an elements of an array.
[initialize] loc:=0 & min=arr[1]
Repeat step 3 & 4 while loc:=0 & min>arr[i]
For( i=1;i<=n;i++)
If (min>arr[i] }
Then min=arr[i] &loc:=i
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int arr[5],i,min,loc=0,n;
cin>>n;
for(i=1;i<=n;i++)
cin>>arr[i]; }
min=arr[i];
for(i=1;i<=n;i++)
{if (min>arr[i])
{min=arr[i];
loc=i;
CGC LANDRAN
}}
cout<<"\n min="<<min;
cout<<"\n loc="<<loc;
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT - 14
Write a program to insert an element in an array in kth position
ALGORITHM :-
Enter an elements an array.
[Initialize counter] set J:=N
Repeat steps 3 & 4 while J>=K
[Move Jth element downwards] set LA [J+1] := LA [ J ]
[ Decrease counter ] set J =J-1
[ End of step 2 loop ]
[Insert elements ] set LA[K] := ITEM
[Reset N ] set N=N+1
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],n,i,iteam,k;
cin>>n;
for(i=1;i<=n;i++)
{cin>>a[i];
cout<<"elements of array";
CGC LANDRAN
for(i=1;i<=n;i++)
cout<<a[i[<<endl;
cin>>k;
cin>>iteam;
for(i=n;i>=k;i--)
{a[i+1]=a[i]; }
n=n+1;
for(i=1;i<=n;i++)
{cout<<a[i];}
getch(): }
OUTPUT:-
CGC LANDRAN
EXPERIMENT - 15
WAP TO DELETE AN ARRAY ELEMENT AT KTH POSITION
ALGORITHM:-
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],n,i,m,k;
cin>>n;
for(i=1;i<=n;i++)
{cin>>a[i];
for(i=1;i<=n;i++)
{cout<<a[i]<<endl;
CGC LANDRAN
cin>>k;
m=a[i];
for(i=k;i<=n-1;i++)
a[i]=a[i+1];
n=n-1;
cout<<"elements are";
for(i=1;i<=n;i++)
cout<<a[i];
getch();}
OUTPUT:-
CGC LANDRAN
EXPRIMENT- 16
WAP TO ADD ALL THE ELEMENTS OF AN ARRAY
ALGORITHM-
Enter the elements in an array.
[Initialize counter] setK:=LB, and Sum:=0
Repeat steps 3 & 4while K<=UB & Sum:=Sum +a[ i ]
[Visit element] Apply PROCESS to LA[K]
[Increase counter] Set K:=K+1
[End of step 2 loop ]
Exit
SOURCE CODE-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[10],i,n,sum=0;
cin>>n;
for(i=1;i<=n;i++)
{cin>>a[i];
cout<<"elements are";
for(i=1;i<=n;i++)
CGC LANDRAN
cout<<a[i]<<endl;
for(i=1;i<=n;i++)
{sum=sum+a[i];
cout<<"sum is"<<sum;
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 17
WAP TO COPY THE ELEMENTS OF ONE ARRAY INTO ANOTHER
ARRRAY.
ALGORITHM:-
Enter the elements of original array.
[Initialize counter] setK:=LB
Repeat steps 3 and 4 whileK<=UB
[Visit elements ]Apply PROCESS b[i]:=a[i]
[Increase counter ] Set K:=K+1
[ End of step 2 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i,n,a[10],b[10];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
CGC LANDRAN
b[i]=a[i];
for(i=0;i<n;i++)
cout<<b[i]<<" ";
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT-18
WAP TO PERFORM LINEAR SEARCH IN AN ARRAY.
ALGORITHM:-
Enter the elements of an array.
[ Initialize ] set count:=1 and LOC:=0
Repeat steps 3 & 4 while count <=N and LOC:=0
If
ITEM:= A[ I ] Then set LOC:=I
Else
set count := count++
[ End of step 2 loop ]
[Successful ]
If LOC:=0 Then
Else
write : Itemis present in an array and print location of ITEM is LOC.
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int n,a[20],i,ITEM,loc=0,count=1;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i];
CGC LANDRAN
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cin>>ITEM;
for(i=1;i<=n;i++)
{ if(a[i]==ITEM)
{ loc=i;
count++;
if(count!=0)
else
getch();
return 0; }
OUTPUT:-
CGC LANDRAN
EXPERIMENT-19
WAP TO PERFORM BINARY SEARCH IN AN ARRAY
ALGORITHM:-
Enter the elements of an array in sorted order.
BINARY(DATA,LB,UB,ITEM,LOC)
[Initialize segment variable] Set BEG:=LB,END:=UB and MID:=INT (BEG+EN/2)
Repeat steps 3 & 4 while BEG<=END and DATA[ MID ] != ITEM
If ITEM<DATA [MID] Then set END:=MID-1
Else
Set BEG:=MID+1
[End of IF statement]
Set MID:=INT [BEG+END/2]
[ End of step 2 loop]
If DATA[ MID ]:=ITEM then set LOC:=MID
Else
Set LOC:=NULL
[End of if structure ]
Exit
SOURCE CODE :-
#include<iostream.h>
#include<conio.h>
void main()
int a[10],i,n,beg=1,end,e,mid;
clrscr();
cin>>n;
CGC LANDRAN
cout<<"Enter the elements in sorted order : ";
for(i=1;i<=n;i++)
cin>>a[i];
cin>>e;
end=n;
mid=(end+beg)/2;
while((a[mid]!=e)&&(beg<=end))
if(a[mid]==e)
cout<<"locis:"<<mid<<endl;
break;
if(e>a[mid])
{beg=mid+1;}
else
{end=mid-1;}
mid=(end+beg)/2;
if(a[mid]==e)
CGC LANDRAN
{
else
getch();
OUTPUT :-
CGC LANDRAN
EXPERIMENT 20
WAP TO SWAPPING OF TWO ARRAY USING THIRD ARRAY.
ALGORITHM:-
Enter an elements in first array and second array.
[ Initialise ] C[I]:=TEMP
[Move fist element array into TEMP array] C[i]:=A[ i]
[Move second array elements into A[i] ] A[i] :=B[i]
[Move TEMO array elements into B[I] ] B[i]:=C[i]
[ end of step 2 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i,n,b[30],a[30],c[30];
cin>>n;
for(i=0;i<=n;i++)
{cin>>a[i];
for(i=0;i<=n;i++)
CGC LANDRAN
cin>>b[i];
for(i=0;i<=n;i++)
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
cout<<"\n the elements of the first array after swapping are = ";
for(i=0;i<=n;i++)
{cout<<a[i]<<" ";
cout<<" \n the elements of the second array after swapping are =";
for(i=0;i<=n;i++)
getch(); }
OUTPUT:-
CGC LANDRAN
EXPERIMENT 21
WAP TO BUBBLE SORT FOR ALPHABETS.
ALGORITHM :-
Repeat step 2 & 3 for K:=1 to N-1
Set PTR:=1 [ Initialize pass pointer PTR ]
Repeat while PTR<=N-K [execute pass ]
If DATA [PTR]>DATA[PTR+1] then interchange DATA[PTR]
And DATA[PTR+1]
[ end of if structure ]
Set PTR :=PTR+1
[ End of inner loop ]
[end of step 1 loop ]
Exit
SOURCE CODE :-
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
clrscr();
char a[30],temp;
int i,j,b;
cin>>a;
b=strlen(a);
for(i=0;i<b-1;i++)
CGC LANDRAN
for(j=0;j<b-i-1;j++)
if (a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}}
for(i=0;i<b;i++)
cout<<"\n"<<a[i];
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 22
WAP TO BUBBLE SORT FOR INTEGER.
ALGORITHM:-
Repeat step 2 & 3 for K:=1 to N-1
Set PTR:=1 [ Initialize pass pointer PTR ]
Repeat while PTR<=N-K [execute pass ]
If DATA [PTR]>DATA[PTR+1] then interchange DATA[PTR]
And DATA[PTR+1]
[ end of if structure ]
Set PTR :=PTR+1
[ End of inner loop ]
[end of step 1 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
cin>>n;
for(int i=1;i<=n;i++)
cin>>data[i];
int temp;
for(int j=1;j<=n;j++)
CGC LANDRAN
{
for(int k=1;k<=n-1;k++)
if( data[k]>data[k+1])
temp=data[k];
data[k]=data[k+1];
data[k+1]=temp;}
for(j=1;j<=n;j++)
cout<<"\n"<<data[j];
getch(); }
OUTPUT:-
CGC LANDRAN
EXPERIMENT 23
WAP TP REMOVE DUPLICATE ELEMENT.
ALGORITHM:-
Enter the elements of an array
[initialize ] Set A[i]:=[1] & A[j]:=i+1
Repeat step while A[i]:=A[j]
If
DATA[i]:=DATA[j]
Then
,DATA[J]:=REMOVE
Else
set i:=i+1
[end of if structure]
[end of step 2 loop]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr ();
int arr[20],i,j,k,size;
cin>>size;
cout<<"enter elements";
for(i=0;i<size;i++)
cin>>arr[i];
CGC LANDRAN
for(i=0;i<size;i++)
{ for(j=i+1;j<size;j++)
if
(arr[j]==arr[i])
{ for(k=j;k<size;k++)
{ arr[k]=arr[k+1]; }
size--; } }
for(i=0;i<size;i++)
cout<<arr[i];
getch(); }
OUTPUT:-
CGC LANDRAN
EXPERIMENT 24
ALGORITHM:-
Set a[i+1]=ITEM
[Reset N] N=N+1
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,item;
cout<<"enter the element of array";
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"enter the element at the end of the array";
cin>>item;
n=n+1;
for(i=1;i<n;i++)
{
CGC LANDRAN
a[n]=item;
}
n=n+1;
for(i=1;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
OUTPUT:-
CGC LANDRAN
EXPERIMENT:25
WAP TO DELETE AN ELEMENT WHOSE VALUE IS GIVEN.
ALGORITHM:-
Enter the size & element of array.
[Initialize counter ] I<=N.
[ Initialize variable] ITEM,LOC=0.
If ITEM=a[I].
[successful] set LOC=I.
Repeat for J=I to N-1.
[ Move J + 1st element upward] Set a[I] =a[ J+1].
[Reset the N number elements in array] Set N =N-1
Else ITEM is not in array .[Unsuccessful].
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a[10],n,item,loc=0;
cin>>n;
for(i=1;i<=n;i++)
cin<<a[i];
CGC LANDRAN
for(i=1;i<=n;i++)
cout<<" "<<a[i];
cin>>item;
for(i=1;i<=n;i++)
if(a[i]==item)
loc=i;
}}
for(i=loc;i<=n-1;i++)
a[i]=a[i+1];
n=n-1;
for(i=1;i<=n;i++)
cout<<" "<<a[i];
getch();
CGC LANDRAN
OUTPUT:-
CGC LANDRAN
EXPERIMENT 26
WAP TO FIND THE LOCATION OF A GIVEN ELEMENT.
ALGORITHM:-
Enter the elements of an array
[ Intialize ] LOC:=0
Repeat step 2 & 3 while LOC:=0
If
ITEM:=DATA[K] then set LOC:=K
Else
LOC:=0
[ End of if structure ]
[end of step 2 loop ]
Exit
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int arr[30],i,loc=0,n,item;
cin>>n;
for(i=1;i<=n;i++)
cin>>arr[i];
CGC LANDRAN
cin>>item;
for(i=1;i<=n;i++)
if(item==arr[i])
item=arr[i];
loc=i;
}}
cout<<"\n loc="<<loc;
getch();
OUTPUT:-
CGC LANDRAN
EXPERIMENT 27
ALGORITHM:-
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][2],i,j;
cout<<"Elements of matrix entered are:"<<endl;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Elements of matrix displayed are:"<<endl;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
CGC LANDRAN
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 28
ALGORITHM:-
Suppose A and B are two matrix arrays of order m x n, and C is another matrix
array to store the addition result. i, j are counters
Start
Read: m and n
Read: Take inputs for Matrix A[1:m, 1:n] and Matrix B[1:m, 1:n]
Repeat for i := 1 to m by 1:
Repeat for j := 1 to n by 1:
C[i, j] := A[i, j] + B[i, j]
[End of inner for loop]
[End of outer for loop]
Print: Matrix C
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],i,n,j;
cout<<"Enter the size of array a:";
cin>>n;
cout<<"Enter elements:";
for(i=0;i<n;i++)
CGC LANDRAN
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Enter the size of array b:";
cin>>n;
cout<<"Enter elements:";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
cout<<endl;
}
cout<<"Elements after adding:";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
CGC LANDRAN
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 29
WAP TO MULTIPLICATION OF TWO MATRICES
ALGORITHM:-
[Initialize] A(M,P) &B( P,N) matrix
Enter the elements of matrices A &B
Repeat steps 4 to 6 for I:=1 to M
Repeat step 5 to 6 for J:=1 to N
Set C(I,J):=0 [Initializes C(i,J) ]
Repeat for K:=1 to P
C(I,J):=C(I,J)+A[I,K]*B[K,J]
[End of inner loop ]
[end of step 3 loop]
[ end of step 4 loop]
Exit
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],i,j,n;
cout<<"Enter size of array a:";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
CGC LANDRAN
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Enter size of array b:";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
cout<<endl;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
cout<<endl;
}
cout<<"Elements after multiplication:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
CGC LANDRAN
cout<<endl;
getch();
}
OUTPUT:-
CGC LANDRAN
EXPERIMENT 30
ALGORITHM:-
Start.
Take inputs for Matrix a[i,j].
Print a [i , j]
Print a [j , i]
[Transpose is done]
Exit.
SOURCE CODE:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,n;
cout<<"Elements of matrix are:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Transpose of matrix:"<<endl;
for(i=0;i<3;i++)
CGC LANDRAN
{
for(j=0;j<3;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<endl;
}
getch();
}
OUTPUT:-
CGC LANDRAN