Vous êtes sur la page 1sur 26

Computer Science Practical File

Submitted by: G Satya, XII-B

Contents
Program to:
1. Print sum o ! prime no.s 2. Sum o series 1"#$%$"#&%&"#'%'"(((upto ! terms $. Print ! terms o Fibonacci series ). Count no. o *o+els, consonants and digits in a string &. C,ec- +,et,er a string is a palindrome .. Sort an array +it, selection sort '. Sort an array +it, bubble sort /. Sort an array +it, insertion sort 0. Searc, an array using 1inear searc, 12. Searc, an array using Binary searc, 11. 3elete an item at -t, 1ocation o an array 14. Insert an item at -t, 1ocation o an array 1$.Concat t+o arrays 1).5dd t+o 6atrices o $#) 1&.Product o t+o 6atrices 74#$ and $#48

1. //program to print the sum of n prime nos. #include<iostream.h> #include<conio.h> void main() { clrscr(); int *p,chk !,n,s !,div,i; //*p for storing n prime nos. //chk is to check if no. is prime cout<<"#n please enter the value of n$"; cin>>n; do { if(n< !) { cout<<"#n the value of n should %e greater than &ero#n"; cout<<"#n enter the value of n again$"; cin>>n; ' '(hile(n< !); p ne( int)n*; cout<<"#n enter the "<<n<<" prime nos.$#n"; for(i !;i<n;i++) { cout<<" "; cin>>p)i*; do { if(p)i*<,)//checking if no. is < , as , is lo(est prime no. { cout<<"#n the no. should %e greater than -"; cout<<"#n enter again$#n"; cout<<" "; cin>>p)i*; ' '(hile(p)i*<,); do { chk !; for(div ,;div<p)i*;div++) if(p)i*.div !)//checking if no. is prime { chk++; %reak; ' if(chk !)s+ p)i*;//adding the nos. if the/ are prime else { cout<<"#n the no. entered is not prime"; cout<<"#n enter the no. again$#n"; cout<<" "; cin>>p)i*; ' '(hile(chk -);

' cout<<"#n press an/ ke/"; getch(); clrscr(); cout<<"#n the nos. to %e added are$#n"; for(i !;i<n;i++) cout<<" "<<p)i*<<endl; //displa/ing nos.entered that are prime cout<<"#n the sum of the n prime nos. are$"<<s; cout<<"#n press an/ ke/"; getch(); ' 0utput$

,. //a program to calc the sum of series 1/-2+1(cu%e)/32+....to n terms #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); long fact -; int n,1; float sum !; cout<<"please enter the value of 1 in the seires"; cin>>1; cout<<"please enter the value of n in the seires"; cin>>n; for(int i -;i< n;i+ ,) { for(int 4 -;4< i;4+ ,) fact* 4; sum+ (po((1,i))/fact; ' cout<<"the sum of series is "<<sum; getch(); '

056756$

3. //print n terms of fi%onacci series #include<iostream.h> #include<conio.h> void main() { clrscr(); int n,*s; cout<<"7lease enter the no. of terms in fi%onacci series $"; cin>>n; s)!* !; s)-* -; cout<<"6he series is $ "; cout<<s)!*<<" "<<s)-*<<" "; for(int i ,;i<n;i++) { s)i* s)i8-*+s)i8,*; cout<<s)i*<<" "; ' getch(); ' 0utput$

9.//60 :05;6 <0=>?@, :0;@0;>;6@ A;B ;5CD>E@........ #include<iostream.h> #include<conio.h> #include<stdio.h> #include<ct/pe.h> void main() { clrscr(); char a),-*; int vcnt !,ccnt !,ncnt !; cout<<"#n>nter a string of characters...."; gets(a); for(int i !;a)i*2 F#!F;i++) { if(isalpha(a)i*)) if(a)i* FaFGGa)i* FeFGGa)i* FiFGGa)i* FoFGGa)i* FuFGGa)i* FAFGGa)i* a)i* F5F) vcnt++; else ccnt++; else if(isdigit(a)i*)) ncnt++; ' cout<<endl<<"the vo(els"<<vcnt; cout<<endl<<"the consonants"<<ccnt; cout<<endl<<"the num%ers"<<ncnt; getch(); ' 0utput$

