Você está na página 1de 94

KALINDI COLLEGE

Practical file: Presented by Sugandh gupta Bsc.(h)comp.sci.(1 sem) 1003886


st

//A PROGRAM TO PRINT SUM AND PRODUCT OF INTEGERS DIGITS #include<iostream.h> #include<conio.h> main() { clrscr(); int n,prod=1,r,sum=0; cout<<"enter the number:"; cin>>n; while(n!=0) { r=n%10; prod=prod*r; sum=sum+r; n=n/10; } cout<<"product of digits:"<<prod<<"\n"; cout<<"sum of digits:"<<sum; getch(); }

OUTPUT: enter the number:1234 product of digits:24 sum of digits:10

//A PROGRAM TO REVERSE A NUMBER #include<iostream.h> #include<conio.h> void main() { clrscr(); int a,r=0,s=0; cout<<"enter the number:"; cin>>a; while(a!=0) { r=a%10; a=a/10; s=s*10+r; } cout<<"the reversed number is:"<<s; getch(); }

OUTPUT: enter the number:4567 the reversed number is:7654

//A PROGRAM TO FIND SUM OF SERIES 1+1/2+1/3+....+1/N #include<iostream.h> #include<conio.h> void main() { float i,n; float sum=0; cout<<"enter the terms:"; cin>>n; for(i=1;i<=n;i++) { sum=sum+1/i; } cout<<"the sum of series is:"<<sum; getch(); }

OUTPUT: enter the terms:4 the sum of series is:2.08333

//A PROGRAM TO CALCULATE SUM OF SERIES 1-2+3-4+5.... #include<iostream.h> #include<conio.h> main() { int sodd=0,n,seve=0,tsum=0; cout<<"enter the terms:"; cin>>n; for(int i=1;i<=n;i++) { if(i%2==0) seve=seve-i; else sodd=sodd+i; } tsum=seve+sodd; cout<<"the sum of series:"<<tsum; getch(); }

OUTPUT: enter the terms:6 the sum of series:-3

//A PROGRAM TO CHECK IF A STRING IS PALINDROME OR NOT. #include<iostream.h> #include<conio.h> int main() { clrscr(); char a[80],c; cout<<"enter string:"; cin.getline(a,80); //loop to find the length of string. for(int len=0;a[len]!='\0';len++); int i,j,flag=1; for(i=0,j=len-1;i<len/2;i++,j--) { if(a[i]!=a[j]) { flag=0; break; } } if(flag) //i.e.,If flag is true.

cout<<"It is a palindrome.\n"; else cout<<"It is not a palindrome.\n"; return 0; }

OUTPUT: enter string:radar it is a palindrome. enter string:computer it is not a palindrome.

//A PROGRAM TO CHECK WHETHER A GIVEN NO. IS PRIME OR NOT. #include<iostream.h> #include<conio.h> int main() { int n; cout<<"\n enter the number:"; cin>>n; int i=2; while(i<=n-1&&n<=100) { if(n%i==0) { cout<<"\n"<<n<<" is not a prime no."; break; } i++; } if(i==n) cout<<"\n"<<n<<" is a prime no."; return 0; } OUTPUT: enter the number:97 97 is a prime no.

//A PROGRAM TO FIND FACTOR OF A GIVEN NUMBER. #include<iostream.h> #include<conio.h> void fact(int n) { int i; for(i=1;i<n;i++) { if(n%i==0) cout<<"Factor for given no. is:"<<i<<"\n"; } } main() { int a; cout<<"Enter a number:"; cin>>a; fact(a); }

OUTPUT: Enter a number:6 Factor for given no. is:1 Factor for given no. is:2 Factor for given no. is:3

//A PROGRAM TO PRINT TRIANGLE OF STARS #include<iostream.h> #include<conio.h> void main() { clrscr(); int n=0; cin>>n; for(int i=1;i<=n;i++) { for(int j=(n-i);j>=1;j--) { cout<<" "; } for(int k=1;k<=(2*i-1);k++) { cout<<"*"; } cout<<"\n"; }} OUTPUT: 5 * *** ***** ******* *********

//A PROGRAM TO PERFORM CERTAIN OPERATIONS ON USER ENTERED ARRAY

#include<iostream.h> #include<conio.h> #include<string.h>

//class definition

class Test { int arr[100],size; public: void read(); void print_even(); void print_odd(); void print_Sum_Avg(); void sort(); void print_max_min(); void remove_duplicate(); void reverse_array(); void show(); }ob;

//function definitions

