You are on page 1of 25

# BINARY SEARCH TECHNIQUE

If the element is found, then return 0 else return 1 using Binary Search Technique.
#include<iostream.h> #include<conio.h> int SEARCH(int A[],int s,int ele); void main() { clrscr(); int i,A[100],s,ele; cout<<"Enter the size of an Array"; cin>>s; cout<<"\nEnter the Elments of an Array:-\n"; for(i=0;i<s;i++) { cin>>A[i]; } cout<<"\nEnter the Element to be Searched:-\n"; cin>>ele; SEARCH(A,s,ele); cout<<SEARCH(A,s,ele); getch(); } int SEARCH(int A[],int s,int ele) { int i,p=-1,b=0,l=s-1,m; while(b<=l) { m=(b+l)/2; if(A[i]==A[m])

Page | 1

{ p=m; break; } else if(A[i]>A[m]) { b=m+1; } else { l=m-1; } } if(p==-1) { return 1; } else { return 0; } }

Page | 2

BUBBLE SORT
Arrange all the Even elements towards Left hand side & Odd elements towards Right hand side.
#include<iostream.h> #include<conio.h> void SORT(int A[],int s); void main() { clrscr(); int i,A[100],s; cout<<"\nEnter the size of an Array;-\n"; cin>>s; cout<<"\nEnter the Elments of an Array:-\n"; for(i=0;i<s;i++) { cin>>A[i]; } SORT(A,s); getch(); } void SORT(int A[],int s) { int i,j,t; cout<<"\nResult is:-\n"; for(i=0;i<s;i++) { for(j=0;j<s-1-i;j++) { if(A[j]%2!=0) {

Page | 3

## t=A[j]; A[j]=A[j+1]; A[j+1]=t; } } } for(i=0;i<s;i++) { cout<<A[i]<<" "; } }

INSERTION SORT
#include<iostream.h> #include<conio.h> void SORT(int A[],int s); void main() { clrscr(); int i,A[100],s; cout<<"Enter the size of an Array"; cin>>s; cout<<"Enter the Elments of an Array"; for(i=0;i<s;i++) { cin>>A[i]; } SORT(A,s); getch(); } void SORT(int A[],int s) {

Page | 4

## INSERTION IN AN ASCENDING ARRAY

#include<iostream.h> #include<conio.h> void INSERT(int A[],int s,int ele); void main() { clrscr(); int i,A[100],s,ele; cout<<"\nEnter the size of an Array:-\n"; cin>>s; cout<<"\nEnter the Elments of an Array:-\n";

Page | 5

for(i=0;i<s;i++) { cin>>A[i]; } cout<<"\nEnter the Element to be Inserted:-\n"; cin>>ele; INSERT(A,s,ele); getch(); } void INSERT(int A[],int s,int ele) { int i,p=s; for(i=0;i<s;i++) { if(ele<=A[i]) { p=i; break; } } for(i=s;i>p;i--) { A[i]=A[i-1]; } A[p]=ele; s++; cout<<"\nSorted Array is:-\n"; for(i=0;i<s;i++) { cout<<A[i]<<" "; }}

Merging
Page | 6

Wap in c++ to merge two arrays, first array should be in asc. Order and second in desc. Order, result should be in asc. Order.
#include<iostream.h> #include<conio.h> void main() { Clrscr(); void merge(int A[],int m,int B[],int n,int c[]); int A[50],B[50],C[50],mn=0,m,n; clrscr(); cout<<"\nEnter array size of A :_\n"; cin>>m; cout<<"\nEnter array elements of A in ascending :_\n"; for(int i=0; i<m; i++) { cin>>A[i]; } cout<<"\nEnter array size of B :-\n"; cin>>n; mn=m+n; cout<<"\nEnter array elements of B in descending :-\n"<<endl; for(i=0; i<n; i++) { cin>>B[i]; } merge(A,m,B,n,C); cout<<"\nSorted array of C is as follows ...... :-\n"; for(i=0;i<mn;i++) cout<<C[i]<<" "; getch();