F>FGGa)i*

FHFGGa)i*

F0FGG

I. //to find if a given string is paindrome or not..... #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); char a)I-*; int check !; cout<<"#n>nter some string......"; gets(a); int l strlen(a); for(int i !;i< l;i++) if(a)i* a)l8(i+-)*) check++; if(check l) cout<<"#n JouFve entered a palindrome2"; else cout<<"#n ;ot a palindrome2"; getch(); ' 0utput$

K. //selection sort #include<iostream.h> #include<conio.h> float @>?L@0E6(float *A,int ;) { for(int i !;i<;;i++) for(int 4 i+-;4<;;4++) if(A)i*> A)4*) { float temp A)i*; A)i* A)4*; A)4* temp; ' return(*A); ' void main() { clrscr(); float *A; int ;; cout<<"7lease enter the no. of elements in the arra/"; cin>>;; cout<<"#n7lease enter the elements of the arra/$#n"; for(int i !;i<;;i++) cin>>A)i*; @>?L@0E6(A,;); cout<<"#n6he elements of arra/ after sorting $#n"; for(i !;i<;;i++) cout<<A)i*<<endl; getch(); ' 0utput$

M. //%u%%le sort #include<iostream.h> #include<conio.h> float D5DD?>L@0E6(float *A,int ;) { for(int i !;i<;8-;i++) for(int 4 !;4<;8i8-;4++) if(A)4*> A)4+-*) { float temp A)4*; A)4* A)4+-*; A)4+-* temp; ' return(*A); ' void main() { clrscr(); float *A; int ;; cout<<"7lease enter the no. of elements in the arra/"; cin>>;; cout<<"#n7lease enter the elements of the arra/$#n"; for(int i !;i<;;i++) cin>>A)i*; D5DD?>L@0E6(A,;); cout<<"#n6he elements of arra/ after sorting $#n"; for(i !;i<;;i++) cout<<A)i*<<endl; getch(); ' 0utput$

N. //Insertion Sort #include<limits.h> #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[100 !i!n!"!"tr!tem"; cout<<#$n%%%%%%%%%%%% I&S'()I*& S*() %%%%%%%%%%%% $n$n#; cout<<#'nter &o. o+ 'lements , #; cin>>n; cout<<#$n'nter 'lements , $n#; +or(i-1;i<-n;i..) cin>>a[i ; a[0 -I&)/0I&; //to carr1 the lo2est value out o+ all in the arra1 +or("-1;"<-n;"..) { tem"-a[" ; "tr-"%1; 2hile(tem"<a["tr ) { a["tr.1 -a["tr ; // 0ove 'lement 3or2ard till it is "tr%%; // in "ro"er acendin4 order 2ith 5 // res"ect to the sorted elements a["tr.1 -tem"; // Insert 'lement in 6ro"er 6lace cout<<#$n7rra1 a+ter #<<"<<# insertion are,, #; +or(int 8-1;8<-n;8..) cout<<a[8 <<# #; 5 cout<<#$n7+ter Sortin4 , $n#; +or(i-1;i<-n;i..)//startin4 +rom 1 9ecause a[0 -0 cout<<a[i <<endl; 4etch(); 5 0utput$

O. /* program to accept an arra/ containing ; float t/pe elements and search an element H6>C in the arra/ (ith the help of linear search %/ using the function ?H;>AEL@>AE:P() */ #include<iostream.h> #include<conio.h> void ?H;>AEL@>AE:P(float *A,int ;,float H6>C) { int *pos,4 !; pos)!* 8OO;//8ve value for for(int i !;i<;;i++) if(A)i* H6>C) { //a pointer for more than - element found pos)4* i+-; 4++; ' if(pos)!*<!) cout<<"#n6he element (asnFt found."; else for(i !;i<4;i++) cout<<endl<<H6>C<<"(as found at "<<pos)i*<<"th position in the arra/."; ' void main() { clrscr(); float *A,H6>C; int ;; cout<<"#n>nter the si&e of arra/$ "; cin>>;; cout<<"#n>nter the elements of arra/#n"; for(int i !;i<;;i++) cin>>A)i*; cout<<"#n>nter the H6>C /ou (ant to search for$ "; cin>>H6>C; ?H;>AEL@>AE:P(A,;,H6>C); getch(); ' 0uput$

