Documente Academic
Documente Profesional
Documente Cultură
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
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
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--]; }}
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(); }
Page | 9
@ @ @ @ @ @ @ @ @ @
#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
#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.
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];
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();
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
Page | 18
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
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.
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(); } }
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(); }
#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(); }
Page | 24
Page | 25