void Test::read() {

cout<<"Enter size of array:"; cin>>size; cout<<"Enter elements of array\n"; for(int i=0;i<size;i++) { cout<<"Element cin>>arr[i]; } } "<<i+1<<":";

void Test::print_even() { cout<<"Even elements are\n"; for(int i=0;i<size;i++) if(arr[i]%2==0) cout<<arr[i]<<"\n"; }

void Test::print_odd() { cout<<"Odd elements are\n"; for(int i=0;i<size;i++) if(arr[i]%2!=0) cout<<arr[i]<<"\n"; }

void Test::print_Sum_Avg() { int sum=0;

float avg=0.0; for(int i=0;i<size;i++) sum+=arr[i]; avg=(float)sum/size; cout<<"Sum of elements of array:"<<sum<<"\n"; cout<<"Average of elements of array:"<<avg<<"\n"; }

void Test::sort() { int flag; for(int i=0;i<size;i++) for(int j=0;j<(size-1);--i,j++) { if(arr[j]>arr[j+1]) { flag=arr[j]; arr[j]=arr[j+1]; arr[j+1]=flag; } } cout<<"Sorted array\n"; for(int k=0;k<size;k++) cout<<arr[k]<<"\n"; }

void Test::print_max_min()

{ int max=arr[0],min=arr[0]; for(int i=0;i<size;i++) { if(arr[i]>max) max=arr[i]; if(arr[i]<min)min=arr[i]; } cout<<"Maximum element:"<<max<<"\n"; cout<<"Minimum element:"<<min<<"\n"; }

void Test::remove_duplicate() { for(int i=0;i<size-1;i++) for(int j=i+1;j<size;j++) if(arr[i]==arr[j]) { size-=1; for(int k=j;k<size;k++) arr[k]=arr[k+1]; } cout<<"Duplicate element removed\n"; } void Test::reverse_array() { cout<<"Reversed array\n"; for(int i=size-1;i>=0;i--)

cout<<arr[i]<<"\n"; }

void Test::show() { for (int i=0;i<size;i++) cout<<"Element } :"<<i+1<<": "<<arr[i]<<"\n";

int main() { int choice; ob.read(); char ch; do { cout<<"\n***----------** MENU **----------***\n\n"; cout<<"1 cout<<"2 cout<<"3 cout<<"4 cout<<"5 cout<<"6 cout<<"7 cout<<"8 cout<<"9 :Re-Enter array\n"; :Print Even elements of array\n"; :Print Odd elements of array\n"; :Print Sum and Average of array elements\n"; :Print Maximum and Minimum element of array\n"; :Sort Array\n"; :Remove Duplicate elements from array\n"; :Print Reverse of Array\n";

:To quit the program\n";

cout<<"\nEnter Your Choice:";

cin>>choice; switch(choice) { case 1: { ob.read(); break; } case 2: { ob.print_even(); break; } case 3: { ob.print_odd(); break; } case 4: { ob.print_Sum_Avg(); break; } case 5: { ob.print_max_min(); break;

} case 6: { ob.sort(); break; }

case 7: { ob.remove_duplicate(); ob.show(); break; } case 8: { ob.reverse_array(); break; } case 9: { cout<<"Final array\n"; ob.show(); break; } default: { cout<<"\n You Have Entered a Wrong Choice pls Try Again!!!!!";

break; } } cout<<"\n Want to enter more(y/n)?"; cin>>ch; if(ch=='y'||ch=='Y') cout<<"Again enter choice(1-9):"; } while(ch=='y'||ch=='Y'); return 0; }

OUTPUT Enter size of array:6 Enter elements of array Element 1:1 Element 2:3 Element 3:2 Element 4:5 Element 5:4 Element 6:6

***----------** MENU **----------***

1 2 3 4

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements

5 6 7 8 9 10

:Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:2 Even elements are 2 4 6

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:3 Odd elements are 1 3 5

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:4 Sum of elements of array:21 Average of elements of array:3.5

***----------** MENU **----------***

:Re-Enter array

2 3 4 5 6 7 8 9 10

:Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:5 Maximum element:6 Minimum element:1

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:6 Sorted array 1 2 3 4 5 6

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:7 Duplicate element removed Element :1: 1 Element :2: 2

Element :3: 3 Element :4: 4 Element :5: 5 Element :6: 6

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:8 Enter starting and end point of compactoin:2 3 Element :1: 1 Element :2: 2 Element :3: 5 Element :4: 6 Element :5: 0

Element :6: 0

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Compact array :Print Reverse of Array :To quit the program

Enter Your Choice:9 Reversed array 0 0 6 5 2 1

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9

:Re-Enter array :Print Even elements of array :Print Odd elements of array :Print Sum and Average of array elements :Print Maximum and Minimum element of array :Sort Array :Remove Duplicate elements from array :Print Reverse of Array :To quit the program

Enter Your Choice:9 Final array Element :1: 1 Element :2: 2 Element :3: 5 Element :4: 6 Element :5: 0 Element :6: 0 Want to enter more(y/n)? n

//A PROGRAM TO PRINT A TABLE INDICATING THE NO. OF OCCURENCES OF EACH LETTER OF ALPHABET IN THE TEXT ENTERED AS COMMAND LINE ARGUMENTS

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h>

class Test { char a[100]; public: void readstring(); void displaylist_lowercase(); void displaylist_uppercase(); }ob;

void Test::readstring() { cout<<"Enter String :"; gets(a); cout<<"Entered String:"; puts(a); }

void Test::displaylist_lowercase() { int l=strlen(a); for(int i=97;i<=122;i++) { int flag=0,ch; for(int j=0;j<l;j++) { ch=(int)a[j]; if(i==ch)

flag++; } cout<<"\n"<<(char)i<<" flag=0; } cout<<"\n"; } "<<": "<<flag;

void Test::displaylist_uppercase() { int l=strlen(a); for(int i=65;i<=90;i++) { int flag=0,ch; for(int j=0;j<l;j++) { ch=(int)a[j]; if(i==ch) flag++; } cout<<"\n"<<(char)i<<" flag=0; } getch(); } "<<": "<<flag;

void main ()

{ clrscr(); ob.readstring(); ob.displaylist_lowercase(); ob.displaylist_uppercase(); }

OUTPUT

Enter String :my name is XYANSJHS AJMAK Entered String:my name is XYANSJHS AJMAK

a :1 b :0 c :0 d :0 e :1 f :0 g :0 h :0 i :1 j :0 k :0 l :0 m :2 n :1 o :0 p :0

q :0 r :0 s :1 t :0 u :0 v :0 w :0 x :0 y :1 z :0

A :3 B :0 C :0 D :0 E :0 F :0 G :0 H :1 I :0 J :2 K :1 L :0 M :1 N :1 O :0

P :0 Q :0 R :0 S :2 T :0 U :0 V :0 W :0 X :1 Y :1 Z :0

//A PROGRAM TO SWAP TWO NUMBERS USING POINTERS #include<iostream.h> #include<stdio.h> #include<conio.h> void swap(float*a,float*b) { float t; t=*a; *a=*b; *b=t; } void main() {

float p,q; clrscr(); cout<<"enter number p:"; cin>>p; cout<<"enter number q:"; cin>>q; clrscr(); cout<<p; cout<<q; clrscr(); swap(&p,&q); cout<<p; cout<<q; getch();} OUTPUT: Enter number p:8 Enter number q:5 58

//A PROGRAM TO GENERATE PAY SLIP OF SALARIED EMPLOYEE #include<iostream.h> #include<conio.h> class employee { private: char name1[20][20], desig[30][30]; int empno[10], i, n; float bpay[20], lic[20], pf[20], da[20], hra[20], gpay[20], npay[20], ded[20];

public: void getdata(); void display(); };

void employee::getdata() { cout<<"How many Employee details would you like to enter : "; cin>>n; for(i=0;i<n;i++) { cout<<"\n Employee No. cin>>empno[i]; cout<<"\n Employee name cin>>name1[i]; cout<<"\n Employee Designation cin>>desig[i]; cout<<"Basic Pay cin>>bpay[i]; cout<<"LIC Deduction cin>>lic[i]; cout<<"PF Deduction cin>>pf[i]; } } : "; : "; : "; : "; : "; : ";

void employee::display() { cout<<"SALARY SLIP FOR EMPLOYEES "; cout<<"\n---------------------------------------------------------------------------"; cout<<"\nEMP_NO\tEMP_NAME\tBASIC\tD.A\tH.R.A\tLIC\tPF\tGPAY\tNPAY\t"; cout<<"\n---------------------------------------------------------------------------"; for(i=0;i<n;i++) { da[i]=(bpay[i]*40)/100; hra[i]=(bpay[i]*25)/100; ded[i]=lic[i]+pf[i]; gpay[i]=bpay[i]+da[i]+hra[i]; npay[i]=gpay[i]-ded[i]; cout<<"\n"<<empno[i]<<"\t"<<name1[i]<<"\t"<<bpay[i]<<"\t"<<da[i]<<"\t"<<hr a[i]<<"\t"<<lic[i]<<"\t"<<pf[i]<<"\t"<<gpay[i]<<"\t"<<npay[i]; } cout<<"\n---------------------------------------------------------------------------\n"; }

int main() { clrscr(); employee e; e.getdata(); e.display(); return 0;

getch(); } //A PROGRAM TO PERFORM CERTAIN OPERATIONS ON USER ENTERED STRINGS #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> #include<process.h>

class strings { char c[110],c1[50],c2[50]; int l1,l2,l3; public: void getstring(); void concat1(); void concat2(); void compare(); void lengthcal(); void lower(); void upper(); void vowelcount(); void reverse(); }ob;

void strings::getstring() {

cout<<"\nString 1:"; gets(c1); cout<<"\nString 2:"; gets(c2); } void strings::concat1() { int n=0,l=strlen(c1); for(int i=0;i<l;i++) { c[i]=c1[i]; n++; } l=strlen(c2); for(int j=0;j<l;j++) c[n++]=c2[j]; c[n]='\0'; cout<<"\nConcatenated Strings without using Strcat puts(c); } void strings::concat2() { int n=0,l=strlen(c1); for(int i=0;i<l;i++) { c[i]=c1[i]; n++; :";

} c[n]='\0'; strcat(c,c2); cout<<"\nConcatenated Strings using Strcat puts(c); } :";

void strings::compare() { int cp=strcmp(c1,c2); if(cp==0) cout<<"\nStrings are same"; else cout<<"\nStrings are not same"; }

void strings::lengthcal() { char *p; int i=0,l=0,j=0; p=&c1[0]; while(*(p+i)!='\0') { i++; } cout<<"\nLength of Ist String p=&c2[0]; :"<<l;

while(*(p+j)!='\0') { j++; } cout<<"\nLength of IInd String :"<<l; }

void strings::vowelcount() { int l=strlen(c1),flag=0; for(int i=0;i<l;i++) {

if(c1[i]=='a'||c1[i]=='A'||c1[i]=='e'||c1[i]=='E'||c1[i]=='i'||

c1[i]=='I'||c1[i]=='o'||c1[i]=='O'||c1[i]=='u'||c1[i]=='U') flag++; } cout<<"\nNumber of Vowels in the string I :"<<flag; l=strlen(c2),flag=0; for(int j=0;j<l;j++) {

if(c2[j]=='a'||c2[j]=='A'||c2[j]=='e'||c2[j]=='E'||c2[j]=='i'||

c2[j]=='I'||c2[j]=='o'||c2[j]=='O'||c2[j]=='u'||c2[j]=='U') flag++; } cout<<"\nNumber of Vowels in the string II :"<<flag;

void strings::lower() { int l=strlen(c1); cout<<"\nString 1 in Lower case:"; for(int i=0;i<l;i++) { if(c1[i]>=65&&c1[i]<=90) c1[i]=c1[i]+32; cout<<c1[i]; } l=strlen(c2); cout<<"\nString 2 in Lower case:"; for(int j=0;j<l;j++) { if(c2[j]>=65&&c2[j]<=90) c2[j]=c2[j]+32; cout<<c2[j]; } } void strings::upper() { int l=strlen(c1); cout<<"\nString 1 in Upper case:"; for(int i=0;i<l;i++) {

if(c1[i]>=97&&c1[i]<=122) c1[i]=c1[i]-32; cout<<c1[i]; }

l=strlen(c2); cout<<"\nString 2 in Upper case:"; for(int j=0;j<l;j++) { if(c2[j]>=97&&c2[j]<=122) c2[j]=c2[j]-32; cout<<c2[j]; } }

void strings::reverse() { int j=0,i=0,l=strlen(c1); for(i=l-1;i>=0;i--) c[j++]=c1[i]; c[j]='\0'; cout<<"\nReversed String :"; puts(c); j=0; l=strlen(c2); for(j=l-1;j>=0;j--) c[j++]=c2[i];

c[j]='\0'; cout<<"\nReversed String :"; puts(c); }

void main() { int choice; char ch; ob.getstring(); do { cout<<"\n***----------** MENU **----------***\n\n"; cout<<"1 cout<<"2 cout<<"3 cout<<"4 cout<<"5 cout<<"6 cout<<"7 cout<<"8 cout<<"9 cout<<"10 :Read Strings\n"; :Concatnate without STRCAT\n"; :Concatenate with STRACAT\n"; :Length of Strings\n"; :Convert 2 Lower\n"; :Convert 2 Upper\n"; :Compare The Strings\n"; :Count The Vowels\n"; :Reverse\n"; :To quit the program\n";

cout<<"\nEnter Your Choice:"; cin>>choice; switch(choice) {

case 1: { ob.getstring(); break; } case 2: { ob.concat1(); break; } case 3: { ob.concat2(); break; } case 4: { ob.lengthcal(); break; } case 5: { ob.lower(); break; } case 6: {

ob.upper(); break; } case 7: { ob.compare(); break; } case 8: { ob.vowelcount(); break; } case 9: { ob.reverse(); break; } case 10:

{ cout<<"Enter 0 to quit the program and any key to continue:"; cin>>ch; break; } default: {

cout<<"\n You Have Entered a Wrong Choice pls Try Again!!!!!"; } } } while(ch!='0'); cout<<"\nYou Have Quit The Program!!\n"; getch(); }

OUTPUT

String 1:god

String 2:GOD

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:2

Concatenated Strings without using Strcat :godGOD

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:3

Concatenated Strings using Strcat

:godGOD

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:4

Length of Ist String :3 Length of IInd String :3 ***----------** MENU **----------***

1 2 3 4 5 6

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper

7 8 9 10

:Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:5

String 1 in Lower case:god String 2 in Lower case:god ***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:6

String 1 in Upper case:GOD String 2 in Upper case:GOD

***----------** MENU **----------***

1 2 3 4 5 6 7 8 9 10

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels :Reverse :To quit the program

Enter Your Choice:7

Strings are same ***----------** MENU **----------***

1 2 3 4 5 6 7 8

:Read Strings :Concatnate without STRCAT :Concatenate with STRACAT :Length of Strings :Convert 2 Lower :Convert 2 Upper :Compare The Strings :Count The Vowels

9 10

:Reverse :To quit the program

Enter Your Choice:10

You Have Quit the Program!!

//A PROGRAM THAT SORTS A LIST OF N NO.S USING BUBBLE SORT #include<iostream.h> void bubble_sort(int[],int); void main()

{int ar[50],item,n,index,i,tmp; cout<<"how many elements do you want to create array with?"; cin>>n; cout<<"\n enter array element\n"; for(i=0;i<n;i++) cin>>ar[i]; bubble_sort(ar,n); cout<<"\n the sorted array is shown below \n"; for(i=0;i<n;i++) cout<<ar[i]<<" "; cout<<endl; }

void bubble_sort(int ar[],int size) {int tmp,ctr=0; { for(int i=0;i<size;i++) { for(int j=0;j<(size-1)-i;j++) { if(ar[j]>ar[j+1]) { tmp =ar[j]; ar[j]=ar[j+1]; ar[j+1]=tmp; } } cout<<"array after iteration-"<<++ctr<<"-is:"; for(int k=0;k<size;k++) cout<<ar[k]<<" "; cout<<endl; } } } OUTPUT How many elements do u want to create array with? 5 Enter array elements 9 7 4 6 1 Array after iteration-1-is: 7 4 6 1 9 Array after iteration-2-is: 4 6 1 7 9

Array after iteration-3-is: 4 1 6 7 9 Array after iteration-4-is: 1 4 6 7 9 Array after iteration-5-is: 1 4 6 7 9 The Sorted array is as shown below. 14679

//A PROGRAM TO SEARCH FOR A GIVEN ELEMENT IN A SET OF N NO.'S USING BINARY SEARCH #include<iostream.h> #include<conio.h> int bsearch(int[],int,int); void main() { clrscr(); int ar[50],item,n,index; cout<<"enter desired array size"; cin>>n; cout<<"\nenter array elements\n"; for(int i=0;i<n;i++) cin>>ar[i]; cout<<"\nenter element to be searched for"; cin>>item; index=bsearch(ar,n,item); if(index==-1) cout<<"\nsorry element not found\n"; else cout<<"nelement found at index:"<<index<<",position:"<<index+1<<endl;

} int bsearch(int ar[],int size,int item) { int beg,last,mid; beg=0;last=size-1; while(beg<=last) { mid=(beg+last)/2; if(item==ar[mid]) return mid; else if(item>ar[mid]) beg=mid+1; else last=mid-1; } return -1; } Enter desired array size 4 Enter array elements 3 4 1 8 Enter element to be searched for 4 Element found at index: 1, position: 2

//A PROGRAM TO SORT A LIST OF N NO.'S USING SELECTION SORT #include<iostream.h>

#include<conio.h> void selsort(int[],int); void main() { int ar[50],item,n,index; cout<<"enter desired array size"; cin>>n; cout<<"nenter array elements..."; for(int i=0;i<n;i++) cin>>ar[i]; selsort(ar,n); cout<<"\nthe sorted array is as shown below...\n"; for(i=0;i<n;i++) cout<<ar[i]<<" "; cout<<endl; } void selsort(int ar[],int size) { int small,pos,tmp; for(int i=0;i<size;i++) { small=ar[i]; for(int j=i+1;j<size;j++) { if(ar[j]<small) {

small=ar[j];pos=j;} } tmp=ar[i]; ar[i]=ar[pos]; ar[pos]=tmp; cout<<"\narray after pass-"<<i+1<<"-is:"; for(j=0;j<size;j++) cout<<ar[j]<<""; } } Output: Enter desired array size 3 Enter array array elements4 6 1 Array after pass-1-is:1 6 4 Array after pass-2-is:1 4 6 Array after pass-3-is:1 4 6 The sorted array is as shown below.. 1 4 6

//A PROGRAM TO SORT A LIST OF N NO.'S USING INSERTION SORT #include<iostream.h> #include<limits.h> #include<conio.h> void inssort(int[],int);

void main() {clrscr(); int ar[50],n; cout<<"enter desired array size"; cin>>n; cout<<"\nenter array elements..."; for(int i=1;i<=n;i++) cin>>ar[i]; inssort(ar,n); cout<<"\nthe sorted array is as shown below...\n"; for(i=1;i<=n;i++) cout<<ar[i]<<" "; cout<<endl; } void inssort(int ar[],int size) { int tmp,j; ar[0]=INT_MIN; for(int i=1;i<=size;i++) { tmp=ar[i]; j=i-1; while(tmp<ar[j]) { ar[j+1]=ar[j]; j--;

} ar[j+1]=tmp; cout<<"array after pass-"<<i<<"-is: "; for(int k=1;k<=size;k++) cout<<ar[k]<<""; cout<<endl; } } OUTPUT Enter desired array size 3 Enter array array elements4 6 1 Array after pass-1-is:4 6 1 Array after pass-2-is:4 6 1 Array after pass-3-is:1 4 6 The sorted array is as shown below.. 1 4 6

//A PROGRAM TO MERGE THE 2 ARRAYS TO GET AN ORDERED PAIR #include<iostream.h> void merge(int[],int,int[],int,int[]); void main() { int a[50],b[50],c[50],mn=0,m,n; cout<<"enter desired array size"; cin>>m;

cout<<"\nenter first array elements[ascending]...\n"; for(int i=0;i<m;i++) cin>>a[i]; cout<<"\nenter desired array size"; cin>>n; mn=m+n; cout<<"\nenter second array elements[descending]...\n"; for(i=0;i<n;i++) cin>>b[i]; merge(a,m,b,n,c); cout<<"\nmerged array is as shown below...\n"; for(i=0;i<mn;i++) cout<<c[i]<<""; cout<<endl; } void merge(int a[],int m,int b[],int n,int c[]) { int a1,b1,c1; for(a1=0,b1=n-1,c1=0;a1<m&&b1>=0;) { if (a[a1]<=b[b1]) c[c1++]=a[a1++]; else c[c1++]=b[b1--]; } if(a1<m) {

while (a1<m) c[c1++]=a[a1++]; } else { while(b1>=0) c[c1++]=b[b1--]; } }