-!. //Dinar/ search #include<iostream.h> #include<conio.h> int DH;AEJL@>AE:P(int A)*, int ;, int <A?) { int Cid,?%ound !,5%ound ;8-; (hile(?%ound< 5%ound) { Cid (?%ound+5%ound)/,; if(<A?>A)Cid*) ?%ound Cid+-; else if(<A?<A)Cid*) 5%ound Cid8-; else if(<A? A)Cid*) return(Cid); ' return8OO; ' void main() { clrscr(); int A)-!!*,;,val,found; //considering -!! as ma1. no. of elements to %e entered cout<<">nter num%er of elements /ou (ant in the arra/.(ma1$-!!)"; cin>>;; cout<<">nter elements of the arra/.#n"; for(int i !;i<;;i++) cin>>A)i*; for(i !;i<;;i++) //Qor sorting the arra/ using selction sort for(int 4 i+-;4<;;4++) if(A)i*> A)4*) { int temp A)i*; A)i* A)4*; A)4* temp; ' cout<<"#n6he arra/ after sorting is$$ #n"; for(i !;i<;;i++) cout<<A)i*<<" "; cout<<"#n#n>nter the num%er /ou (ant to search "; cin>>val; found DH;AEJL@>AE:P(A,;,val); if(found>!)cout<<"#nHtem found at "<<found+-<<"th place."; else cout<<"#nHtem not found"; getch();'

0utput$

--. /* program to accept arra/ A of ; float elements.function to delete an item from the arra/ at kth location*/ #include<iostream.h> #include<conio.h> #include<process.h> void B>?>6>LH6>C(float *A,int R;,float item) { int p,chk !; for(int i !;i<;;i++) if(A)i* item) { chk++; p i; %reak; ' if(chk !) //checking if no. is present in arra/ { cout<<"#n element not present in the arra/"; cout<<"#n press an/ ke/"; getch(); e1it(!); ' for(i p;i<;;i++) A)i* A)i+-*; ;88; //decrementing the si&e after deletion ' void main() { clrscr(); float *A,item; int ;,k; cout<<"#n enter the si&e of arra/$"; cin>>;; do { if(;< !)//checking if si&e is < ! { cout<<"#n si&e should %e > !.enter again$"; cin>>;; ' '(hile(;< !); A ne( float);*; cout<<"#n enter the "<<;<<" elements of arra/$#n"; for(int i !;i<;;i++) { cout<<" "; cin>>A)i*; ' cout<<"#n the original arra/ is$#n"; for(i !;i<;;i++) cout<<" "<<A)i*; cout<<"#n#n enter the element to %e deleted$"; cin>>item;//item to %e deleted

B>?>6>LH6>C(A,;,item); cout<<"#n the ne( arra/ is$#n"; for(i !;i<;;i++) cout<<" "<<A)i*; getch(); ' 0utput$

-,. /* program to accept arra/ A of ; float elements.function to insert an item in the arra/ at kth location */ #include<iostream.h> #include<conio.h> #include<stdli%.h> //- e1tra free space is needed in the memor/ for its correct (orking void H;@>E6LH6>C(float *A,float item,int R;,int S) { if(S< !GGS>;) { cout<<"#n sorr/ insertion not possi%le$"; cout<<"#n specified position %e/ond si&e of arra/"; cout<<"#n press an/ ke/"; getch(); e1it(!); ' for(int i ;;i> S;i88) A)i* A)i8-*; A)S8-* item; //inserting item at kth location ;++; //incrementing the si&e after insertion ' void main() { clrscr(); float *A,item; int ;,k; cout<<"#n enter the si&e of arra/$"; cin>>;; do { if(;< !)//checking if si&e is < ! { cout<<"#n si&e should %e > !.enter again$"; cin>>;; ' '(hile(;< !); A ne( float);*; cout<<"#n enter the "<<;<<" elements of arra/$#n"; for(int i !;i<;;i++) { cout<<" "; cin>>A)i*; ' cout<<"#n#n enter the item to %e inserted$"; cin>>item;//item to %e searched cout<<"#n enter the location (here element is to %e inserted$"; cin>>k; do { if(k< !RRk>!)//checking if loction is < ! { cout<<"#n location should %e > ! R < si&e.enter again$"; cin>>k; '