Page | 7

void merge(int A[], int m, int B[], int n, int C[]) { int a,b,c; for(a=0, b=n-1, c=0; a<m && b>=0;) { if (A[a]<=B[b]) C[c++]=A[a++]; else C[c++]=B[b--]; } if (a<m) { while(a<m) C[c++]=A[a++]; } else { while(b>=0) C[c++]=B[b--]; }}

## #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sp=0,r,p;

Page | 8

cout<<"Enter a no. |- "; cin>>n; p=n; while(n>0) { r=n%10; sp=sp+(r*r*r); n=n/10; } if(p==sp) cout<<"Armstrong no."; else cout<<"Not a Armstrong no."; getch(); }

## WAP to create a pattern.

#include<iostream.h> #include<conio.h> Void main () { Clrscr (); Int i,j,s; For (i=1;i<=5;i++) { cout<<"\n"; for(s=1;s<=5-i;s++) { cout<<" "; } for(j=1;j<=i;j++) {

Page | 9

## Wap in c++ to create a pattern. @ @ @ @ @

@ @ @ @ @ @ @ @ @ @

#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j; for (i=0;i<=5;i++) { for(j=0;j<i;j++) { cout<<"@"<<" } cout<<"\n"<<"\t"; } getch(); } ";

Page | 10

WAP in c++ to accept no. and an arithmetic operator from the user display the output after performing the operation on the operator?

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,ans; char op; cout<<"\nEnter two no.:- \n"; cin>>a>>b; cout<<"\nEnter an arithmetic operator :- \n"; cin>>op; switch(op) { case '+': ans=a+b; cout<<"Sum is = "<<ans; break; case '-': ans=a-b; cout<<"Difference is = "<<ans; break; case '*': ans=a*b; cout<<"Product is = "<<ans; break; case '/': ans=a/b;

Page | 11

cout<<"quotient is = "<<ans; break; case '%': ans=a%b; cout<<"Remainder is = "<<ans; break; default: cout<<"WRONG ARITHMETIC OPERATOR"; } getch(); }

Reverse of a digit.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,r,s=0; cout<<"Enter a no.:- "; cin>>n; while (n>0) { r=n%10; s=s*10+r; n=n/10; } cout<<"reverse no. = "<<s; getch();

Page | 12

## Upper Right Triangle in an 2d array.

#include<iostream.h> #include<conio.h> void fn(int A[][30],int s); void main() { clrscr(); int i,j,s,A[30][30]; cout<<"\nEnter the size of the Matrix :-\n"; cin>>s; cout<<"\nEnter the elements:-\n"; for(i=0;i<s;i++) { for(j=0;j<s;j++) { cin>>A[i][j]; } } fn(A,s); getch(); } void fn(int A[][30],int s) { int i,j; cout<<"\nMatrix entered is:-\n"; for(i=0;i<s;i++) { for(j=0;j<s;j++)

Page | 13

{ cout<<A[i][j]; } cout<<" "<<"\n"; } cout<<"\nUpper right triangle is:-\n"; for(i=0;i<s;i++) { for(j=0;j<s;j++) { if(i<=j) { cout<<A[i][j]; } else { cout<<" "; } } cout<<"\n"; } }

Swap First half elements with Next half elements from a matrix.

## #include<iostream.h> #include<conio.h> void SWAP(int A[],int s); void main() {

Page | 14