OUTPUT Enter the desired array size 4 Enter first array elements[ascending] 1234 Enter the desired array size 4 Enter second array elements[descending] 9876 Merged array is as shown below 12346789

// search for a given element x in set of N nos. using linear search #include<iostream.h> #include<conio.h> void main() { clrscr();

int a[20]; int i,n,num; cout<<"********************************** \n"; cout<<"enter the number of elemnts : \n "; cout<<"********************************** \n" ; cin>>n; cout<<"enter the array elements : \n "; for(i=0;i<n;i++) cin>>a[i]; cout<<"enter the element you want to search"; cin>>num; for(i=0;i<n;i++) if(a[i]==num) cout<<"value found at"<<i+1; } OUTPUT Enter the number of elements: 7 Enter the array elements: 1 5 2 8 6 10 7 Enter element you want to search: 6 Value found at 5

//.This program prints the fibonnaci series till n th term by the recursion method.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h> #include<conio.h> class Fib { int c; public: void fcal(int a,int b,int n) { if(n>1) { c=a+b; a=b; b=c; cout<<c<<" "; fcal(a,b,--n); } } }; void main() { clrscr(); int n,c,a,b; Fib v; cout<<"\nENTER THE VALUE OF N:"; cin>>n;

cout<<"\nFIBONNACI SERIES WITH RECURSION:"; cout<<"0 1 "; v.fcal(0,1,n-1); cout<<"\n\n\nFIBONNACI SERIES WITHOUT RECURSION:"; a=0; b=1; cout<<a<<" "<<b; for(int i=1;i<n-1;i++) { c=a+b; a=b; b=c; cout<<" "<<c; } getch(); } OUTPUT Enter the value of n:7 Fibonacci series with recursion:0 1 1 2 3 5 8 Fibonacci series without recursion:0 1 1 2 3 5 8