'(hile(k< !); cout<<"#n the original arra/ is$#n"; for(i !;i<;;i++) cout<<" "<<A)i*; H;@>E6LH6>C(A,item,;,k);//inserting the arra/ cout<<"#n#n the ne( arra/ is$#n"; for(i !;i<;;i++) cout<<" "<<A)i*; getch(); ' 0utput$

-3/* program to accept ARD containing ;RC elements respectivel/ and concat ARD and store the result in the third arra/ : %/ using a function :0;:A6() */ #include<iostream.h> #include<conio.h> int *:0;:A6(int *A,int*D,int C,int ;) { int *:; for(int i !;i<;;i++) // concating %oth arra/s :)i* A)i*; for(int 4 !;4<C;4++) :)(;)+4* D)4*; return(:); ' void main() { clrscr(); int *A,*D,C,;,*:; cout<<"#n enter the si&e of -st arra/$"; cin>>;; (hile(;< !) { if(;< !) { cout<<"#n the si&e of arra/ must %e > than !"; cout<<"#n#n enter again$"; cin>>;; ' ' cout<<"#n enter the si&e of ,nd arra/$"; cin>>C; (hile(C< !) { if(C< !) { cout<<"#n the si&e of arra/ must %e > than !"; cout<<"#n#n enter again$"; cin>>C; ' ' cout<<"#n enter the "<<;<<" elements of -st arra/$#n#n"; for(int i !;i<;;i++) { cout<<" "; cin>>A)i*; ' cout<<"#n enter the "<<C<<" elements of ,nd arra/$#n#n"; for(int 4 !;4<C;4++) { cout<<" "; cin>>D)4*; ' : :0;:A6(A,D,C,;);

cout<<"#n the -st arra/ is$#n#n"; for(i !;i<;;i++) cout<<" "<<A)i*; cout<<"#n the second arra/ is$#n#n"; for(4 !;4<C;4++) cout<<" "<<D)4*; cout<<"#n the concated third arra/ is$#n#n"; for(int k !;k<(C+;);k++) cout<<" "<<:)k*; getch(); ' 0utput$

-9. /* program to Accept t(o matri1 A and D of order 3 T 9 and 3 T 9 respectivel/.function ABBCA6() to calculate sum of A and D.*/ #include<iostream.h> #include<conio.h> #include<process.h> //structure to store a matri1 of -! T -! order struct CA6EHT { int Cat)-!*)-!*; '; /* function to add t(o matri1 A of m T n order and matri1 D of p T U order */ CA6EHT ABBCA6(int A)*)-!*,int m,int n,int D)*)-!*,int p,int U) { //assuming columns as ma1. -! CA6EHT :; //to store sum of A and D if(m2 pGGn2 U) //checking if ro(s and columns are eUual { cout<<"#n order of %oth matri1 are not same"; cout<<"#n @um calculation is not possi%le"; getch(); e1it(!); ' else { for(int i !;i<m;i++) for(int 4 !;4<n;4++) :.Cat)i*)4* A)i*)4*+D)i*)4*; return(:); //returning sum of A and D ' ' void main() { clrscr(); CA6EHT A,D,:; /* A is to store matri1 of 3 T 9 order D is to store matri1 of 3 T 9 order : is to store sum of A and D */ int c ,,r 9;//for colomn and ro( cout<<"#n >nter first matri1 of 3 T 9 order$#n"; for(int i !;i<3;i++) { for(int 4 !;4<9;4++) { goto1/(c,r); cin>>A.Cat)i*)4*; //accepting first matri1 A c+ I; ' r++;

