Successfully reported this slideshow.
Your SlideShare is downloading. ×

Computer practicals(part) Class 12

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
/* Write a program to find out the two digit numbers in an array? */
#include<iostream.h>
#include<conio.h>
void twodigit(...
cin>>n;
cout<<"nEnter the Elements of the array: ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
twodigit(a,m,n);
getch...
/* WAP to find the stream of engineering a student goes (Using OOP). According to following guidelines:
marks>=96 computer...
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Loading in …3
×

Check these out next

1 of 87 Ad

More Related Content

Slideshows for you (18)

Similar to Computer practicals(part) Class 12 (20)

Advertisement

Recently uploaded (20)

Computer practicals(part) Class 12

  1. 1. /* Write a program to find out the two digit numbers in an array? */ #include<iostream.h> #include<conio.h> void twodigit(int a[10][10],int m,int n) { int i,j,flag=0; cout<<"The Two digit Numbers are: "; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(a[i][j]>=10&&a[i][j]<=99) { cout<<a[i][j]<<ends; flag=1; } } if(flag==0) cout<<"None"; } void main() { clrscr(); int a[10][10],i,j,m,n; cout<<"n EnterNo. of rows: "; cin>>m; cout<<"nEnter the no. of columns: ";
  2. 2. cin>>n; cout<<"nEnter the Elements of the array: "; for(i=0;i<m;i++) for(j=0;j<n;j++) cin>>a[i][j]; twodigit(a,m,n); getch(); } /***********************OUTPUT*********************************** EnterNo. of rows: 2 Enter the no. of columns: 2 Enter the Elements of the array: 1 2 3 4 5 6 The Two digit Numbers are: None **************************************** */
  3. 3. /* WAP to find the stream of engineering a student goes (Using OOP). According to following guidelines: marks>=96 computer sc marks>=91&marks<=95 Electronics marks>=86&marks<=90 Mechanical marks>=81&marks<=85 Electrical marks>=76&marks<=80 Chemical marks>=71&marks<=75 Civil marks<70 none */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<process.h> class student { char name[30]; int rollno; int marks; public: void input() { cout<<"nEnter Name: "; gets(name); cout<<"Enter Rollno.: "; cin>>rollno; cout<<"enter marks"; cin>>marks;
  4. 4. } void display() { cout<<"n"<<name<<"t"<<rollno<<"t"<<marks<<"t"; if(marks>=96) cout<<"computer sc."; else if(marks>=91&&marks<=95) cout<<"Electronics"; else if(marks>=86&&marks<=90) cout<<"Mechanical"; else if(marks>=81&&marks<=85) cout<<"Electrical"; else if(marks>=76&&marks<=80) cout<<"Chemical"; else if(marks>=71&&marks<=75) cout<<"Civil"; else cout<<"none"; } }; void main() { clrscr(); student s; int n,i,j;
  5. 5. fstream ofile,afile; char ch,ch1; do { cout<<"nt1.Add recordsnt2.Show Recordsnt3.Exit"; cin>>ch; switch(ch) { case '1' : ofile.open("st.dat",ios::app|ios::binary); cout<<"nEnter no. of records to be Entered: "; cin>>n; for(i=0;i<n;i++) { s.input(); ofile.write((char*)&s,sizeof(student)); } ofile.close(); break; case '2' : cout<<"nNametRollnotMarkstStream"; afile.open("st.dat",ios::in); while(afile) { afile.read((char *)&s,sizeof(student)); if (!afile)
  6. 6. break; s.display(); } afile.close(); break; case '3' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1; }while(ch1=='Y'); getch(); } /********************** OUTPUT ********************************* 1.Add records 2.Show Records 3.Exit 2 Name Rollno Marks Stream Ayshkant 5 90 Mechanical Shuvam 6 70 none Bhabesh 1 78 Chemical DO U want to continue n *********************************************** */
  7. 7. /* WAP to enter and display the following details of employee in tabular form (in class) Employee No. Employee Name Salary */ #include<iostream.h> #include<conio.h> #include<stdio.h> #include<process.h> #include<iomanip.h> class employee { int eno; char name[30]; float salary; public : void input() { cout << "Enter Employee Number "; cin >>eno; cout << "Enter name "; gets(name); cout << "Enter salary "; cin >>salary; } void show() { cout << eno << setw(20)<<name<<setw(20)<<salary<<endl;
  8. 8. } float rt_sal() { return salary; } }emp[10]; main() { int n,ch,i,j; char choice; do { clrscr(); cout << "1. For enter "<<endl; cout << "2. For tabular report"<<endl; cout << "3. For exit"; cin >> ch; switch(ch) { case 1: cout << "Enter how many employees "; cin >>n; for(i=0;i<n;i++) { emp[i].input(); } break;
  9. 9. case 2: employee temp; for (i=0;i<n;i++) { for(j=i;j<n-1;j++) { if (emp[j].rt_sal()>emp[j+1].rt_sal()) { temp = emp[j]; emp[j]=emp[j+1]; emp[j+1]=temp; } } } gotoxy(6,6); cout <<"Employee Number "; gotoxy(26,6); cout <<"Name"; gotoxy(46,6); cout <<"Salary"<<endl; int r = 8; for(i=0;i<n;i++) { emp[i].show(); r++; } break; case 3: exit(0);
  10. 10. } cout << "n Do U want to continue"; cin>>choice; }while(choice == 'Y' ||choice =='y'); } /******************************** OUTPUT ******************************* 1. For enter 2. For tabular report 3. For exit 2 Employee Number Name Salary 1 Dan 700 2 Nick 900 Do U want to continue n *************************************************************** */
  11. 11. /* WAP to find the no. of words, vowels, numbers in a given text? */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<ctype.h> #include<process.h> void main() { clrscr(); int n,j; fstream ofile,afile; char str[100]; char ch,ch1; do { cout<<"nt1.Create Textnt2.Count vowels/words/digitsnt3.Show Textnt4.Exit"; cin>>ch; switch(ch) { case '1' : ofile.open("smp.txt",ios::out); cout<<"n Enter The Text "; gets(str); ofile<<str; ofile.close(); break;
  12. 12. case '2' : char tmp1; int v=0,d=0,w=0; afile.open("smp.txt",ios::in); while(!afile.eof()) { afile.get(tmp1); if(tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u') v++; if(isdigit(tmp1)) d++; if(tmp1==' '||tmp1=='.') w++; } afile.close(); cout<<"n No of Vowels: "<<v; cout<<"n No of digits: "<<d+1; cout<<"n No of words: "<<w; break; case '3' : char tmp2; afile.open("smp.txt",ios::in); ofile.open("spl.txt",ios::out); while(!afile.eof()) {
  13. 13. afile.get(tmp2); if(tmp2==' ') { ofile<<'#'; } else { ofile<<tmp2; } } afile.close(); ofile.close(); cout<<"nFormatted text:t"; afile.open("spl.txt",ios::in); while(afile) { afile.get(ch); cout<<ch; } afile.close(); break; case '4' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1; }while(ch1=='Y'||ch1=='y');
  14. 14. getch(); } /********************* OUTPUT ************************************ 1.Create Text 2.Count vowels/words/digits /* 3.Show Text 4.Exit 1 Enter The Text india DO U want to continue y 1.Create Text 2.Count vowels/words/digits 3.Show Text 4.Exit 2 No of Vowels: 3 No of digits: 1 No of words: 0 DO U want to continue y 1.Create Text 2.Count vowels/words/digits 3.Show Text 4.Exit 3 Formatted text: indiaÿÿ DO U want to continue n ******************************************************** */
  15. 15. /* WAP to create a text and find the upper case and lower case constants and vowels? */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<ctype.h> #include<process.h> void main() { clrscr(); int n,j; fstream ofile,afile; char str[100]; char ch,ch1; do { cout<<"nt1.Create Textnt2.Read from Filent3.create another file"; cout << "n 4.Exit "; cin>>ch; switch(ch) { case '1' : ofile.open("smp.txt",ios::out); cout<<"n Enter The Text "; gets(str); ofile<<str; ofile.close(); break;
  16. 16. case '2' : char tmp1; afile.open("smp.txt",ios::in); while(!afile.eof()) { afile.get(tmp1); if(isalpha(tmp1)) { if (islower(tmp1)) { if (tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u') cout << "n Lower case vowel "<<tmp1; else cout<<"n Lower case consonants "<<tmp1; } if (isupper(tmp1)) { if (tmp1=='A'||tmp1=='E'||tmp1=='I'||tmp1=='O'||tmp1=='U') cout << "n Upper case vowel "<<tmp1; else cout<<"n Lower case consonants "<<tmp1; } } } afile.close(); break;
  17. 17. case '3' : ofile.open("smp.txt",ios::in); afile.open("smp1.txt",ios::out); char c; while(ofile) { ofile.get(c); c = tolower(c); if (c=='a'||c=='i'||c=='e'||c=='o'||c=='u') afile.put(c); } ofile.close(); afile.close(); case '4' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1; }while(ch1=='Y'||ch1=='y'); getch(); } /******************* OUTPUT ************************************** 1.Creat Text 2.Read from file 3.creat another file 4.Exit 1
  18. 18. Enter The Text I am the King DO U want to continue Y 1.Create Text 2.Read from File 3.create another file 4.Exit 2 Upper case vowel I Lower case vowel a Lower case consonants m Lower case consonants t Lower case consonants h Lower case vowel e Lower case consonants K Lower case vowel i Lower case consonants n Lower case consonants g DO U want to continue n ********************************************************** */
  19. 19. /* WAP to append record and search in a telephone directory? */ # include <fstream.h> #include <conio.h> #include <string.h> #include <stdio.h> #include <stdlib.h> class telephone { char name[30]; char address[50]; double tno; public : void input() { cout<<"n Enter the name "; gets(name); cout << "n Enter address "; gets(address); cout<<"n Enter the telephone number "; cin>>tno; } void show() { cout << "n Name "<<name; cout << "n Address "<<address; } double rt_tno()
  20. 20. { return tno; } }tele; // Function to append the records in file void append() { ofstream tfile; telephone tele; tfile.open("tele.dat", ios :: app); int n,i; cout<< "Enter how many customers "; cin>>n; for (i =0; i<n ;i++) { tele.input(); tfile.write((char *)& tele,sizeof(tele)); } tfile.close(); } // Function to search a record in the file void display() { ifstream tfile; tfile.open("tele.dat",ios :: binary); int no,flag;
  21. 21. flag = 0; cout<< "n Enter telephone number to be searched "; cin>>no; while(tfile) { tfile.read((char *)&tele , sizeof(tele)); if(!tfile) break; if (tele.rt_tno() == no) { tele.show(); flag = 1; } } if (flag == 0) cout<< "n Record does not exist "; } void main() { clrscr(); int ch; cout << " 1. For append record "; cout <<"n 2. For search "; cout << "n 3. For exit"; cin >> ch; switch (ch) {
  22. 22. case 1: append(); break; case 2: display(); break; case 3 : exit(0); } } /***************************OUTPUT*************************** 1. For append record 2. For search 3. For exit 1 Enter how many customers 2 Enter the name Dan Enter address KNAGAR Enter the telephone number 223344 Enter the name mak Enter address MNAGAR Enter the telephone number 334455 Do Yoy want to cotinue y 1. For append record 2. For search 3. For Exit 2Enter the number to be searched 556677 Record does not exist Do you want to continue n ********************************************************* */
  23. 23. /* WAP to record the blood donor’s name, address and blood group and search by the blood group when required? */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> class donor { char name[30]; char address[30]; char bgroup[5]; public: void input() { cout<<"nEnter Donor Name: "; gets(name); cout<<"Enter Address: "; gets(address); cout<<"Enter Blood Group: "; gets(bgroup); } void display() { cout<<"nDonor Name: "<<name<<"tAddress: "<<address<<"tBlood Group: "<<bgroup<<"t"; }
  24. 24. char *getbgroup() { return bgroup; } }; void main() { clrscr(); donor d; int n,i,j; fstream ofile,afile; char ch,ch1; do { cout<<"nt1.Add recordsnt2.Search Recordsnt3.List Recordsnt4.Exit"; cin>>ch; switch(ch) { case '1' : ofile.open("dnr.dat",ios::out|ios::binary); cout<<"nEnter no. of records to be Entered: "; cin>>n; for(i=0;i<n;i++) {
  25. 25. d.input(); ofile.write((char*)&d,sizeof(donor)); } ofile.close(); break; case '2' : cout<<"nEnter Blood Group to be searched: "; char bg[5],flag=0; gets(bg); afile.open("dnr.dat",ios::in); while(afile) { afile.read((char *)&d,sizeof(donor)); if(!afile) break; if (strcmp(bg,d.getbgroup())==0) { d.display(); flag=1; } } if(flag==0) cout<<"n No record Found"; afile.close(); break; case '3' : afile.open("dnr.dat",ios::in);
  26. 26. while(afile) { afile.read((char *)&d,sizeof(donor)); if(!afile) break; d.display(); } afile.close(); break; case '4' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1; }while(ch1=='Y'||ch1=='y'); getch(); } /**********************OUTPUT*************************************** 1.Add records 2.Search Records 3.List Records 4.Exit 1 Enter no. of records to be Entered: 2
  27. 27. Enter Donor Name: Ayashkant Enter Address: pl no. 820 Enter Blood Group: B+ Enter Donor Name: Swaraj Enter Address: Pl. 98 Enter Blood Group: A+ DO U want to continue y 1.Add records 2.Search Records 3.List Records 4.Exit 2 Enter Blood Group to be searched: O+ No record Found DO U want to continue N ****************************************** */
  28. 28. /* WAP in C++ to record the book no., book name and price and search by its number when required (USING CLASS)? */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<process.h> class book { char bname[30]; int bno; float price; public: void input() { cout<<"nEnter Book Name: "; gets(bname); cout<<"Enter BOOK No.: "; cin>>bno; cout<<"Enter Price"; cin>>price; } void setprice() { cout<<"nEnter Price"; cin>>price; } void display()
  29. 29. { cout<<"nBook Name: "<<bname<<"tBook No.: "<<bno<<"tPrice: "<<price<<"t"; } int getbno() { return bno; } }; void main() { clrscr(); book b; int n,i,j; fstream ofile,afile; char ch,ch1; do{ cout<<"nt1.Add recordsnt2.Search Recordsnt3.Modify Recordsnt4.Exit"; cin>>ch; switch(ch) { case '1' : ofile.open("bk.dat",ios::out|ios::binary); cout<<"nEnter no. of records to be Entered: "; cin>>n; for(i=0;i<n;i++)
  30. 30. { b.input(); ofile.write((char*)&b,sizeof(book)); } ofile.close(); break; case '2' : cout<<"nEnter Book No. to be searched: "; int bn,flag=0; cin>>bn; afile.open("bk.dat",ios::in); while(afile) { afile.read((char *)&b,sizeof(book)); if(!afile) break; if (bn==b.getbno()) { b.display(); flag=1; break; } } if(flag==0) cout<<"n No record Found"; afile.close(); break; case '3' :
  31. 31. cout<<"nEnter Book No. to be modified "; int bn1,flag1=0,r=0; cin>>bn1; afile.open("bk.dat",ios::in|ios::out|ios::binary); while(afile) { afile.read((char *)&b,sizeof(book)); if(!afile) break; if (bn1==b.getbno()) { b.setprice(); afile.seekp(r*sizeof(b),ios::beg); afile.write((char *)&b,sizeof(book)); flag1=1; break; } r++; } if(flag1==0) cout<<"n No record Found"; afile.close(); break; case '4' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1;
  32. 32. }while(ch1=='Y'||ch1=='y'); getch(); } /****************************OUTPUT********************** 1.Add records 2.Search Records 3.Modify Records 4.Exit 1 Enter no. of records to be Entered: 2 Enter Book Name: The Wolf Of The Wallstreet Enter BOOK No.: 34 Enter Price 50 Enter Book Name: The Oath Of The Vayuputra Enter BOOK No.: 47 Enter Price 8 DO U want to continue y 1.Add records 2.Search Records 3.Modify Records 4.Exit 2 Enter Book No. to be searched: 34 Book Name: The Wolf Of The Wallstreet Book No.: 34 Price: 50 DO U want to continue n ****************************************************** */
  33. 33. /* WAP to record the student name, roll no. and fees and display its details when searched by roll no.? */ #include<fstream.h> #include<stdio.h> #include<conio.h> #include<process.h> class students { char sname[30]; int sno; float fees; public: void input() { cout<<"nEnter Student Name: "; gets(sname); cout<<"Enter Roll No.: "; cin>>sno; cout<<"Enter Fees: "; cin>>fees; } void display() { cout<<"nStudent Name: "<<sname<<"tRoll No.: "<<sno<<"tFees: "<<fees<<"t"; } int getsno() { return sno;}
  34. 34. }; void main() { clrscr(); students s; int n,i,j; fstream ofile,afile; char ch,ch1; do { cout<<"nt1.Add recordsnt2.Search Recordsnt3.Delete Recordsnt4.Exit"; cin>>ch; switch(ch) { case '1' : ofile.open("std.dat",ios::out|ios::binary); cout<<"nEnter no. of records to be Entered: "; cin>>n; for(i=0;i<n;i++) { s.input(); ofile.write((char*)&s,sizeof(students)); } ofile.close(); break; case '2' : cout<<"nEnter Roll No. to be searched: ";
  35. 35. int sn,flag=0; cin>>sn; afile.open("std.dat",ios::in); while(afile) { afile.read((char *)&s,sizeof(students)); if(!afile) break; if (sn==s.getsno()) { s.display(); flag=1; break; } } if(flag==0) cout<<"n No record Found"; afile.close(); break; case '3' : cout<<"nEnter Roll No. to be Deleted "; int sn1,flag1=0; cin>>sn1; afile.open("std.dat",ios::in|ios::binary); ofile.open("tmpstd.dat",ios::out|ios::binary); while(afile) {
  36. 36. afile.read((char *)&s,sizeof(students)); if(!afile) break; if (sn1==s.getsno()) { flag1=1; } else { ofile.write((char *)&s,sizeof(students)); } } if(flag1==0) cout<<"n No record Found"; afile.close(); ofile.close(); afile.open("tmpstd.dat",ios::in|ios::binary); ofile.open("std.dat",ios::out|ios::binary); while(afile) { afile.read((char *)&s,sizeof(students)); ofile.write((char *)&s,sizeof(students)); } afile.close();
  37. 37. ofile.close(); break; case '4' : exit(0); } cout<<"nt DO U want to continue "; cin>>ch1; }while(ch1=='Y'||ch1=='y'); getch(); } /************************OUTPUT******************************* 1.Add records 2.Search Records 3.Delet Records 4.Exit 1 Enter no. of records to be Entered: 2 Enter Student Name: Daniel Enter Roll No.: 23 Enter Fees: 1200 Enter Student Name: Max
  38. 38. Enter Roll No.: 56 Enter Fees: 7890 DO U want to continue y 1.Add records 2.Search Records 3.Delete Records 4.Exit 2 Enter Roll No. to be searched: 23 Student Name: Daniel Roll No.: 23 Fees: 1200 DO U want to continue n ********************************************************* */
  39. 39. // This program illustrates the basic operation of add queue, delete queue and show queue // using linked list. #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> // Declares a queue structure struct node { int Eno; float Salary; node *link; }; // Functions prototype to add queue, delete queue, and show queue node *add_Q(node *rear, int val,float val1); // Add queue node *del_Q(node *front, int &val, float &val1);// Delete queue void show_Q(node *front); // Show queue // Main programming logic void main() { node *front, *rear; int val; float val1; int choice; char opt = 'Y'; // To continue the do loop in case
  40. 40. front = rear = NULL; // Initialization of Queue clrscr(); do { cout << "ntt Main Menu"; cout << "nt1. Addition of Queue"; cout << "nt2. Deletion from Queue"; cout << "nt3. Traverse of Queue"; cout << "nt4. Exit from Menu"; cout << "nnEnter Your choice from above "; cin >> choice; switch (choice) { case 1: do { cout << "Enter the value to be added in the queue "; cin >> val; cin >> val1; rear = add_Q(rear, val,val1); if (front == NULL) front = rear; cout << "nDo you want to add more element <Y/N>? "; cin >> opt; } while (toupper(opt) == 'Y'); break;
  41. 41. case 2: opt = 'Y';// Initialize for the second loop do { front = del_Q(front, val, val1); if (front == NULL) rear = front; if (val != -1) cout << "Value deleted from Queue is " << val; cout << "nDo you want to delete more element <Y/N>? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 3: show_Q(front); break; case 4: exit(0); } } while (choice != 4); } // Function body to add queue elements node *add_Q(node *rear, int val, float val1) { node *temp;
  42. 42. temp = new node; temp->Eno = val; temp->Salary = val1; temp->link = NULL; rear->link = temp; rear = temp; return (rear); } // Function body to delete queue elements node *del_Q(node *front, int &val, float &val1) { node *temp; clrscr(); if (front == NULL) { cout << "Queue Empty "; val = -1; } else { temp = front; front = front->link; val = temp->Eno; val1 = temp->Salary; temp->link = NULL; delete temp; }
  43. 43. return (front); } // Function body to show queue elements void show_Q(node *front) { node *temp; temp = front; clrscr(); cout << "The Queue values are"; while (temp != NULL) { cout <<"nENO : "<< temp->Eno; cout <<"nSalary : "<<temp->Salary; temp = temp->link; } } /******************************OUTPUT******************************** Main Menu 1. Addition of Queue 2. Deletion from Queue 3. Traverse of Queue 4. Exit from Menu Enter Your choice from above 1 Enter the value to be added in the queue 234 567
  44. 44. Do you want to add more element <Y/N> n Main Menu 1. Addition of Queue 2. Deletion from Queue 3. Traverse of Queue 4. Exit from Menu Enter Your choice from above 2 The Queue values are ENO : 234 Salary : 567 Main Menu 1. Addition of Queue 2. Deletion from Queue 3. Traverse of Queue 4. Exit from Menu Enter Your choice from above 4 ********************************************************************** */
  45. 45. /* WAP to add, delete and transverse stacks? */ #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> // Declares a stack structure struct node { int roll; int age; node *link; }; // Function prototype declaration for add stack, delete stack, and show stack node *push(node *top, int val, int tage); // Add stack node *pop(node *top); // Delete stack void show_Stack(node *top); // Show stack // Main programming logic void main() { node *top; int troll, tage, choice; char opt = 'Y'; // To continue the do loop in case top = NULL; // Initialization of Stack clrscr(); do {
  46. 46. cout << "ntt Main Menu"; cout << "nt1. Addition of Stack"; cout << "nt2. Deletion from Stack"; cout << "nt3. Traverse of Stack"; cout << "“nt4. Exit from Menu"; cout << "nnEnter your choice from above "; cin >> choice; switch (choice) { case 1: do { cout << "Enter the roll no. : "; cin >> troll; cout << "Enter age : "; cin >> tage; top = push(top, troll, tage); cout << "nDo you want to add more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 2: opt = 'Y'; // Initialize for the second loop do { top = pop(top); if (troll != -1)
  47. 47. cout << "Value deleted from Stack is " << troll; cout << "nDo you want to delete more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 3: show_Stack(top); break; case 4: exit(0); } } while (choice != 4); } // Function body for adds stack elements node *push(node *top, int val, int tage) { node *temp; temp = new node; temp->roll = val; temp->age = tage; temp->link = NULL; if(top ==NULL) top = temp; else { temp->link = top;
  48. 48. top = temp; } return(top); } // Function body for delete stack elements node *pop(node *top) { node *temp; int tage, troll; clrscr(); if (top == NULL ) { cout << "Stack Empty "; troll = -1; } else { temp = top; top = top->link; troll = temp->roll; tage = temp->age; temp->link = NULL; cout << "ntPopped Roll Number is : " << temp->roll; cout << "ntPopped Age is : " << temp->age; delete temp; } return (top);
  49. 49. } // Function body for show stack elements void show_Stack(node *top) { node *temp; temp = top; clrscr(); cout << "The values are n"; while (temp != NULL) { cout << "n" << temp->roll << "t" << temp->age; temp = temp->link; } } /***********************OUTPUT*********************************** Main Menu 1. Addition of Stack 2. Deletion from Stack 3. Traverse of Stack“ 4. Exit from Menu Enter your choice from above 1 Enter the roll no. : 1 Enter age : 15 Do you want to add more elements <Y/N> ? y
  50. 50. Enter the roll no. : 2 Enter age : 17 Do you want to add more elements <Y/N> ? n Main Menu 1. Addition of Stack 2. Deletion from Stack 3. Traverse of Stack 4. Exit from Menu Enter your choice from above 3 The values are 2 17 1 15 Main Menu 1. Addition of Stack 2. Deletion from Stack 3. Traverse of Stack“ 4. Exit from Menu Enter your choice from above 4 *********************************************************** */
  51. 51. /*This program illustrates the basic operation of circular to add queue, delete queue and show queue using array. The queue contains data of type character. */ #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> #define MAX 20 // Show maximum array length char queue[MAX]; // Declares array global variable int front, rear; // Declares integer front and read // Function prototypes to add queue, delete queue and show queue in array implementation void add_Q(char queue[], int front, char val, int &rear); // Add queue char del_Q(char queue[], int &front, int rear); // Delete queue void show_Q(char queue[], int front, int rear); // Show queue void main() { int choice; char val; char opt = 'Y'; // To continue the do loop in case rear = -1; // Initialization of Queue ront = -1; clrscr(); do { cout << "ntt Main Menu"; cout << "nt1. Addition of Queue"; cout << "nt2. Deletion from Queue";
  52. 52. cout << "nt3. Traverse of Queue"; cout << "nt4. Exit from Menu"; cout << "nnEnter Your choice from above "; cin >> choice; switch (choice) { case 1: do { cout << "Enter the value to be added in the queue "; cin >> val; add_Q(queue, front, val, rear); cout << "Do you want to add more element <Y/N>? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 2: opt = 'Y';// Initialize for the second loop do { val = del_Q(queue, front, rear); if (val != -1) cout << "Value deleted from Queue is " << val; cout << "nDo you want to delete more element <Y/N>? "; cin >> opt; } while (toupper(opt) == 'Y'); break;
  53. 53. case 3: show_Q(queue, front, rear); break; case 4: exit(0); } } while (choice != 4); } // Function body to add circular queue with array of character void add_Q(char queue[], int front, char val, int &rear) { if ((rear + 1) % MAX == front) { cout << "Queue Full "; } else { rear = (rear + 1) % MAX; queue[rear] = val; } } // Function body to delete circular queue with array of character char del_Q(char queue[], int &front, int rear) { char value; if (front == rear)
  54. 54. { cout << "Queue Empty "; value = -1; } else { front = (front + 1) % MAX; value = queue[front]; } return (value); } // Function body to show circular queue with array void show_Q(char queue[], int front, int rear) { clrscr(); cout << "The values are "; do { front = (front + 1) % MAX; cout << "n" << queue[front]; }while(front != rear); } /***********************OUTPUT************************************************ Main Menu 1. Addition of Queue 2. Deletion from Queue 3. Traverse of Queue 4. Exit from Menu
  55. 55. Enter Your choice from above 1 Enter the value to be added in the queue 3 Do you want to add more element <Y/N>? n Main Menu 1. Addition of Queue 2. Deletion from Queue 3. Traverse of Queue 4. Exit from Menu Enter Your choice from above 4 **************************************************************************** */
  56. 56. /* Program to define the classes PERSON, GAME and STUDENT & to access the essential data using multiple inheritance.*/ #include<iostream.h> #include<stdio.h> #include<conio.h> class person{ char name[21]; int age; public: void indata() {cout<<"nnEnter the name of Student: " ; gets(name); cout<<"nnEnter the age : "; cin>>age; } void outdata(); }; void person::outdata() // since the function contains loop so it is not made inline { cout<<"nn"; for(int i=0; i<79; i++) cout<<"-"; cout<<"nnName of the student is: "<<name; cout<<"nnAge of the student is : "<<age; } class game { char game_name[20]; public: void input() { cout<<"nnEnter the game name : "; cin.get();cin.getline(game_name,20); } void output() { cout<<"nnGame opted by the student is : "<<game_name; } }; class student: public person, public game { float Tmarks; int rollno; public: char calgrade() {if(Tmarks>90) return 'A'; else if(Tmarks>80&&Tmarks<=90) return 'B';
  57. 57. else if(Tmarks>70&&Tmarks<=80) return 'C'; else if(Tmarks>60&&Tmarks<=70) return 'D'; else return 'E'; } void enter() { indata(); // indata() of class person called here cout<<"nnEnter the roll number: "; cin>>rollno; input(); // input() of class game called here cout<<"nnEnter total marks (out of 100) : "; cin>>Tmarks; } void display() { outdata(); cout<<"nnRoll number : "<<rollno; output(); cout<<"nnTotal marks are : "<<Tmarks; cout<<"nnGrade = "<<calgrade(); } }; void main() { clrscr(); student A; A.enter(); A.display(); getch(); } /**************************OUTPUT********************************* Enter the name of Student: Ayashkant Mishta Enter the age : 16 Enter the roll number: 01 Enter the game name : Kabbadi Enter total marks (out of 100) : 95 -------------------------------------------------------------------------------------------------------------------------------------------------------- ------------ Name of the student is: Ayashkant Mishra
  58. 58. Age of the student is : 16 Roll number : 34 Game opted by the student is : Kabbadi Total marks are : 95 Grade = A ****************************************************************** */
  59. 59. /* Wap in c++ to implement stack as an array */ #include<iostream.h> #include<conio.h> #include<process.h> int pop(int[],int&); int push(int[],int&,int); void display(int[],int); const int size=50; void main() { clrscr(); char m,ch; int k,stack[size],item,top=-1,res; do { cout<<"nChoose from the following : nn" <<"n 1. Push" <<"n 2. Pop" <<"n 3. Display" <<"n 4. Exit" <<"nnEnter your choice : "; cin>>k; switch(k) { case 1: ch='y'; while(ch=='y'||ch=='Y') { cout<<"nEnter the element : "; cin>>item; res=push(stack,top,item); if(res==-1) {cout<<"nOverflow !!!!"; exit(1); }
  60. 60. cout<<"nThe stack formed is : nn"; display(stack,top); cout<<"nnnWant to enter again ?: "; cin>>ch; } break; case 2: ch='y'; while(ch=='y'||ch=='Y') { res=pop(stack,top); if(res==-1) { cout<<"nUnderflow !!!!"; exit(1); } else { cout<<"nThe deleted Element is : "<<res<<endl; cout<<"nThe resultant stack is : nn"; display(stack,top); } cout<<"nWant to delete again ? : "; cin>>ch; } break; case 3: cout<<"nThe resultant stack is : "; display(stack,top); break; case 4: exit(0); break; default: cout<<"nPlease enter desired keyword : "; } // end of switch
  61. 61. cout<<"nnChoose from the menu again ? : "; cin>>m; }while(m=='y'||m=='Y'); // end of do-while loop getch(); } // end of main() int push(int stack[],int &top,int el) { if(top==size-1) return -1; else { top++; stack[top]=el; return 0; } } int pop(int stack[],int &top) { int ret; if(top==-1) return -1; else { ret=stack[top]; top--; return ret; } } void display(int stack[],int top) { cout<<stack[top]<<"<--";
  62. 62. for(int i=top-1;i>=0;i--) cout<<stack[i]<<"<--"; } /************************************OUTPUT******************************************** Choose from the following : 1. Push 2. Pop 3. Display 4. Exit Enter your choice : 1 Enter the element : 1 The stack formed is : 1  Want to enter again ?: y Enter the element : 2 The stack formed is : 2  1  Want to enter again ?: y Enter the element : 3 The stack formed is : 3 2  1  Want to enter again ?: y Enter the element : 4 The stack formed is : 4  3  2  1 
  63. 63. Want to enter again ?: N Choose from the menu again ? : y Choose from the following : 1. Push 2. Pop 3. Display 4. Exit Enter your choice : 2 The deleted Element is : 4 The resultant stack is : 3  2  1  Want to delete again ? : y The deleted Element is : 3 The resultant stack is : 2  1  Want to delete again ? : n Choose from the menu again ? : y Choose from the following : 1. Push 2. Pop 3. Display 4. Exit Enter your choice : 3 The resultant stack is : 2 1  Choose from the menu again ? : n ********************************************************************************** */
  64. 64. /* Wap to implement stack as a linked list */ #include<iostream.h> #include<conio.h> #include<process.h> struct node { int roll; node* next; }*top,*save,*ptr,*newptr,*np; node *create(int a) { ptr=new node; ptr->roll=a; ptr->next=NULL; return ptr; } void push(node *np) { if(top==NULL) top=np; else { save=top; top=np; np->next=save; } } void pop() { if(top==NULL) cout<<"n Underflow!!!!"; else { ptr=top; top=top->next; delete ptr; } } void display(node *np) { while(np!=NULL) { cout<<np->roll<<" -> "; np=np->next; } }
  65. 65. void main() { clrscr(); top=NULL; int n,m; char k,ch; do { cout<<"nChoose from the menu :n" <<"n 1. Push." <<"n 2. Pop." <<"n 3. Display." <<"n 4. Quit." <<"nnEnter your choice : "; cin>>n; switch(n) { case 1: k='y'; while(k=='y'||k=='Y') { cout<<"n Enter element to be inserted ."; cin>>m; newptr=create(m); if(newptr==NULL) cout<<"n Cannot create !!!!"; push(newptr); cout<<"n The Stack formed is : "; display(top); cout<<"nn Want to enter again ?: "; cin>>k; } break; case 2: k='y'; while(k=='y'||k=='Y') { pop(); cout<<"n The Stack formed is : nn"; display(top); cout<<"nn Want to delete again ?: "; cin>>k; } break; case 3: cout<<"n The Stack formed is : ";
  66. 66. display(top); break; case 4: exit(0); break; default: cout<<"n Please enter desired keyword : "; } cout<<"n Do you want to continue..? : "; cin>>ch; }while(ch=='y'||ch=='Y'); getch(); } /*********************************************OUTPUT***************************************** ***** Choose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit. Enter your choice : 1 Enter element to be inserted : 1 The Stack formed is : 1 -> Want to enter again ?: y Enter element to be inserted : 2 The Stack formed is : 2 -> 1 -> Want to enter again ?: y Enter element to be inserted : 3 The Stack formed is :
  67. 67. 3 -> 2 -> 1 -> Want to enter again ?: n Do you want to continue..? : y Choose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit. Enter your choice : 2 The Stack formed is : 2 -> 1 -> Want to delete again ?: y The Stack formed is : 1 -> Want to delete again ?: N Do you want to continue..? : y Choose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit. Enter your choice : 3 The Stack formed is : 1 -> Do you want to continue..? : y Choose from the menu : 1. Push. 2. Pop. 3. Display. 4. Quit. Enter your choice : 4 *********************************************************************************** */
  68. 68. /* Wap in C++ to read file “sports.dat” and copy only those records where event name is ATHLETIC using the concept of Data file handling. */ #include<fstream.h> #include<string.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct sports { char event[20]; char participants[10][30]; int no_of_participants; } s[20], s2[20]; void copy(fstream &ob); int i=0; void main() { clrscr(); char choice; fstream ob("sports.dat",ios::binary|ios::in|ios::out); do { if(i>0) cin.get(); cout<<"nnEnter the name of Event : "; cin.getline(s[i].event,20); cout<<"nnEnter the total number of participants in Event "<<s[i].event<<" : "; cin>>s[i].no_of_participants; cout<<"nnEnter the name of Praticipants : n"; cin.get(); for(int j=0; j<s[i].no_of_participants; j++) cin.getline(s[i].participants[j],30); ob.write((char*)&s[i], sizeof(sports)); cout<<"nnnWant to Enter Details of Another Event (Y/N) : "; cin>>choice; i++; } while(choice=='y'||choice=='Y');
  69. 69. cout<<"nnnnn****************************************************************************** ******************************nn"; copy(ob); cout<<"nn*********************************************************************************** ******************************nnn"; getch(); } void copy(fstream &o) { sports s[20]; o.seekg(0); ofstream file; file.open("athletic.dat",ios::binary); file.seekp(0); int j,n=0; int c=0; while(o) { o.read((char*)&s[c], sizeof(sports)); if(strcmp("athletic",s[c].event)==0) { file.write((char*)&s[c], sizeof(sports)); n=1; break; } c++; } if(n==0) { cout<<"nnUnsuccessful Search."; getch(); exit(0); }o.close();
  70. 70. file.close(); sports sp; ifstream oo; oo.open("athletic.dat",ios::binary); while(oo) { oo.read((char*)&sp, sizeof(sports)); } cout<<"nnThe Records of file are : nn"; cout<<"nnEvent = "<<sp.event; cout<<"nnnnThe Participants are : nn"; for(int i=0; i<sp.no_of_participants; i++) { cout<<sp.participants[i]<<"nn"; } oo.close(); remove("athletic.dat"); remove("sports.dat"); } /**************************************OUTPUT************************************************ ** Enter the name of Event : Cricket Enter the total number of participants in Event Cricket : 3 Enter the name of Praticipants : Rahul verma Shivam Siddharth Want to Enter Details of Another Event (Y/N) : y
  71. 71. Enter the name of Event : athleltic Enter the total number of participants in Event atlhletic : 2 Enter the name of Praticipants : Mohak Vikas Want to Enter Details of Another Event (Y/N) : y Enter the name of Event : Football Enter the total number of participants in Event Football : 2 Enter the name of Praticipants : Arsh Ashu Want to Enter Details of Another Event (Y/N) : n ********************************************************************************************* ************************************ The Records of file are : Event = athletic The Participants are : Mohak Vikas ********************************************************************************************* ************************************ */
  72. 72. /* Wap to print and find the sum of Fibonacci series using recursion. */ #include<iostream.h> #include<conio.h> int fibonacci(int n); void main() { clrscr(); int n; cout<<"nn Enter the number of terms upto which you want the sum of fibonnaci series : "; cin>>n; cout<<"nnThe fibonacci series generated is : nn"; cout<<"1 1 "; int s=fibonacci(n); cout<<"nnThe sum of fibonacci series for "<<n<<" terms = "<<s; getch(); } int first=1; int second=1; int third; int i=2; int sum=0; int fibonacci(int n) { if(n==1) sum=1;
  73. 73. else if(n==2) sum=2; // n = 1 2 3 4 5 else if(n>1 && i!=n) // num = 1 1 2 3 5 { third=first+second; cout<<third<<” “; if(i==2) sum+=first+second+third; else sum+=third; first=second; second=third; ++i; fibonacci(n); } return sum; } /*****************************OUTPUT******************************************* Enter the number of terms upto which you want the sum of fibonnaci series : 5 The fibonacci series generated is : 1 1 2 3 5 The sum of fibonacci series for 5 terms = 12 **************************************************************************************** */
  74. 74. /* Wap in C++ to print and find the sum of even /odd numbers using recursion.*/ #include<iostream.h> #include<conio.h> int sum_even(int ); int sum_odd(int ); int sum=0; int num=0; void main() { clrscr(); int n; int s_e, s_o; cout<<"nnEnter the number upto which you want the sum of even/odd numbers : "; cin>>n; cout<<"nn The list of integers is : nn"; for(int l=1; l<=n; l++) cout<<l<<"n"; s_e=sum_even(n); s_o=sum_odd(n); cout<<"nnThe sum of even numbers upto "<<n<<" = "<<s_e; cout<<"nnThe sum of odd numbers upto "<<n<<" = "<<s_o; getch(); } int sum_even(int n){ if(num%2==0) sum=sum+num; if(num!=n && num<n) { ++num; sum_even(n); } return sum; } int sum_2=0; int num_2=0;
  75. 75. int sum_odd(int n) { if(num_2%2!=0) sum_2=sum_2+num_2; if(num_2!=n) { num_2++; sum_odd(n); } return sum_2; } /*********************************OUTPUT********************************************** Enter the number upto which you want the sum of even/odd numbers : 10 The list of integers is : 1 2 3 4 5 6 7 8 9 10 The sum of even numbers upto 10 = 30 The sum of odd numbers upto 10 = 25 **************************************************************************** */
  76. 76. /* WAP in c++ using pointers to find the smallest and the largest element in a dynamically created array?*/ #include<iostream.h> #include<conio.h> void main() { clrscr(); int *array, smallest, largest, n; cout<<"nnEnter the number of elements : "; cin>>n; array=new[n]; cout<<"nnEnter the elements : nn"; for(int i=0; i<n; i++) cin>>array[i]; i=0; cout<<"nnThe array formed is : nn"; while(i!=n) { cout<<array[i]<<" "; i++; } smallest=array[0]; for(i=0; i<n; i++) { if(array[i]<=smallest) smallest=array[i]; }
  77. 77. largest=array[0]; for(i=0; i<n; i++) { if(array[i]>=largest) largest=array[i]; } cout<<"nnThe smallest element is : "<<smallest<<"nnThe largest element is : "<<largest; getch(); } /*********************************************OUTPUT***************************************** ***************** Enter the number of elements : 5 Enter the elements : 1 9 2 8 6 The array formed is : 1 9 2 8 6 The smallest element is : 1 The largest element is : 9 ************************************************************************************** */
  78. 78. /* Wap using pointers to swap two integers. */ #include<iostream.h> #include<conio.h> void swap_using_pointers(int *, int *); void main() { clrscr(); int a,b; cout<<"nnEnter first integer : "; cin>>a; cout<<"nnEnter second integer : "; cin>>b; swap_using_pointers(&a,&b); cout<<"nnNow value of first integer = "<<a; cout<<"nnNow value of second integer = "<<b; getch(); } void swap_using_pointers(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; } /*****************************************OUTPUT********************************************* *** Enter first integer : 1 Enter second integer : 100 Now value of first integer = 100 Now value of second integer = 1 **************************************************************************** */
  79. 79. /* Wap using pointers to swap two integers. */ #include<iostream.h> #include<conio.h> void swap_using_pointers(int *, int *); void main() { clrscr(); int a,b; cout<<"nnEnter first integer : "; cin>>a; cout<<"nnEnter second integer : "; cin>>b; swap_using_pointers(&a,&b); cout<<"nnNow value of first integer = "<<a; cout<<"nnNow value of second integer = "<<b; getch(); } void swap_using_pointers(int *a,int *b) { int temp; *a=*b; *b=temp; temp=*a; } /*****************************************OUTPUT***************************** Enter first integer : 1 Enter second integer : 100 Now value of first integer = 100 Now value of second integer = 1 ******************************************************************** */
  80. 80. /* WAP to create a text file(.txt) and display number of words, alphabets, vowels and consonants and number of lowercase and uppercase letters using the concept of DATA FILE HANDLING? */ #include<fstream.h> #include<string.h> #include<ctype.h> #include<conio.h> void main() { clrscr(); char a[80]; int words=0, upper_letters=0, lower_letters=0, alpha=0, vowels=0,consonants=0; ofstream string("str.txt"); cout<<"nnEnter the string : "; cin.getline(a,79); string<<"The string is : "<<a<<"nn"; for(int i=0; i<strlen(a); i++) { if(a[i]==' ') while(a[i]==' ') { i++; } if(isalnum(a[i])) { while(a[i]!=' ') { i++; } words++; } } string<<"nnThe number of words in string are : "<<words; string<<"nnnAlphabets in string are : nn"; for(i=0; i<strlen(a); i++) { if(isalpha(a[i])) { alpha++; string<<a[i]<<" "; } } string<<"nnTotal number of alphabets in string => "<<alpha; string<<"nnnUppercase letters in string are : nn"; for(i=0; i<strlen(a); i++) { if(isupper(a[i])) { upper_letters++; string<<a[i]<<" ";
  81. 81. } } string<<"nnTotal number of uppercase letters in string => "<<upper_letters; string<<"nnnLowercase letters in string are : nn"; for(i=0; i<strlen(a); i++) { if(islower(a[i])) { lower_letters++; string<<a[i]<<" "; } } string<<"nnTotal number of Lowercase letters in string => "<<lower_letters; string<<"nnnVowels in string are : nn"; for(i=0; i<strlen(a); i++) { if(isalpha(a[i])) { if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U') { vowels++; string<<a[i]<<" "; } } } string<<"nnTotal number of vowels in string => "<<vowels; string<<"nnnConsonants in string are : nn"; for(i=0; i<strlen(a); i++) { if(isalpha(a[i])) { if(a[i]!='a'&&a[i]!='A'&&(a[i]!='e'&&a[i]!='E'&&a[i]!='i'&&a[i]!='I'&&(a[i]!='o'&&a[i]!='O')&&a[i]!='u'&&a[i]!='U') { consonants++; string<<a[i]<<" "; } } } string<<"nnTotal number of vowels in string => "<<consonants; getch(); } /*****************************OUTPUT************************************* Enter the string : Ayashkant Misha Output of created text file: The string is : Atashkant Mishra
  82. 82. The number of words in string are : 2 Alphabets in string are : A y a s h k a n t M i s h r a Total number of alphabets in string => 10 Uppercase letters in string are : A M Total number of uppercase letters in string => 2 y a s h k a n t i s h r a Lowercase letters in string are : Total number of Lowercase letters in string => 13 Vowels in string are : a i Total number of vowels in string => 2 Consonants in string are : y s h k n t r s h Total number of vowels in string => 8 ************************************************************************ */
  83. 83. /* WAP Using Multiple Inheritance For The Classes Student, Game And Person? */ #include<iostream.h> #include<stdio.h> #include<conio.h> class person{ char name[21]; int age; public: void indata() {cout<<"nnEnter the name of Student: " ; gets(name); cout<<"nnEnter the age : "; cin>>age; } void outdata(); }; void person::outdata() { cout<<"nn"; for(int i=0; i<79; i++) cout<<"-"; cout<<"nnName of the student is: "<<name; cout<<"nnAge of the student is : "<<age; } class game { char game_name[20]; public: void input() { cout<<"nnEnter the game name : "; cin.get();cin.getline(game_name,20); } void output() { cout<<"nnGame opted by the student is : "<<game_name; } }; class student: public person, public game { float Tmarks; int rollno; public: char calgrade() {if(Tmarks>90) return 'A'; else if(Tmarks>80&&Tmarks<=90) return 'B'; else if(Tmarks>70&&Tmarks<=80) return 'C'; else if(Tmarks>60&&Tmarks<=70)
  84. 84. return 'D'; else return 'E'; } void enter() { indata(); cout<<"nnEnter the roll number: "; cin>>rollno; input(); cout<<"nnEnter total marks (out of 100) : "; cin>>Tmarks; } void display() { outdata(); cout<<"nnRoll number : "<<rollno; output(); cout<<"nnTotal marks are : "<<Tmarks; cout<<"nnGrade = "<<calgrade(); } }; void main() { clrscr(); student A; A.enter(); cout<<”nnstudent details are : nn”; A.display(); getch(); } /*********************************************OUTPUT***************************************** ** Enter the name of student : Mahendra Enter the age : 17 Enter the roll number: 21 Enter the game name : Cricket Enter total marks (out of 100) : 99 Student Details are :
  85. 85. Name of the student is: Mahendra Age of the student is : 17 Roll number : 21 Game opted by the student is : Cricket Total marks are : 99 Grade = A ********************************************************************************************* *********************** */

×