//.This program prints the FACTORIAL of a number.using RECURSION & ITERATION.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h>

#include<conio.h>

class Fact { int b; public: void fcal(int a,int c=1) { if(a>1) { b=a-1; c=(a*b)*c; b--; fcal(b,c); } else cout<<"\nFACTORIAL IS:"<<c; } }; void main() { clrscr(); int c,a; Fact v; cout<<"\nENTER THE NUMBER:"; cin>>a;

cout<<"\nFACTORIAL WITH RECURSION:"; v.fcal(a); cout<<"\n\n\nFACTORIAL WITHOUT RECURSION:"; for(int i=a-1;i>=1;i--) { c=a*i; a=c; } cout<<"\nFACTORIAL IS:"<<c; getch(); }

OUTPUT Enter the number:5 Factorial with recursion: Factorial is:120 Factorial without recursion: Factorial is:120

//Q23. WAP to calculate GCD of 2 numbers a)using iteration b)using recursion.

//----------------------------------------------------------------------------------------------------------

#include<iostream.h> #include<conio.h> int GCD(int,int);

void main() { clrscr(); int a,b,rem; cout<<"\nEnter two numbers: "; cin>>a>>b; cout<<"\nGCD of two numbers using recursion is: "<<GCD(a,b); cout<<"\nGCD of two numbers without using recursion is: "; while(b%a!=0) { rem=b%a; b=a; a=rem; } cout<<a; getch(); } int GCD(int a,int b) { int rem; if(b%a==0) return a; else { rem=b%a; b=a;

a=rem; GCD(a,b); } } OUTPUT Enter two numbers:6 8 Gcd of two no.s using recursion:2 Gcd of two no.s without using recursion:2