c ,; ' c ,; r -!; cout<<"#n >nter second matri1 of 3 T 9 order$#n"; for(i !;i<3;i++) { for(int 4 !;4<9;4++) { goto1/(c,r); cin>>D.Cat)i*)4*; //accepting second matri1 D c+ I; ' r++; c ,; ' cout<<"#n Qirst matri1$#n#n"; for(i !;i<3;i++) { //displa/ing matri1 A cout<<" "; for(int 4 !;4<9;4++) cout<<A.Cat)i*)4*<<"#t"; cout<<endl; ' cout<<"#n @econd matri1$#n#n"; for(i !;i<3;i++) { cout<<" "; //displa/ing matri1 D for(int 4 !;4<9;4++) cout<<D.Cat)i*)4*<<"#t"; cout<<endl; ' : ABBCA6(A.Cat,3,9,D.Cat,3,9); /* calculating sum of A and D using function ABBCA6() */ cout<<"#n @um of the matrices$#n#n"; for(i !;i<3;i++) { cout<<" "; //displa/ing sum of A and D for(int 4 !;4<9;4++) cout<<:.Cat)i*)4*<<"#t"; cout<<endl; ' getch(); '

0utput$

-I./* a program to accept t(o matrices A and D of ,13 and 31, order respective/, calculate their product using functon 7E0CA6() and displa/ A, D and 7E0B5:6*/ #include<iostream.h> #include<conio.h> //structure to store a matri1 of -! T -! order struct CA6EHT { int Cat)-!*)-!*; '; /* function to multipl/ t(o matri1 A of m T n order and matri1 D of p T U order */ CA6EHT 7E0CA6(int A)*)-!*,int m,int n,int D)*)-!*,int U) { //assuming ,nd order of matrices i.e. columns as ma1. -! CA6EHT :; //to store product of A and D for(int i !;i<m;i++) for(int 4 !;4<U;4++) { :.Cat)i*)4* !; /* initiali&ing first element of each ro( (ith ! */ for(int k !;k<n;k++) :.Cat)i*)4*+ A)i*)k**D)k*)4*; //calculating product ' return(:); //returning product of A and D ' /* program to Accept t(o matri1 A and D of order , T 3 and 3 T , respectivel/.function 7E0CA6() to calculate product of A and D */ void main() { clrscr(); CA6EHT A,D,:; /* A is to store matri1 of , T 3 order D is to store matri1 of 3 T , order : is to store product of A and D */ int c ,,r 9;//for colomn and ro( cout<<"#n >nter first matri1 of , T 3 order$#n"; for(int i !;i<,;i++) { for(int 4 !;4<3;4++) { goto1/(c,r); cin>>A.Cat)i*)4*; //accepting first matri1 A c+ I; ' r++; c ,; '

c ,; r -!; cout<<"#n >nter second matri1 of 3 T , order$#n"; for(i !;i<3;i++) { for(int 4 !;4<,;4++) { goto1/(c,r); cin>>D.Cat)i*)4*; //accepting second matri1 D c+ I; ' r++; c ,; ' cout<<"#n Qirst matri1$#n#n"; for(i !;i<,;i++) { //displa/ing matri1 A cout<<" "; for(int 4 !;4<3;4++) cout<<A.Cat)i*)4*<<"#t"; cout<<endl; ' cout<<"#n @econd matri1$#n#n"; for(i !;i<3;i++) { cout<<" "; //displa/ing matri1 D for(int 4 !;4<,;4++) cout<<D.Cat)i*)4*<<"#t"; cout<<endl; ' : 7E0CA6(A.Cat,,,3,D.Cat,,); /* calculating product of A and D using function 7E0CA6() */ cout<<"#n 7roduct of the matrices$#n#n"; for(i !;i<,;i++) { //displa/ing product of A and D cout<<" "; for(int 4 !;4<,;4++) cout<<:.Cat)i*)4*<<"#t"; cout<<endl; ' getch(); ' output:

Vous aimerez peut-être aussi