clrscr(); int i,A[100],s; cout<<"Enter the size of an Array"; cin>>s; cout<<"Enter the Elments of an Array"; for(i=0;i<s;i++) { cin>>A[i]; } SWAP(A,s); getch(); } void SWAP(int A[],int s) { int i,t,j; for(i=0,j=s/2;i<s/2;i++,j++) { t=A[i]; A[i]=A[j]; A[j]=t; } for(i=0;i<s;i++) { cout<< <<A[i];

## Swap the element Divisible by 10 with its very Next element.

#include<iostream.h> #include<conio.h> void main() { clrscr();

Page | 15

int i,t,A[100],s; cout<<"Enter the size of an Array"; cin>>s; cout<<"Enter the Elements of an Array"; for(i=0;i<s;i++) { cin>>A[i]; } for(i=0;i<s-1;i++) { if(A[i]%10==0) { t=A[i]; A[i]=A[i+1]; A[i+1]=t; } } cout<<"Result =\n"; for(i=0;i<s;i++) { cout<<A[i]<<" "<<"\n";} getch();

## #include<iostream.h> #include<conio.h> void fn(int A[][30],int s); void main() { clrscr();

Page | 16

int i,j,s,A[30][30]; cout<<"Enter the size of the Matrix"; cin>>s; cout<<"Enter the elements"; for(i=0;i<s;i++) { for(j=0;j<s;j++) { cin>>A[i][j]; } } fn(A,s); getch(); } void fn(int A[ ][30],int s) { int i,j; for(i=0;i<s;i++) { for(j=0;j<s;j++) { if(i==j||i+j==s-1) { cout<<A[i][j]; } else { cout<<" "; } } cout<<"\n"; }}

Page | 17

## Find Maximum & Minimum element of an Array.

#include<iostream.h> #include<conio.h> void FIND(int A[],int s); void main() { clrscr(); int i,A[100],s; cout<<"Enter the size of an Array"; cin>>s; cout<<"Enter the Elments of an Array"; for(i=0;i<s;i++) { cin>>A[i]; } FIND(A,s); getch(); } void FIND(int A[],int s) { int i,max,min,p1,p2; max=A[0]; min=A[0]; for(i=0;i<s;i++) { if(A[i]>max) { max=A[i]; p1=i; } else {

Page | 18

## min=A[i]; p2=i; } } cout<<"Maximum element="<<max<<","<<" "<<"Position="<<p1+1<<"\n"; cout<<"Minimum element="<<min<<","<<" "<<"Position="<<p2+1; }

Wap in c++ to read two strings and appends the 1st string to the second. for example,if the 1st string is "good" and second as "morning " then result should be morning good

## #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr();

Page | 19

char str[50],srt[50]; int s; cout<<"Enter the string1\n"; gets(str); cout<<"Enter the string2\n"; gets(srt); strcat(srt,str); s=strlen(srt); puts(srt); getch(); }

Wap in c++ to read the password .If the password is correct then print o.k. else print sorry. There are 5 chance given to the user.

## #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() {

Page | 20

clrscr(); char str1[9], str2[9]; int i; cout<<"Enter the password\n"; gets(str1); cout<<"\n\n\nI N S E R T E D\n\n\n"; cout<<"Enter the password to check\n"; gets(str2); for(i=0;i<3;i++) { if(strcmp(str2,str1)) cout<<"\nSORRY ,RE-ENTER"; else cout<<"\nO.K\n"; break; getch(); } }

## #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr();

Page | 21

char a[50]; int i,x=0; cout<<"\nEnter the string:-\n"; gets(a); int s=strlen(a); for(i=0;i<s;i++) { if((a[i]!=' '&&a[i+1]==' ')||(a[i+1]=='\0')) x++; } cout<<"\nNo. of wordsa are:- "<< x; getch(); }

Page | 22

## 1234 1234 12345 12345 12345

#include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"\nThe pattern is :-\n\n"; for(int i=1;i<=6;i++) { for(int x=1;x<=i;x++) { for(int j=1;j<=i;j++) { cout<<j; } cout<<"\n"; }

Page | 23

} getch(); }

## Generate the following series. 0.1,1,2,5,8,13,21,31

#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,b,a,c; cout<<"\nEnter the term value:-\n"; cin>>n; a=0; b=1; cout<<a<<","<<b<<" ,"; for(int j=0;j<=n-2;j++) { c=a+b; cout<<c<<" , ";

Page | 24

Page | 25