//A PROGRAM TO PERFORM CERTAIN MATRIX OPERATIONS #include<iostream.h> #include<conio.h> void main() { clrscr(); int i,j; int a[2][2],b[2][2],c1[2][2]; int c,ip; cout<<"Matrix Menu"<<"\n"; cout<<"1.Addition"<<"\n"; cout<<"2.Subtraction"<<"\n"; cout<<"3.Transpose"<<"\n"; cout<<"4.Multiplication"<<"\n"; cout<<"Enter Your Choice:"<<"\n"; cin>>c; cout<<"Enter The First Matrix:"<<"\n";

for( i=0;i<2;i++) { for( j=0;j<2;j++) { cin>>a[i][j]; }

} cout<<"Enter The Second Matrix:"<<"\n"; for( i=0;i<2;i++) { for( j=0;j<2;j++) { cin>>b[i][j]; }

} switch(c) { case 1:cout<<"The Addition Of Matrix Is:"<<"\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<a[i][j]+b[i][j]<<" "; }

cout<<"\n"; } break; case 2:cout<<"The Subtraction Of Matrix Is:"<<"\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { cout<<a[i][j]-b[i][j]<<" "; } cout<<"\n"; } break; case 3:cout<<"The Transpose Of Matrix A Is:"; for(i=0;i<2;i++) { cout<<"\n"; for(j=0;j<2;j++) { cout<<a[j][i]<<" "; } } cout<<"\n"; cout<<"The Transpose Of Matrix B Is:"; for(i=0;i<2;i++) {

cout<<"\n"; for(j=0;j<2;j++) { cout<<b[j][i]<<" "; } } break; case 4:cout<<"The Multiplication of two Matrix Is:"; for(i=0;i<2;i++) for(j=0;j<2;j++) { c1[i][j]=0; for(ip=0;ip<2;ip++) { c1[i][j]=c1[i][j]+(a[i][ip]*b[ip][j]); cout<<c1[i][j]<<" "; } cout<<"\n"; } break;

default :cout<<"Wrong Choice";

} getch(); }

OUTPUT Enter Dimension of Ist matrix Dimension 1:3 Dimension 2:2

Enter elements of Ist Matrix

Element 1:1 Element 2:2 Element 3:3 Element 4:4 Element 5:5 Element 6:6 Enter Dimension of IInd Matrix Dimension 1:2 Dimension 2:3

Enter elements of IInd Matrix Element 1:6 Element 2:5 Element 3:4 Element 4:3 Element 5:2 Element 6:1

Ist Matrix

1 3 5

2 4 6

IInd Matrix 6 3 5 2 4 1

***----------** MENU **----------***

1 2 3 4 5 6

:Re-Enter Matrix :Add the Matrix :Subtract the Matrix :Multiply the Matrix :Transpose the Matrix :To quit the program

Enter Your Choice:2 Matrix are not compatible for Addition

***----------** MENU **----------***

1 2 3 4 5 6

:Re-Enter Matrix :Add the Matrix :Subtract the Matrix :Multiply the Matrix :Transpose the Matrix :To quit the program

Enter Your Choice:3 Matrix are not compatible for Subtraction

***----------** MENU **----------***

1 2 3 4 5 6

:Re-Enter Matrix :Add the Matrix :Subtract the Matrix :Multiply the Matrix :Transpose the Matrix :To quit the program

Enter Your Choice:4 Product Matrix 4 2

12 20

4 6

***----------** MENU **----------***

1 2 3 4 5 6

:Re-Enter Matrix :Add the Matrix :Subtract the Matrix :Multiply the Matrix :Transpose the Matrix :To quit the program

Enter Your Choice:5 Transpose Of Ist Matrix 1 2 3 4 5 6

Transpose Of IInd Matrix 6 5 4 3 2 1

***----------** MENU **----------***

1 2 3 4 5 6

:Re-Enter Matrix :Add the Matrix :Subtract the Matrix :Multiply the Matrix :Transpose the Matrix :To quit the program

Enter Your Choice:6 Enter 0 to quit the program and any key to continue:0

You Have Quit the Program!! ----------------------------------------------------------------------------------------------------------*/

//CREATE CLASS TRIANGLE USING OVERLOAD FUNCTIONS FOR CALCULATING //AREA,OVERLOAD ASSIGNMENT OPERATOR AND EQUALITY OPERATOR

#include<iostream.h> #include<conio.h>

class triangle { int base,height; public: triangle() {}

triangle(int b,int h) { base=b; height=h; } void area(); void area(float,float); void show(); triangle operator=(triangle &r) { base=r.base; height=r.height; return *this; } triangle operator-=(triangle r) { base=base-r.base; height=height-r.height; return *this; }

};

void triangle::area() { int temp=(base*height)/2;

cout<<temp; }

void triangle::area(float l,float m) { cout<<l*m; } void triangle::show() { cout<<"\nBASE IS:"<<base<<"\nHEIGHT IS:"<<height; } void main() { clrscr(); triangle t1(10,20),t2(5,6),t3; float l3,m3; cout<<"\nENTER THE BASE & HEIGHT OF 3rd TRIANGLE:"; cin>>l3>>m3; cout<<"AREA OF TRIANGLE 3 IS:"; t3.area(l3,m3); cout<<"\nAREA OF TRIANGLE ONE IS:"; t1.area(); cout<<"\nAFTER OVERLOADING OPERATOR -=::TRIANGLE ONE changed:"; t1-=t2; t1.show(); cout<<"\nAREA OF TRIANGLE ONE IS:";

t1.area(); cout<<"\nAFTER OVERLOADING OPERATOR =::TRIANGLE ONE changed:"; t1=t2; t1.show(); cout<<"\nAREA OF TRIANGLE ONE IS:"; t1.area(); getch(); }

OUTPUT ENTER THE BASE & HEIGHT OF 3rd TRIANGLE:10.5 21.9 AREA OF TRIANGLE 3 IS:229.95 AREA OF TRIANGLE ONE IS:100 AFTER OVERLOADING OPERATOR -=::TRIANGLE ONE changed: BASE IS:5 HEIGHT IS:14 AREA OF TRIANGLE ONE IS:35 AFTER OVERLOADING OPERATOR =::TRIANGLE ONE changed: BASE IS:5 HEIGHT IS:6 AREA OF TRIANGLE ONE IS:15

//.CREATING A CLASS BOX CONTAINING LENGTH,BREADTH,HEIGHT BY INCLUDING //SOME OPERATIONS ON IT

#include<iostream.h> #include<conio.h>

class box { public: int len,breadth,height; box(int l,int b,int h) { len=l; breadth=b; height=h; } void surface(); void volume(); void check(); box operator++() { len++; breadth++; height++; return *this; } box operator++(int x) { ++len;

++breadth; ++height; return *this; } box operator--(int x) { --len; --breadth; --height; return *this; } box operator--() { len--; breadth--; height--; return *this; } box operator=(box b4) { len=b4.len; breadth=b4.breadth; height=b4.height; return *this; } box operator==(box b2)

{ if((len==b2.len) && (breadth==b2.breadth) && (height==b2.height)) cout<<"TWO BOXES ARE EQUAL\n"; else cout<<"\n\nTWO BOXES ARE NOT EQUAL"; } };

void box::surface() { int sur=0; if(len==breadth && len==height) sur=(6*len*len); else sur=((4*len*breadth)+(2*breadth*height)); cout<<" SURFACE AREA:"<<sur; }

void box::volume() { cout<<" VOLUME IS:"<<(len*breadth*height); }

void box::check() { if(len==breadth && len==height)

cout<<"\nIT IS A CUBE"; else cout<<"\nIT IS A CUBOID"; }

void main() { clrscr(); int a,d,c; cout<<"Enter Dimension of Box1:"; cin>>a>>c>>d; box b(a,c,d); cout<<"Enter Dimension of Box2:"; cin>>a>>c>>d; box b1(a,c,d); cout<<"\nCHECKING EQUALITY:"; b==b1; cout<<"\nBOX I's "; b.surface(); cout<<"\nBOX II's "; b1.surface(); cout<<"\nBOX I's ";

b.volume(); cout<<"\nBOX II's "; b1.volume(); cout<<"\nOVERLOADED ++(prefix) OPERATOR:"; ++b; b.volume(); cout<<"\nOVERLOADED ++(postfix) OPERATOR:"; b++; b.volume(); cout<<"\nOVERLOADED --(prefix) OPERATOR:"; --b; b.volume(); cout<<"\nOVERLOADED --(postfix) OPERATOR:"; b--; b.volume(); cout<<"\nGIVEN BOX IS A CUBE OR CUBOID(ON FIRST BOX):"; b.check(); cout<<"\nGIVEN BOX IS A CUBE OR CUBOID(ON SECOND BOX):"; b1.check(); cout<<"\nOVERLOADING ASSIGNMENT OPERATOR:"; b=b1; b.volume(); getch(); } OUTPUT: OUTPUT

Enter Dimension of Box1:1 2 3 Enter Dimension of Box2:2 3 4

CHECKING EQUALITY:

TWO BOXES ARE NOT EQUAL BOX I's SURFACE AREA:20 BOX II's SURFACE AREA:48 BOX I's VOLUME IS:6 BOX II's VOLUME IS:24 OVERLOADED ++(prefix) OPERATOR: VOLUME IS:24 OVERLOADED ++(postfix) OPERATOR: VOLUME IS:60 OVERLOADED --(prefix) OPERATOR: VOLUME IS:24 OVERLOADED --(postfix) OPERATOR: VOLUME IS:6 GIVEN BOX IS A CUBE OR CUBOID(ON FIRST BOX): IT IS A CUBOID GIVEN BOX IS A CUBE OR CUBOID(ON SECOND BOX): IT IS A CUBOID OVERLOADING ASSIGNMENT OPERATOR: VOLUME IS:24

//Q-28 & 29. CREATE STRUCTURE STUDENT CONTAING ROLL NO.,NAME,CLASS,YEAR,TOTAL MARKS

#include<iostream.h> #include<iomanip.h> #include<conio.h> struct student { int rno,year,tot; char name[10],depart[10]; }stud[3];

int main() { clrscr(); int i; for(i=0;i<3;i++) { cout<<"\n Enter the details of student "; cout<<"\n NAME :: "; cin>>stud[i].name; cout<<"\n DEPARTMENT :: "; cin>>stud[i].depart; cout<<"\n ROLL NO. :: "; cin>>stud[i].rno; cout<<"\n YEAR :: "; cin>>stud[i].year; cout<<"\n TOTAL MARKS :: ";

cin>>stud[i].tot; } cout<<"\n DETAILS "; cout<<"\n NAME \t DEPARTMENT \t ROLL NO \t YEAR \t TOTAL MARKS "; for(i=0;i<3;i++) {

cout<<"\n"<<setw(3)<<stud[i].name<<setw(12)<<stud[i].depart<<setw(12)<<stud[i].rno<<setw(1 8)<<stud[i].year<<setw(13)<<stud[i].tot<<"\t";

} getch(); return 0; } O UTPUT ENTER THE DETAILS OF THE STUDENT: Name::sugandh Department::comp.sci. Roll no.::1803 Year::2011 Total marks::300 Enter the details of the student: Name::darshika Department::maths (h) Roll no.::1875 Year::2011 Total marks::400

Details Name Sugandh Darshika . Department comp.sci. maths(h) Roll no. 1803 1875 Year 2011 2011 Total marks 300 400

//COPY THE CONTENTS OF ONE TEXT FILE TO ANOTHER FILE AFTER REMOVING ALL WHITESPACES

#include<iostream.h> #include<fstream.h>

int main() { clrscr(); char data[80],ch,data2[80]; fstream f1; f1.open("file1.txt",ios::out); cout<<"Enter data to be written in file: "; cin.getline(data,80); f1<<data; f1.close(); fstream f2; f2.open("file2.txt",ios::out); f1.open("file1.txt",ios::in); while(f1.get(ch)) {

if(ch!=' ') f2<<ch; } f1.close(); f2.close(); f2.open("file2.txt",ios::in); f2>>data2; cout<<"Data in second file: "<<data2; getch(); return 0; }

OUTPUT Enter data to be writeen in file:sugandh Data in second file:sugandh

//Write a function thet reverses the elements of an array in place. The function must acccept //only one pointer value and return void #include<iostream> using namespace std;

void array(int *b,int n) { int t; for(int i=0,j=n-1;i<(n/2);i+=1,j--) { t=*(b+i);

*(b+i)=*(b+j); *(b+j)=t; } }

int main() { int a[10],n; cout<<"\nEnter number of elements required in the array : "; cin>>n; cout<<"\nEnter the array : "; for(int i=0;i<n;i++) cin>>a[i]; cout<<"\nThe initial array is : "; for(int i=0;i<n;i++) cout<<a[i]<<" "; array(a,n); cout<<"\n\nThe reversed array is : "; for(int i=0;i<n;i++) cout<<a[i]<<" "; getchar(); getchar(); return 0; }

OUTPUT Enter number of elements required in the array : 5

Enter the array : 12 0 2 4 5

The initial array is : 12 0 2 4 5

The reversed array is : 5 4 2 0 12

//WAP THAT WILL READ 5 INTEGERS FROM USER AND STORE THEM IN THE ARRAY . THE PROGRAM WILL PRINT THE ARRAY IN ASCENDING AND DESCENDING ORDER //THE PROGRAM MUST NOT CHANGE THE ORIGINAL ARRAY .(USE 2 POINTERS ARRAYS). using namespace std; #include<iostream> int main() { int *p,*p1,a[10],n,i,t,j,b[10]; cout<<"\n Enter the value of n :: "; cin>>n; cout<<"\n Enter the elements of array :: "; for(i=0;i<n;i++) cin>>a[i];

for(i=0;i<n;i++) { b[i]=a[i]; } p=b; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(*(p+j)>*(p+j+1)) { t=*(p+j); *(p+j)=*(p+j+1); *(p+j+1)=t; } } } cout<<"\n Array in ascending array is :: "; for(i=0;i<n;i++) cout<<*(p+i)<<" "; p1=b; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(*(p1+j)<*(p1+j+1))

{ t=*(p1+j); *(p1+j)=*(p1+j+1); *(p1+j+1)=t; } } } cout<<"\n Array in descending array is :: "; for(i=0;i<n;i++) cout<<*(p1+i)<<" "; cout<<"\n Original array is:"; for(i=0;i<n;i++) cout<<a[i]<<" "; getchar(); getchar(); return 0; }

OUTPUT -

Enter the value of n :: 5

Enter the elements of array :: 1 2 6 9

Array in ascending array is :: 1 2 6 8 9 Array in descending array is :: 9 8 6 2 1 Original array is:1 2 6 9 8

/* Write a function that allocates memory for a single data type passed as parameter. The function uses the new operator and returns a pointer to the allocated memory. the function must each catch and handle any exception during allocation. */ using namespace std;

#include<iostream.h> #include<new.h>

int allocate(int *a) { try { a=new int; cout<<"Memory allocation successful!!"; } catch(exception &err) { cout<<"ERROR 100: Program out of memory!";

} return(*a); }

int main() { int *a; *a=allocate(a); delete a; getchar(); return 0; } OUTPUT Memory allocation successful!!

//THIS PROGRAM DEMONSTRATES HANDLING OF "DIVIDE BY ZERO" AND "DIVIDE BY NEGATIVE NUMBER ERRORS" using namespace std ; #include<iostream> #include<cstdlib>

double divide(double dividend ,double divisor); int main() { cout<<"\n\n\tPlease enter the dividend: "; double dividend; cin>>dividend;

cout<<"\n\n\tPlease enter the divisor : "; double divisor; cin>>divisor; try { double quotient = divide (dividend ,divisor ); cout<<"\n\n\tqoutient is : "<<quotient<<endl;

catch(float& zeroerror) { cout<<"\nERROR100:DIVISOR 0"; getchar();

getchar();

getchar();

getchar();

exit(100);

} catch(double& negerror) { cout<<"\n\tERROR 101:NEGATIVE DIVISOR "; exit(101); getchar(); }

getchar(); getchar(); getchar();

return 0; } double divide( double dvnd,double dvsr ) { float zeroerror =0; double negerror=-1; if(dvsr==0) throw zeroerror; if(dvsr<0) throw negerror;

return dvnd/dvsr; } OUTPUT Please enter the dividend: 5

Please enter the divisor : 0 ERROR100:DIVISOR 0 AT THE SECOND RUN TIME

Please enter the dividend: 5

Please enter the divisor : -1 ERROR 101: NEGATIVE DIVISOR

Você também pode gostar