├── .gitignore ├── .replit ├── programiz ├── readme.md ├── 1.hello_world.cpp ├── 3.add_two_number.cpp ├── 2.print_user_given_number.cpp ├── 11.sum_natural_numbers.cpp ├── 4.find_quotient_remindere.cpp ├── 6.swap_numbers.cpp ├── 7.even_odd.cpp ├── 5.size_of.cpp ├── 12.leap_year.cpp ├── 8.vowel_consonent.cpp ├── 13.factorial.cpp ├── 9.largest_among_three.cpp └── 10.roots_quadretic_equation.cpp ├── .vscode └── settings.json ├── char1.c ├── sf1.c ├── add.cpp ├── README.md ├── sum.c ├── dectobin.c ├── strlen.c ├── strcpy.c ├── pattern1n.c ├── ss age.c ├── external file read.c ├── pattern2.c ├── pattern3.c ├── pattern1.c ├── digit.c ├── 47.c ├── printNumWithoutLoop.cpp ├── no2_palindromeNumber.cpp ├── 63.c ├── concat using + overload.cpp ├── no3_primeNumber.cpp ├── ss.c ├── 57.c ├── armstrong.c ├── strcat.c ├── s1.c ├── f1.c ├── 65.c ├── struct student display.cpp ├── array1.cpp ├── no4_primeInRange.cpp ├── fact using func.c ├── no1_stringPalindrome.cpp ├── sf.c ├── sw22.cpp ├── digit 2.c ├── strcmp.c ├── sw18.cpp ├── bubble sort.c ├── swapNumberUsingFriend.cpp ├── patternDiamond.cpp ├── stringCompare.cpp ├── perfect number.c ├── no12_reverseElement.cpp ├── complex.cpp ├── no8_concatString.cpp ├── sw20.cpp ├── swap.c ├── array rev.cpp ├── operator overloding comp add.cpp ├── concat using + overload geeks.cpp ├── f2.c ├── sw15.cpp ├── concat using + overload geeks binary.cpp ├── sw16.cpp ├── no7_complexNumber.cpp ├── numberBasic.cpp ├── ipsita memucal.c ├── f_copy_f_noWhite.cpp ├── student stryct.c ├── sw17.cpp ├── bubble in file.c ├── sw19.cpp ├── quiz.c ├── struct student.cpp ├── matsum.c ├── menu cal.c ├── menucal.c ├── no5_areaOverloading.cpp ├── smmul.c ├── bin2grey.cpp ├── area.cpp ├── 7.stack_using_array.cpp ├── smtrans.c ├── cube.cpp ├── no6_cube.cpp ├── binaryOverloading.cpp ├── swapfinal.c ├── no9_runtimePolymorphism.cpp ├── 6.stack_using_linked_list.cpp ├── trisvs.cpp ├── triangle.cpp ├── matrix menu 2.cpp ├── subham.c ├── matrix menu.cpp ├── subham.cpp ├── no10_metriangle.cpp ├── soumyaneel-emp.cpp ├── sv_emp.cpp ├── 5.circular_linked_list.cpp ├── s_emp.cpp ├── 4.doubly_linked_list.cpp └── 3.linked_list.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.out -------------------------------------------------------------------------------- /.replit: -------------------------------------------------------------------------------- 1 | language = "cpp" 2 | run = "" -------------------------------------------------------------------------------- /programiz/readme.md: -------------------------------------------------------------------------------- 1 | ## https://www.programiz.com/cpp-programming/examples -------------------------------------------------------------------------------- /programiz/1.hello_world.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | int main(){ 3 | std::cout<<"hello world"; 4 | return 0; 5 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "Disabled", 3 | "files.associations": { 4 | "iostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /char1.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | void main() 3 | { 4 | char a[10]; 5 | int i=0; 6 | while(scanf("%[^\n]c",&a[i])!='\0') 7 | i++; 8 | puts(a); 9 | getch(); 10 | } 11 | -------------------------------------------------------------------------------- /sf1.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | void main() 3 | { 4 | char c; 5 | FILE *fp; 6 | fp=fopen("SOUMYANEEL","r"); 7 | while((c=getc(fp))!=EOF) 8 | printf("%c",c); 9 | } 10 | -------------------------------------------------------------------------------- /add.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream.h" 2 | int main() 3 | { 4 | int a,b; 5 | cout<<"enter two numbes"; 6 | cin>>a>>b; 7 | cout<<"result is equal to "< C CPP program. A beginner may need this code often. 4 | 5 | # Star this repo if you love it. 6 | 7 | # Love from A-Little-Hat 8 | -------------------------------------------------------------------------------- /programiz/3.add_two_number.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | int main(){ 4 | int a,b; 5 | cout<<"enter two number : "; 6 | cin>>a>>b; 7 | cout<<"\nresult is : "<>n; 7 | cout<<"\ngiven number is : "<>n; 7 | cout<<"\nresult is : "<50&&j<60) 11 | c++; 12 | } 13 | printf("\n %d thank you.",c); 14 | getch(); 15 | } 16 | -------------------------------------------------------------------------------- /ss age.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | void main() 4 | { 5 | int i,j,c=0; 6 | for(i=1;i<=10;i++) 7 | { 8 | printf("\n enter the age: "); 9 | scanf("%d",j); 10 | if(j>50&&j<60) 11 | c++; 12 | } 13 | printf("\n %d thank you.",c); 14 | getch(); 15 | } 16 | -------------------------------------------------------------------------------- /external file read.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | 3 | void main() 4 | { 5 | FILE *fp,*f; 6 | char c; 7 | int i=0; 8 | fp=fopen("sy.txt","rb"); 9 | while((c=fgetc(fp))!=EOF) 10 | { 11 | printf("%c",c); 12 | i++; 13 | } 14 | 15 | printf("%d",i); 16 | getch(); 17 | } 18 | -------------------------------------------------------------------------------- /pattern2.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | void main() 3 | { 4 | int i,j,n; 5 | printf("\n enter the value of n: "); 6 | scanf("%d",&n); 7 | for(i=1;i<=n;i++) 8 | { 9 | for(j=1;j<=n-i;j++) 10 | printf(" "); 11 | for(j=1;j<=i;j++) 12 | printf("%d",i); 13 | printf("\n"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /pattern3.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char c[100]; 5 | int i; 6 | printf("enter sentence :\n"); 7 | i=0; 8 | while(scanf("%[^\n]c",&c[i])!='\0') 9 | i++; 10 | printf("entered sentence\n"); 11 | i=0; 12 | while(c[i]!='\0') 13 | { 14 | printf("%c",c[i]); 15 | i++; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /programiz/4.find_quotient_remindere.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | int main(){ 4 | int a,b,q,r; 5 | cout<<"enter two number : "; 6 | cin>>a>>b; 7 | q=a>b?a/b:b/a; 8 | r=a>b?a-((a/b)*b):b-((b/a)*a); 9 | cout<<"\nquotient is : "<>a>>b; 7 | cout<<"before swapping a = "<>a; 13 | isEven(a)?cout<<"\n Even":cout<<"\n Odd"; 14 | return 0; 15 | } -------------------------------------------------------------------------------- /pattern1.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | void main() 4 | { 5 | int i,j; 6 | for(i=5;i>=0;i--) 7 | { 8 | for(j=1;j<=5;j++) 9 | { 10 | if(j<5-i) 11 | printf(" "); 12 | else 13 | printf("*"); 14 | 15 | } 16 | printf("\n"); 17 | } 18 | printf("\n thank you."); 19 | getch(); 20 | } 21 | -------------------------------------------------------------------------------- /digit.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"math.h" 3 | 4 | void main() 5 | { 6 | int n,d,l,f,i; 7 | printf("\n enter the number"); 8 | scanf("%d",&n); 9 | d=log10(n); 10 | i=pow(10,d); 11 | f=n/10; 12 | l=n%i; 13 | n=n-(f*i); 14 | n=n+(l*(i)); 15 | i=n%10; 16 | n=n-i+f; 17 | printf("\n number is %d ",n); 18 | getch(); 19 | } 20 | -------------------------------------------------------------------------------- /47.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100 3 | void main() 4 | { 5 | int a[MAX] 6 | printf("Enter the integers in the array"); 7 | for(i=0;i 2 | #include 3 | using namespace std; 4 | static int n=0; 5 | class num{ 6 | public:inline num(){ 7 | cout<<++n<<" "; 8 | } 9 | }; 10 | 11 | using namespace std; 12 | int main(){ 13 | int x; 14 | cout<<"enter the maximum number"; 15 | cin>>x; 16 | num obj[x]; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /no2_palindromeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout << "Enter a number:"; 6 | int n, temp = 0, N; 7 | cin >> n; 8 | 9 | for (N = n; N > 0; N /= 10) 10 | temp = (temp * 10) + (N % 10); 11 | 12 | if (temp == n) 13 | cout << "YES"; 14 | else 15 | cout << "NO"; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /63.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"math.h" 3 | #include"string.h" 4 | 5 | int sum(int n) 6 | { 7 | int k,s=0; 8 | while(n>0) 9 | { 10 | k=n%10; 11 | s+=k; 12 | n=n/10; 13 | } 14 | return(s); 15 | } 16 | void main() 17 | { 18 | int n,s; 19 | printf("\n enter a number"); 20 | scanf("%d",&n); 21 | s=sum(n); 22 | printf("\n sum = %d",s); 23 | getch(); 24 | } 25 | -------------------------------------------------------------------------------- /concat using + overload.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | #include"string.h" 3 | using namespace std; 4 | void operator+(char *a ;char *b){ 5 | strcat(a,b); 6 | return *a; 7 | } 8 | int main() 9 | { 10 | char a[100],b[100],c[200]; 11 | cout<<"enter a string : "; 12 | gets(a); 13 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Enter a number:"; 7 | int n, i, c = 0; 8 | cin >> n; 9 | for (i = 2; i < n; i++) 10 | { 11 | if (n % i == 0) 12 | c++; 13 | } 14 | 15 | if (c == 0) 16 | cout << "YES"; 17 | else 18 | cout << "NO"; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /programiz/5.size_of.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Size of char: " << sizeof(char) << " byte" << endl; 7 | cout << "Size of int: " << sizeof(int) << " bytes" << endl; 8 | cout << "Size of float: " << sizeof(float) << " bytes" << endl; 9 | cout << "Size of double: " << sizeof(double) << " bytes" << endl; 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /ss.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | #include"math.h" 4 | void main() 5 | { 6 | int i,n,k,sum=0; 7 | for(n=100;n<=999;n++) 8 | { 9 | i==n; 10 | while(n!=0) 11 | { 12 | k=0; 13 | k=n%10; 14 | sum=sum+pow(k,3); 15 | n=n/10; 16 | } 17 | if(i==sum) 18 | printf("\n %d is amstrong number.",i); 19 | } 20 | getch(); 21 | } 22 | -------------------------------------------------------------------------------- /57.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"math.h" 3 | #include"string.h" 4 | 5 | void main() 6 | { 7 | char s[100]; 8 | int i,n,k; 9 | printf("\n please enter the string "); 10 | gets(s); 11 | n=strlen(s); 12 | printf("\n enter the value of k..."); 13 | scanf("%d",&k); 14 | for(i=0;i>year; 16 | isLeap(year)?cout<<"\nLeap Year.":cout<<"\nNot a Leap Year."; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /armstrong.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | #include"math.h" 4 | void main() 5 | { 6 | int i,n,k,sum=0; 7 | for(n=100;n<=999;n++) 8 | { 9 | sum=0; 10 | i==n; 11 | while(n!=0) 12 | { 13 | k=0; 14 | k=n%10; 15 | sum=sum+pow(k,3); 16 | n=n/10; 17 | } 18 | if(i==sum) 19 | printf("\n %d is amstrong number.",i); 20 | } 21 | getch(); 22 | } 23 | -------------------------------------------------------------------------------- /strcat.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #define MAX 100 3 | void main() 4 | { 5 | char a[MAX],b[MAX],i=0,j; 6 | printf("\n enter the string : "); 7 | gets(a); 8 | printf("\n enter the second string : "); 9 | gets(b); 10 | for(i=0;a[i]!='\0';i++); 11 | { 12 | for(j=0;b[j]!='\0';j++) 13 | { 14 | a[i++]=b[j]; 15 | } 16 | } 17 | printf("\n contatination done....\n\n new string is : "); 18 | puts(a); 19 | getch(); 20 | } 21 | -------------------------------------------------------------------------------- /programiz/8.vowel_consonent.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | bool isVowel(char x){ 4 | char vowel[10]={'a','e','i','o','u','A','E','I','O','U'}; 5 | for(int i=0;i<10;i++){ 6 | if(x == vowel[i]) 7 | return true; 8 | } 9 | return false; 10 | } 11 | int main(){ 12 | char a; 13 | cout<<"enter a character : "; 14 | cin>>a; 15 | isVowel(a)?cout<<"\n vowel":cout<<"\n consonent"; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /s1.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | void main() 4 | { 5 | int k,n,m,sum=0; 6 | printf("\n enter a integer."); 7 | scanf("%d", &n); 8 | m=n; 9 | if(n==1) 10 | printf("\n binary is 1."); 11 | else 12 | { 13 | while(m!=0) 14 | { 15 | k=0; 16 | k=m%2; 17 | sum=k*10+sum; 18 | m=m/2; 19 | } 20 | printf("\n binary is %d.",sum); 21 | } 22 | getch(); 23 | } 24 | -------------------------------------------------------------------------------- /programiz/13.factorial.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | int factorial(int x){ 4 | if(x>1){ 5 | return x * factorial(x-1); 6 | } 7 | else if(x==0){ 8 | return 1; 9 | } 10 | } 11 | int main(){ 12 | int n; 13 | cout<<"enter the number of n : "; 14 | cin>>n; 15 | if(n<0){ 16 | cout<<"\ninvalid input."; 17 | exit(0); 18 | }else{ 19 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | float n1, n2, n3; 6 | 7 | cout << "Enter three numbers: "; 8 | cin >> n1 >> n2 >> n3; 9 | 10 | if(n1 >= n2 && n1 >= n3) 11 | cout << "Largest number: " << n1; 12 | 13 | if(n2 >= n1 && n2 >= n3) 14 | cout << "Largest number: " << n2; 15 | 16 | if(n3 >= n1 && n3 >= n2) 17 | cout << "Largest number: " << n3; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /65.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"math.h" 3 | #include"string.h" 4 | 5 | void main() 6 | { 7 | char a[100],b[100],c[100]; 8 | int x,y,z; 9 | printf("\n enter the first "); 10 | gets(a); 11 | printf("\n enter the second"); 12 | gets(b); 13 | printf("\n enter the third"); 14 | gets(c); 15 | x=strlen(a); 16 | y=strlen(b); 17 | z=strlen(c); 18 | if(x 4 | using namespace std; 5 | int main() { 6 | int arr[100],even=0,odd=0; 7 | bool toggle=true; 8 | cout << "enter numbers\n"; 9 | for(int i=0;i<100;i++){ 10 | cin>>arr[i]; 11 | if(toggle){ 12 | even+=arr[i]; 13 | toggle=false; 14 | }else{ 15 | odd+=arr[i]; 16 | toggle=true; 17 | } 18 | } 19 | if(even>odd){ 20 | cout<<"\nsum of even is greater."; 21 | }else{ 22 | cout<<"\nsum of odd is greater."; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /no4_primeInRange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "Enter a range:"; 7 | int i, a, b, c; 8 | cin >> a >> b; 9 | while (a <= b) 10 | { 11 | if (a != 1) 12 | { 13 | c = 0; 14 | for (i = 2; i < a; i++) 15 | { 16 | if (a % i == 0) 17 | c++; 18 | } 19 | if (c == 0) 20 | cout << a << ","; 21 | } 22 | a++; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /fact using func.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | int fact(int f); 3 | void main() 4 | { 5 | int n,f; 6 | printf("\n enter the value of n"); 7 | scanf("%d",&n); 8 | switch (n) 9 | { 10 | case 0: 11 | { 12 | printf(" result = 1"); 13 | exit(0); 14 | } 15 | case 1: 16 | { 17 | printf(" result = 1"); 18 | exit(0); 19 | } 20 | } 21 | f=fact(n); 22 | printf("%d ",f); 23 | getch(); 24 | } 25 | 26 | int fact(int n) 27 | { 28 | int i,f=1; 29 | for(i=2;i<=n;i++) 30 | f=f*i; 31 | return (f); 32 | } 33 | -------------------------------------------------------------------------------- /no1_stringPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout << "Enter a string:"; 8 | char str[1000]; 9 | cin >> str; 10 | int i = 0, j = strlen(str) - 1, flag = 0; 11 | 12 | while (i < j) 13 | { 14 | if (str[i] != str[j]) 15 | { 16 | flag = 1; 17 | break; 18 | } 19 | i++; 20 | j--; 21 | } 22 | 23 | if (flag == 0) 24 | cout << "YES"; 25 | else 26 | cout << "NO"; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /sf.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | #include"stdlib.h" 4 | void main() 5 | { 6 | FILE *f1,*f2; 7 | char c; 8 | f1=fopen("file.txt","w"); 9 | while((c=getchar())!=EOF) 10 | { 11 | if(c=='1') 12 | break; 13 | putc(c,f1); 14 | } 15 | fclose(f1); 16 | f1=fopen("file.txt","r"); 17 | f2=fopen("copy.txt","w"); 18 | while((c=getc(f1))!=EOF) 19 | putc(c,f2); 20 | fclose(f2); 21 | f2=fopen("copy.txt","r"); 22 | while((c=getc(f2))!=EOF) 23 | printf("%c",c); 24 | fclose(f1); 25 | fclose(f2); 26 | getch(); 27 | } 28 | -------------------------------------------------------------------------------- /sw22.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | using namespace std; 3 | template 4 | T mymin(T a, T b) 5 | { 6 | return (a < b) ? a : b; 7 | } 8 | int main() 9 | { 10 | int a = 10, b = 20; 11 | cout << endl<<"INTEGER"< array[d+1]) /* For decreasing order use < */ 14 | { 15 | swap = array[d]; 16 | array[d] = array[d+1]; 17 | array[d+1] = swap; 18 | } 19 | } 20 | } 21 | printf("Sorted list in ascending order:\n"); 22 | for (c = 0; c < n; c++) 23 | printf("%d\n", array[c]); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /swapNumberUsingFriend.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class num{ 4 | private:int number; 5 | public:inline num(){ 6 | cout<>number; 8 | } 9 | inline void display(){ 10 | cout< 0; i--) 19 | { 20 | for (int j = 0; j < space; j++) 21 | cout << " "; 22 | for (int j = 0;j < i;j++) 23 | cout << c++<<" "; 24 | cout << endl; 25 | space++; 26 | } 27 | } 28 | int main() 29 | { 30 | int n; 31 | cout<<"enter the no of highest row : "; 32 | cin>>n; 33 | printPattern(n); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /stringCompare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class str{ 6 | private:char s[100]; 7 | public:inline str(){ 8 | cout< 2 | 3 | int main() 4 | { 5 | int i,c=0, Number, sum = 0 , j ,arr[10]; 6 | for(i=0;i<10;i++) 7 | { 8 | printf("\n enter a number"); 9 | scanf("%d",&arr[i]); 10 | } 11 | for(j=0;j<10;j++) 12 | { 13 | sum=0; 14 | Number=arr[j] ; 15 | 16 | for(i = 1 ; i < Number ; i++) 17 | { 18 | if(Number % i == 0) 19 | sum = sum + i ; 20 | } 21 | 22 | if (sum == Number) 23 | { 24 | printf("\n %d is a Perfect Number", Number) ; 25 | c++; 26 | } 27 | else 28 | printf("\n%d is not the Perfect Number", Number) ; 29 | } 30 | printf("\n total number of perfect number is : %d",c); 31 | 32 | return 0 ; 33 | } 34 | -------------------------------------------------------------------------------- /no12_reverseElement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n; 4 | void reverseArray(int *arr) 5 | { 6 | int *x = arr, *y = arr + n - 1, *temp; 7 | while (x < y) 8 | { 9 | *temp = *x; 10 | *x = *y; 11 | *y = *temp; 12 | x++; 13 | y--; 14 | } 15 | cout << "Reversed elements are: "; 16 | for (int i = 0; i < n; i++) 17 | cout << arr[i] << ","; 18 | } 19 | int main() 20 | { 21 | int i; 22 | cout << "enter the number of element :"; 23 | cin >> n; 24 | int arr[n]; 25 | cout << "Enter the elements: "; 26 | for (i = 0; i < n; i++) 27 | cin >> arr[i]; 28 | reverseArray(arr); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /complex.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | 4 | class complex 5 | { 6 | private: int real,img; 7 | public: inline void display(void) 8 | { 9 | cout<>real; 21 | cout<<"enter the complex part"; 22 | cin>>img; 23 | } 24 | 25 | }; 26 | int main() 27 | { 28 | complex a,b,c; 29 | a.input(); 30 | b.input(); 31 | a.display(); 32 | b.display(); 33 | add(a,b); 34 | c.display(); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /no8_concatString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Concat 6 | { 7 | private: string str; 8 | public: 9 | void input(void); 10 | Concat operator+(Concat); 11 | void output(void); 12 | }; 13 | 14 | void Concat::input(void) 15 | { 16 | cout << "Enter a string: "; 17 | cin >> str; 18 | } 19 | 20 | Concat Concat::operator+(Concat obj1) 21 | { 22 | Concat temp; 23 | temp.str = str + obj1.str; 24 | return temp; 25 | } 26 | 27 | void Concat::output(void) 28 | { 29 | cout << "New String: " << str; 30 | } 31 | 32 | int main() 33 | { 34 | Concat obj1, obj2, obj3; 35 | obj1.input(); 36 | obj2.input(); 37 | obj3 = obj1 + obj2; 38 | obj3.output(); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /sw20.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | using namespace std; 3 | class base 4 | { 5 | public: 6 | void display() 7 | { 8 | cout << "\n display base"; 9 | } 10 | virtual void show() 11 | { 12 | cout << "\n virtual base class"; 13 | } 14 | }; 15 | class derived : public base 16 | { 17 | public: 18 | void display() { cout << "\n display derived"; } 19 | void show() { cout << "\n show derived"; } 20 | }; 21 | int main() 22 | { 23 | base b; 24 | derived d; 25 | base *bptr; 26 | cout << "\n bptr points to base\n"; 27 | bptr = &b; 28 | bptr->display(); 29 | bptr->show(); 30 | cout << "\n\n bptr points derived\n"; 31 | bptr = &d; 32 | bptr->display(); 33 | bptr->show(); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /swap.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | void swapvar(int *a,int *b); 4 | void swap(int a,int b); 5 | void main(void) 6 | { 7 | int a,b; 8 | printf("\n enter two number : "); 9 | scanf("%d%d",&a,&b); 10 | printf("\n before swapping two numbers are:\n a=%d b=%d\n",a,b); 11 | swap(a,b); 12 | swapvar(&a,&b); 13 | getch(); 14 | } 15 | void swap(int a,int b) 16 | { 17 | a=a+b; 18 | b=a-b; 19 | a=a-b; 20 | printf("\n swap without using third variable\na=%d b=%d\npress any key to continue",a,b); 21 | getch(); 22 | } 23 | void swapvar(int *a,int *b) 24 | { 25 | int temp; 26 | temp=*a; 27 | *a=*b; 28 | *b=temp; 29 | printf("\n swap using third variable\na=%d b=%d\npress any key to continue",*a,*b); 30 | getch(); 31 | } 32 | -------------------------------------------------------------------------------- /array rev.cpp: -------------------------------------------------------------------------------- 1 | // Write a function that reverses the elements of an array in place. The function must accept only one pointer value and return void. 2 | #include"iostream" 3 | using namespace std; 4 | #define MAX 100 5 | int n; 6 | void rev(int *a){ 7 | int l=n-1; 8 | int i=0; 9 | while(i>n; 22 | cout<<"enter the element of the array : "; 23 | for(i=0;i>a[i]; 25 | rev(a); 26 | cout<<"reverse done...your array is: "<>x>>y; 26 | a.input(x,y); 27 | cout<>x>>y; 29 | b.input(x,y); 30 | c=a+b; 31 | c.display(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /concat using + overload geeks.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ Program to concatenate two string 3 | // using unary operator overloading 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | class AddString { 9 | public: 10 | char s1[25], s2[25]; 11 | AddString(char str1[], char str2[]) 12 | { 13 | strcpy(this->s1, str1); 14 | strcpy(this->s2, str2); 15 | } 16 | void operator+() 17 | { 18 | cout << "\nConcatenation: " << strcat(s1, s2); 19 | } 20 | }; 21 | int main() 22 | { 23 | char str1[50] , str2[50]; 24 | cout<<"enter a string : "; 25 | gets(str1); 26 | cout<str, str); 12 | } 13 | AddString operator+(AddString& S2) 14 | { 15 | AddString S3; 16 | strcat(this->str, S2.str); 17 | strcpy(S3.str, this->str); 18 | return S3; 19 | } 20 | }; 21 | int main() 22 | { 23 | char str1[100],str2[100]; 24 | cout<<"enter a string : "; 25 | gets(str1); 26 | cout<> a >> b; 27 | } 28 | int B::get_a() { return a; } 29 | void B::show_a() { cout << "a=" << a << endl; } 30 | void D::mul() 31 | { 32 | get_ab(); 33 | c = b * get_a(); 34 | } 35 | void D::display() 36 | { 37 | show_a(); 38 | cout 39 | << "b= " << b << endl 40 | << "c=a*b\nvalue of c=" << c << endl; 41 | } 42 | int main() 43 | { 44 | D d; 45 | d.mul(); 46 | d.display(); 47 | d.mul(); 48 | d.display(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /no7_complexNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Complex 5 | { 6 | private: 7 | int real, img; 8 | 9 | public: 10 | Complex() {} 11 | 12 | Complex(int r, int i) 13 | { 14 | real = r; 15 | img = i; 16 | } 17 | Complex operator+(Complex obj) 18 | { 19 | Complex temp; 20 | temp.real = real + obj.real; 21 | temp.img = img + obj.img; 22 | return temp; 23 | } 24 | 25 | void output(void) 26 | { 27 | cout << "Sum: " << real << " + " << img << "i"; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | Complex obj1, obj2, obj3; 34 | int a, b; 35 | cout << "Enter the real and imaginary value of first complex number: "; 36 | cin >> a >> b; 37 | obj1 = Complex(a, b); 38 | cout << "Enter the real and imaginary value of second complex number: "; 39 | cin >> a >> b; 40 | obj2 = Complex(a, b); 41 | obj3 = obj1 + obj2; 42 | obj3.output(); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /numberBasic.cpp: -------------------------------------------------------------------------------- 1 | // Question no 4 2 | 3 | #include 4 | using namespace std; 5 | int main() { 6 | int n,i,count=0; 7 | bool isPrime = true; 8 | cout << "Enter an integer: "; 9 | cin >> n; 10 | if ( n % 2 == 0) 11 | cout << n << " is even.\n"; 12 | else 13 | cout << n << " is odd.\n"; 14 | if (n == 0 || n == 1) { 15 | isPrime = false; 16 | } 17 | else { 18 | for (i = 2; i <= n / 2; ++i) { 19 | if (n % i == 0) { 20 | isPrime = false; 21 | break; 22 | } 23 | } 24 | } 25 | if (isPrime) 26 | cout << n << " is a prime number\n"; 27 | else{ 28 | cout << n << " is composite number\n"; 29 | cout << "Factors of " << n << " are: "; 30 | for(i = 1; i <= n; ++i) { 31 | if(n % i == 0){ 32 | cout << i << " "; 33 | count++; 34 | } 35 | } 36 | cout<<"\ntotal factors : "< 2 | #include 3 | #include 4 | void main() 5 | { 6 | float c; 7 | int a,b,n,m ; 8 | printf("****calculator****\n"); 9 | printf("enter your choice\n"); 10 | printf("1)Addition\n 2)Subtraction\n3)Multiplication\n4)Division\n5)Exit\n"); 11 | scanf("%d",&n); 12 | switch (m) 13 | { 14 | case 1: 15 | { 16 | printf("enter two num:"); 17 | scanf("%d%d",&a,&b); 18 | c=a+b; 19 | printf("the result is :%f",c); 20 | } 21 | case 2: 22 | { 23 | 24 | printf("enter two num:"); 25 | scanf("%d%d",&a,&b); 26 | c=a-b; 27 | printf("the result is :%f",c); 28 | break; 29 | } 30 | case 3: 31 | { 32 | printf("enter two num:"); 33 | scanf("%d%d",&a,&b); 34 | c=a*b; 35 | printf("the result is :%f",c); 36 | break; 37 | } 38 | case 4: 39 | { 40 | printf("enter two num:"); 41 | scanf("%d%d",&a,&b); 42 | c=a+b; 43 | printf("the result is :%f",c); 44 | break; 45 | } 46 | default: 47 | { 48 | printf("the elementa are wrong"); 49 | } 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /f_copy_f_noWhite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | ofstream out; 9 | ifstream in; 10 | char fname1[10],fname2[10]; 11 | char ch; 12 | cout<<"enter the file name to be copied.?"<>fname1; 14 | if(in.fail()){ 15 | cerr<<"no such a file exists"<>fname2; 23 | in.open(fname1); 24 | out.open(fname2); 25 | if(out.fail()){ 26 | cerr << "unable to create a new file"< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | float a, b, c, x1, x2, discriminant, realPart, imaginaryPart; 8 | cout << "Enter coefficients a, b and c: "; 9 | cin >> a >> b >> c; 10 | discriminant = b*b - 4*a*c; 11 | 12 | if (discriminant > 0) { 13 | x1 = (-b + sqrt(discriminant)) / (2*a); 14 | x2 = (-b - sqrt(discriminant)) / (2*a); 15 | cout << "Roots are real and different." << endl; 16 | cout << "x1 = " << x1 << endl; 17 | cout << "x2 = " << x2 << endl; 18 | } 19 | 20 | else if (discriminant == 0) { 21 | cout << "Roots are real and same." << endl; 22 | x1 = -b/(2*a); 23 | cout << "x1 = x2 =" << x1 << endl; 24 | } 25 | 26 | else { 27 | realPart = -b/(2*a); 28 | imaginaryPart =sqrt(-discriminant)/(2*a); 29 | cout << "Roots are complex and different." << endl; 30 | cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl; 31 | cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl; 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /sw17.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | using namespace std; 3 | class student 4 | { 5 | private: 6 | int roll; 7 | 8 | public: 9 | void get_num(int); 10 | void put_num(); 11 | }; 12 | void student ::get_num(int a) 13 | { 14 | roll = a; 15 | } 16 | void student::put_num() 17 | { 18 | cout << "roll number=" << roll << endl; 19 | } 20 | class test : public student 21 | { 22 | protected: 23 | float sub1, sub2; 24 | 25 | public: 26 | void get_marks(float, float); 27 | void put_marks(); 28 | }; 29 | void test::get_marks(float x, float y) 30 | { 31 | sub1 = x; 32 | sub2 = y; 33 | } 34 | void test::put_marks() 35 | { 36 | cout << "marks in sub1: " << sub1 << endl; 37 | cout << "marks in sub2: " << sub2 << endl; 38 | } 39 | class result : public test 40 | { 41 | float total; 42 | 43 | public: 44 | void display(); 45 | }; 46 | void result::display() 47 | { 48 | total = sub1 + sub2; 49 | put_num(); 50 | put_marks(); 51 | cout << "total= " << total << endl; 52 | } 53 | int main() 54 | { 55 | result student; 56 | student.get_num(111); 57 | student.get_marks(75.0, 59.5); 58 | student.display(); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /bubble in file.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | 3 | void main() 4 | { 5 | FILE *fp; 6 | char c; 7 | char a[100]; 8 | int i,n, d, swap; 9 | 10 | fp=fopen("bubble.txt","w"); 11 | printf("enter numbers \n"); 12 | while((c=getchar())!=EOF) 13 | { 14 | if(c=='1') 15 | break; 16 | fputc(c,fp); 17 | } 18 | 19 | fclose(fp); 20 | 21 | fp=fopen("bubble.txt","r"); 22 | 23 | i=0; 24 | while((c=fgetc(fp))!=EOF) 25 | { 26 | a[i]=c; 27 | i++; 28 | } 29 | 30 | fclose(fp); 31 | 32 | n=i; 33 | 34 | for (c = 0 ; c < n - 1; c++) 35 | { 36 | for (d = 0 ; d < n - c - 1; d++) 37 | { 38 | if (a[d] > a[d+1]) 39 | { 40 | swap = a[d]; 41 | a[d] = a[d+1]; 42 | a[d+1] = swap; 43 | } 44 | } 45 | } 46 | printf("\nsort completed.....\n"); 47 | getch(); 48 | 49 | fp=fopen("bubble.txt","w"); 50 | 51 | i=0; 52 | while(i<=n&&(c=a[i])!=EOF) 53 | { 54 | fputc(c,fp) ; 55 | i++; 56 | } 57 | 58 | fclose(fp); 59 | system("cls"); 60 | 61 | printf("sorted data is :\n"); 62 | fp=fopen("bubble.txt","r"); 63 | 64 | while((c=fgetc(fp))!=EOF) 65 | printf("%c ",c); 66 | 67 | getch(); 68 | } 69 | -------------------------------------------------------------------------------- /sw19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class student 5 | { 6 | protected: 7 | int roll_number; 8 | 9 | public: 10 | void get_number(int a) 11 | { 12 | roll_number = a; 13 | } 14 | void put_number(void) 15 | { 16 | cout << "Roll No." << roll_number << "\n"; 17 | } 18 | }; 19 | class test : public student 20 | { 21 | protected: 22 | float part1, part2; 23 | 24 | public: 25 | void get_marks(float x, float y) 26 | { 27 | part1 = x; 28 | part2 = y; 29 | } 30 | void put_marks(void) 31 | { 32 | cout << "Marks obtained"<>n; 18 | fp=fopen("struct.txt","w"); 19 | if(fp==NULL) 20 | { 21 | printf("\n unable to create file file"); 22 | exit(0); 23 | } 24 | for(int i=0;i>(s.name); 27 | cout<>s.year; 29 | cout<>s.cls; 31 | cout<>s.roll; 33 | cout<>s.total; 35 | fwrite(&s,sizeof(s),1,fp); 36 | system("cls"); 37 | } 38 | fclose(fp); 39 | cout< 2 | #include"stdio.h" 3 | #include"conio.h" 4 | int main() 5 | { 6 | int r1,r2, c1,c2, a[100][100], b[100][100], sum[100][100], i, j; 7 | 8 | printf("Enter number of rows and columns for first matrix (between 1 and 100): "); 9 | scanf("%d%d", &r1,&c1); 10 | printf("Enter number of rows and columns for second matrix (between 1 and 100): "); 11 | scanf("%d%d", &r2,&c2); 12 | if(r1!=r2||c1!=c2) 13 | { 14 | printf("\n wrong input...compatibility error...press any key to try again..."); 15 | getch(); 16 | system("cls"); 17 | main(); 18 | exit (0); 19 | } 20 | else 21 | { 22 | printf("\nEnter elements of 1st matrix:\n"); 23 | 24 | for(i=0; i 2 | #include "math.h" 3 | using namespace std; 4 | 5 | class Area 6 | { 7 | private: 8 | int choice, len, bdth; 9 | double calc, r, l, b, h, g; 10 | 11 | public: 12 | void input(void); 13 | void area(double); 14 | void area(int, int); 15 | void area(double, double, double); 16 | }; 17 | 18 | void Area::input(void) 19 | { 20 | cout << "1. Area of circle\n"; 21 | cout << "2. Area of rectangle\n"; 22 | cout << "3. Area of triangle\n"; 23 | cout << "4. Exit\n"; 24 | cout << "Enter your choice...\n>"; 25 | cin >> choice; 26 | 27 | switch (choice) 28 | { 29 | case 1: 30 | cout << "Enter the radius: "; 31 | cin >> r; 32 | area(r); 33 | input(); 34 | case 2: 35 | cout << "Enter the length and breadth: "; 36 | cin >> len >> bdth; 37 | area(len, bdth); 38 | input(); 39 | case 3: 40 | cout << "Enter the values of the sides: "; 41 | cin >> b >> h >> g; 42 | area(b, h, g); 43 | input(); 44 | case 4: 45 | exit(0); 46 | default: 47 | cout << "Invalid choice!\n"; 48 | input(); 49 | } 50 | } 51 | 52 | void Area::area(double rad) 53 | { 54 | cout << "Area of the circle: " << (3.14 * rad * rad) << " Sq. Cm.\n"; 55 | } 56 | 57 | void Area::area(int ln, int bd) 58 | { 59 | cout << "Area of the rectangle: " << (ln * bd) << " Sq. Cm.\n"; 60 | } 61 | 62 | void Area::area(double bs, double ht, double g) 63 | { 64 | double s = bs + ht + g; 65 | cout << "Area of the triangle: " << pow((s * (s - bs) * (s - ht) * (s - g)), .5) << " Sq. Cm.\n"; 66 | } 67 | 68 | int main() 69 | { 70 | Area obj; 71 | obj.input(); 72 | return 0; 73 | } -------------------------------------------------------------------------------- /smmul.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | #include"stdlib.h" 4 | #define MAX 10 5 | void main() 6 | { 7 | int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],r1,r2,c1,c2,i,j,k,sum; 8 | printf("\n enter the row and column of first matrix : "); 9 | scanf("%d%d",&r1,&c1); 10 | printf("\n enter the row and column of second matrix : "); 11 | scanf("%d%d",&r2,&c2); 12 | if(c1!=r2) 13 | { 14 | printf("\n wrong input...compatibility error...press any key to try again..."); 15 | getch(); 16 | system("cls"); 17 | main(); 18 | exit (0); 19 | } 20 | else 21 | { 22 | printf("\n enter value for first matrix."); 23 | for(i=0;i 4 | using namespace std; 5 | 6 | // Helper function to xor two characters 7 | char xor_c(char a, char b) { return (a == b) ? '0' : '1'; } 8 | 9 | // Helper function to flip the bit 10 | char flip(char c) { return (c == '0') ? '1' : '0'; } 11 | 12 | // function to convert binary string 13 | // to gray string 14 | string binarytoGray(string binary) 15 | { 16 | string gray = ""; 17 | 18 | // MSB of gray code is same as binary code 19 | gray += binary[0]; 20 | 21 | // Compute remaining bits, next bit is comuted by 22 | // doing XOR of previous and current in Binary 23 | for (int i = 1; i < binary.length(); i++) { 24 | // Concatenate XOR of previous bit 25 | // with current bit 26 | gray += xor_c(binary[i - 1], binary[i]); 27 | } 28 | 29 | return gray; 30 | } 31 | 32 | // function to convert gray code string 33 | // to binary string 34 | string graytoBinary(string gray) 35 | { 36 | string binary = ""; 37 | 38 | // MSB of binary code is same as gray code 39 | binary += gray[0]; 40 | 41 | // Compute remaining bits 42 | for (int i = 1; i < gray.length(); i++) { 43 | // If current bit is 0, concatenate 44 | // previous bit 45 | if (gray[i] == '0') 46 | binary += binary[i - 1]; 47 | 48 | // Else, concatenate invert of 49 | // previous bit 50 | else 51 | binary += flip(binary[i - 1]); 52 | } 53 | 54 | return binary; 55 | } 56 | 57 | // Driver program to test above functions 58 | int main() 59 | { 60 | string binary = "11001011"; 61 | cout << "Gray code of " << binary << " is " 62 | << binarytoGray(binary) << endl; 63 | 64 | string gray = "01101"; 65 | cout << "Binary code of " << gray << " is " 66 | << graytoBinary(gray) << endl; 67 | return 0; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define PI 3.14 6 | void area(float r) 7 | { 8 | cout<>c; 25 | switch(c) 26 | { 27 | case 1: 28 | { 29 | 30 | float r; 31 | cout<<"enter the value of the radious in cm : "; 32 | cin>>r; 33 | area(r); 34 | break; 35 | } 36 | case 2: 37 | { 38 | float a,b,c; 39 | cout << "enter the value of three sides of the triangle in cm : "; 40 | cin >> a >> b >> c; 41 | area(a, b, c); 42 | break; 43 | } 44 | case 3: 45 | { 46 | float a,b; 47 | cout << "enter the value of two sides of the ractangle in cm : "; 48 | cin>>a>>b; 49 | area(a,b); 50 | break; 51 | } 52 | default : 53 | { 54 | cout<<"wrong input please try again.....\n enter any key to continue...."; 55 | system("cls"); 56 | main(); 57 | } 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /7.stack_using_array.cpp: -------------------------------------------------------------------------------- 1 | //stack operations using array : 2 | /*---------------------------------------------------------------------------------------------------------------------*/ 3 | #include 4 | using namespace std; 5 | #define MAX 10 6 | 7 | template 8 | class stack { 9 | public: 10 | T item; 11 | int top; 12 | T arr[MAX]; 13 | stack() 14 | { 15 | // for (int i = 0; i < MAX; i++) { 16 | // arr[i] = NULL; 17 | // } 18 | top = -1; 19 | } 20 | void push(T a) 21 | { 22 | top++; 23 | if (top < MAX) { 24 | arr[top] = a; 25 | } 26 | else 27 | { 28 | cout<<"STACK IS FULL"; 29 | top--; 30 | } 31 | } 32 | void pop() 33 | { 34 | if (top == -1) { 35 | cout<<"STACK IS EMPTY\n"; 36 | // return NULL; 37 | } 38 | else { 39 | // T data = arr[top]; 40 | cout << "Item popped from Stack is:" << arr[top] << endl; 41 | // arr[top] = NULL; 42 | top--; 43 | // return data; 44 | } 45 | } 46 | void display() 47 | { 48 | for(int i=0;i<=top;i++){ 49 | cout< a; 56 | int opt = 1; 57 | while (opt != 4) { 58 | cout << "\n MAX STACK CAPACITY=" << ((MAX - a.top) - 1) << "\n\n\n\n"; 59 | cout << "1) Push Item\n"; 60 | cout << "2) Pop Item\n"; 61 | cout << "3) Display\n"; 62 | cout << "4) Exit\n\n"; 63 | cout << "Option?"; 64 | cin >> opt; 65 | switch (opt) { 66 | case 1: 67 | cout << "Which Number should be pushed?"; 68 | cin >> a.item; 69 | a.push(a.item); 70 | break; 71 | case 2: 72 | a.pop(); 73 | break; 74 | case 3: 75 | a.display(); 76 | break; 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /smtrans.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; 6 | 7 | printf("Enter rows and column for first matrix: "); 8 | scanf("%d %d", &r1, &c1); 9 | 10 | printf("Enter rows and column for second matrix: "); 11 | scanf("%d %d",&r2, &c2); 12 | 13 | // Column of first matrix should be equal to column of second matrix and 14 | while (c1 != r2) 15 | { 16 | printf("Error! column of first matrix not equal to row of second.\n\n"); 17 | printf("Enter rows and column for first matrix: "); 18 | scanf("%d %d", &r1, &c1); 19 | printf("Enter rows and column for second matrix: "); 20 | scanf("%d %d",&r2, &c2); 21 | } 22 | 23 | // Storing elements of first matrix. 24 | printf("\nEnter elements of matrix 1:\n"); 25 | for(i=0; i>l>>b>>h; 10 | } 11 | inline float sarea(){ 12 | this->x=2*(l*b+b*h+h*l); 13 | return x; 14 | } 15 | inline float vol(){ 16 | this->v=(l*b*h); 17 | return v; 18 | } 19 | friend operator==(box &a,box &b){ 20 | if(a.vol()==b.vol()){ 21 | cout<v++; 37 | return v; 38 | } 39 | float operator--(){ 40 | this->x--; 41 | return x; 42 | } 43 | }; 44 | int main() 45 | { 46 | int f; 47 | do{ 48 | int c; 49 | cout<<"menu\n1.Calculate surface area\n2. Calculate volume\n3. Overload ++ operator to increment volume \n4. Overload - - operator to decrement surface area\n5. Overload == operator to check for eqality of two boxes using friend function. \n6. Check if it is a cube or cuboid"<>c; 51 | switch(c){ 52 | case 1: { 53 | box a; 54 | a.sarea(); 55 | break; 56 | } 57 | case 2:{ 58 | box a; 59 | float v=a.vol(); 60 | cout<<"volume is "<>f; 100 | system("cls"); 101 | }while(f!=0); 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /no6_cube.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | #include"math.h" 3 | using namespace std; 4 | class box{ 5 | private: float l,b,h,v,x; 6 | public: box(){ 7 | cout<<"enter the length breadth and height of the box in cm : "; 8 | cin>>l>>b>>h; 9 | } 10 | inline float sarea(){ 11 | x=2*(l*b+b*h+h*l); 12 | return x; 13 | } 14 | inline float vol(){ 15 | v=(l*b*h); 16 | return v; 17 | } 18 | friend bool operator==(box &a,box &b){ 19 | if(a.vol()==b.vol()){ 20 | return true; 21 | } 22 | else{ 23 | return false; 24 | } 25 | } 26 | void iscube(){ 27 | if(l*l*l==l*b*h){ 28 | cout<>c; 50 | switch(c){ 51 | case 1: { 52 | box a; 53 | cout<<"surface are is "<>f; 103 | }while(f!=0); 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /binaryOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class num{ 4 | private:float number; 5 | public:inline num(){ 6 | cout<>number; 8 | } 9 | num(float a){ 10 | number=a; 11 | } 12 | inline void display(){ 13 | cout<>n; 49 | return n; 50 | } 51 | int main(){ 52 | int c; 53 | do{ 54 | c=menu(); 55 | switch(c){ 56 | case 1:{ 57 | num a,b; 58 | num r=a+b; 59 | r.display(); 60 | break; 61 | } 62 | case 2:{ 63 | num a,b; 64 | num r=a-b; 65 | r.display(); 66 | break; 67 | } 68 | case 3:{ 69 | num a,b; 70 | num r=a*b; 71 | r.display(); 72 | break; 73 | } 74 | case 4:{ 75 | num a,b; 76 | num r=a/b; 77 | r.display(); 78 | break; 79 | } 80 | case 5:{ 81 | num a,b; 82 | if(a==b){ 83 | cout<>c; 97 | }while(c!=0); 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /swapfinal.c: -------------------------------------------------------------------------------- 1 | #include"stdio.h" 2 | #include"conio.h" 3 | void swapvari(int *a,int *b); 4 | void swapi(int a,int b); 5 | void swapvarf(float *a,float *b); 6 | void swapf(float a,float b); 7 | void swapvarc(char *a,char *b); 8 | void swapc(char a,char b); 9 | int main(void) 10 | { 11 | int a,b,c; 12 | float d,e; 13 | char f,g; 14 | do 15 | { 16 | printf("\n value swap\n1 integer\n2 float\n3character\n choose your choice :"); 17 | scanf("%d",&c); 18 | switch (c) 19 | { 20 | case 1: 21 | { 22 | printf("\n enter two number : "); 23 | scanf("%d%d",&a,&b); 24 | printf("\n before swapping two numbers are:\n a=%d b=%d\n",a,b); 25 | swapi(a,b); 26 | swapvari(&a,&b); 27 | break; 28 | } 29 | case 2: 30 | { 31 | printf("\n enter two number : "); 32 | scanf("%f%f",&d,&e); 33 | printf("\n before swapping two numbers are:\n a=%f b=%f\n",d,e); 34 | swapf(d,e); 35 | swapvarf(&d,&e); 36 | break; 37 | } 38 | case 3: 39 | { 40 | printf("\n enter two number : "); 41 | scanf("%c%c",&f,&g); 42 | printf("\n before swapping two numbers are:\n a=%c b=%c\n",f,g); 43 | swapc(f,g); 44 | swapvarc(&f,&g); 45 | break; 46 | } 47 | default: 48 | { 49 | printf("\n wrong choice try again"); 50 | main(); 51 | exit(0); 52 | } 53 | } 54 | 55 | printf("\n press 0 to exit...press any key to continue..."); 56 | scanf("%d",&c); 57 | }while(c!=0); 58 | return (0); 59 | getch(); 60 | } 61 | void swapi(int a,int b) 62 | { 63 | a=a+b; 64 | b=a-b; 65 | a=a-b; 66 | printf("\n swap without using third variable\na=%d b=%d\npress any key to continue\n",a,b); 67 | getch(); 68 | } 69 | void swapvari(int *a,int *b) 70 | { 71 | int temp; 72 | temp=*a; 73 | *a=*b; 74 | *b=temp; 75 | printf("\n swap using third variable\na=%d b=%d\npress any key to continue",*a,*b); 76 | getch(); 77 | } 78 | 79 | void swapf(float a,float b) 80 | { 81 | a=a+b; 82 | b=a-b; 83 | a=a-b; 84 | printf("\n swap without using third variable\na=%f b=%f\npress any key to continue",a,b); 85 | getch(); 86 | } 87 | void swapvarf(float *a,float *b) 88 | { 89 | float temp; 90 | temp=*a; 91 | *a=*b; 92 | *b=temp; 93 | printf("\n swap using third variable\na=%f b=%f\npress any key to continue",*a,*b); 94 | getch(); 95 | } 96 | 97 | void swapc(char a,char b) 98 | { 99 | a=a+b; 100 | b=a-b; 101 | a=a-b; 102 | printf("\n swap without using third variable\n%c %c\npress any key to continue",a,b); 103 | getch(); 104 | } 105 | void swapvarc(char *a,char *b) 106 | { 107 | char temp; 108 | temp=*a; 109 | *a=*b; 110 | *b=temp; 111 | printf("\n swap using third variable\n%c %c\npress any key to continue",*a,*b); 112 | getch(); 113 | } 114 | -------------------------------------------------------------------------------- /no9_runtimePolymorphism.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Person 6 | { 7 | protected: 8 | string name; 9 | long cellNumber; 10 | 11 | public: 12 | virtual void getData(void) 13 | { 14 | cout << "Enter the name:"; 15 | cin >> name; 16 | cout << "Enter the mobile number: "; 17 | cin >> cellNumber; 18 | } 19 | virtual void displayData(void) 20 | { 21 | cout << "Name: " << name; 22 | cout << "Mobile Number: " << cellNumber; 23 | } 24 | }; 25 | 26 | class Student : public Person 27 | { 28 | private: 29 | int std, roll; 30 | 31 | public: 32 | void getData(void) 33 | { 34 | cout << "Enter the name:"; 35 | cin >> name; 36 | cout << "Enter the class: "; 37 | cin >> std; 38 | cout << "Enter the roll no.: "; 39 | cin >> roll; 40 | cout << "Enter the mobile number: "; 41 | cin >> cellNumber; 42 | } 43 | void displayData(void) 44 | { 45 | cout <> name; 63 | cout << "Enter the subject taught: "; 64 | cin >> subject; 65 | cout << "Enter the salary: "; 66 | cin >> salary; 67 | cout << "Enter the mobile number: "; 68 | cin >> cellNumber; 69 | } 70 | void displayData(void) 71 | { 72 | cout << endl<< "Name: " << name << endl; 73 | cout << "Subject taught: " << subject << endl; 74 | cout << "Salary: " << salary << endl; 75 | cout << "Mobile Number: " << cellNumber << endl; 76 | } 77 | }; 78 | 79 | int main() 80 | { 81 | Person *obj; 82 | cout << "Choose an option.." << endl; 83 | cout << "1. Student" << endl; 84 | cout << "2. Teacher:" << endl; 85 | cout << "->"; 86 | int ch; 87 | cin >> ch; 88 | switch (ch) 89 | { 90 | case 1: 91 | obj = new Student(); 92 | obj->getData(); 93 | cout<<"data added successfully"; 94 | obj->displayData(); 95 | break; 96 | case 2: 97 | obj = new Teacher(); 98 | obj->getData(); 99 | cout<<"data added successfully"; 100 | obj->displayData(); 101 | break; 102 | default: 103 | cout << "Invalid Input!"; 104 | break; 105 | } 106 | cout<<"\n\n\n"; 107 | delete obj; 108 | cout << "Object Deleted!"; 109 | return 0; 110 | } -------------------------------------------------------------------------------- /6.stack_using_linked_list.cpp: -------------------------------------------------------------------------------- 1 | //stack operations using linked list : 2 | /*---------------------------------------------------------------------------------------------------------------------*/ 3 | #include "iostream" 4 | #include "stdlib.h" 5 | using namespace std; 6 | void push(); 7 | void pop(); 8 | void display(); 9 | struct node 10 | { 11 | int val; 12 | struct node *next; 13 | }; 14 | struct node *head; 15 | int main () 16 | { 17 | int choice=0; 18 | cout<<"\n*********Stack operations using linked list*********\n"; 19 | cout<<"\n----------------------------------------------\n"; 20 | while(choice != 4) 21 | { 22 | cout<<"\n\nChoose one from the below options...\n"; 23 | cout<<"\n1.Push\n2.Pop\n3.Show\n4.Exit"; 24 | cout<<"\n Enter your choice \n"; 25 | cin>>choice; 26 | switch(choice) 27 | { 28 | case 1: 29 | { 30 | push(); 31 | break; 32 | } 33 | case 2: 34 | { 35 | pop(); 36 | break; 37 | } 38 | case 3: 39 | { 40 | display(); 41 | break; 42 | } 43 | case 4: 44 | { 45 | cout<<"Exiting.... "; 46 | break; 47 | } 48 | default: 49 | { 50 | cout<<"Please Enter valid choice "; 51 | } 52 | } 53 | } 54 | } 55 | void push () 56 | { 57 | int val; 58 | struct node *ptr = (struct node*)malloc(sizeof(struct node)); 59 | if(ptr == NULL) 60 | { 61 | cout<<"not able to push the element"; 62 | } 63 | else 64 | { 65 | cout<<"Enter the value "; 66 | cin>>val; 67 | if(head==NULL) 68 | { 69 | ptr->val = val; 70 | ptr -> next = NULL; 71 | head=ptr; 72 | } 73 | else 74 | { 75 | ptr->val = val; 76 | ptr->next = head; 77 | head=ptr; 78 | } 79 | cout<<"Item pushed "; 80 | } 81 | } 82 | void pop() 83 | { 84 | int item; 85 | struct node *ptr; 86 | if (head == NULL) 87 | { 88 | cout<<"Underflow "; 89 | } 90 | else 91 | { 92 | item = head->val; 93 | ptr = head; 94 | head = head->next; 95 | free(ptr); 96 | cout<<"Item popped "; 97 | 98 | } 99 | } 100 | void display() 101 | { 102 | int i; 103 | struct node *ptr; 104 | ptr=head; 105 | if(ptr == NULL) 106 | { 107 | cout<<"Stack is empty\n"; 108 | } 109 | else 110 | { 111 | cout<<"Printing Stack elements \n"; 112 | while(ptr!=NULL) 113 | { 114 | cout<val<<" "; 115 | ptr = ptr->next; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /trisvs.cpp: -------------------------------------------------------------------------------- 1 | /*Create a class Traingle. Include overloaded functions for calculating area. Overload assignment operator and equality operator. */ 2 | #include 3 | #include"iostream" 4 | #include"math.h" 5 | using namespace std; 6 | class triangle 7 | { 8 | private: 9 | int area,a,b,c; 10 | public: 11 | triangle(){} 12 | triangle (int x,int y,int z) { 13 | this->a=x; 14 | this->b=y; 15 | this->a=z; 16 | } 17 | 18 | void display() { 19 | cout<<"sides are : "<a<<" "<b<<" "<c<area; 41 | } 42 | friend bool operator==(triangle &t1,triangle &t2); 43 | void operator=(triangle &t1); 44 | }; 45 | bool operator==(triangle &t1,triangle &t2) { 46 | if(t1.getarea()==t2.getarea()) 47 | return true; 48 | } 49 | 50 | void triangle::operator=(triangle &t1) { 51 | this->a=t1.a; 52 | this->b=t1.b; 53 | this->c=t1.c; 54 | } 55 | 56 | void trimenu() 57 | { 58 | system("cls"); 59 | cout<<"what type of triangle you want.?"<>y; 72 | switch(y) 73 | { 74 | case 1:{ 75 | t2=t; 76 | cout<<"assigned successfully"; 77 | cout<<"new triangle's "; 78 | t2.display(); 79 | break; 80 | } 81 | case 2:{ 82 | cout<<"enter the value of the 1st side : "; 83 | cin>>a; 84 | cout<<"enter the value of the 2nd side : "; 85 | cin>>b; 86 | cout<<"enter the value of the 3rd side : "; 87 | cin>>c; 88 | triangle t2(a,b,c); 89 | t2.area(a,b,c); 90 | if(t2==t) 91 | cout<<"triangles are same."; 92 | else 93 | cout<<"triangles are not same."; 94 | break; 95 | } 96 | default :{ 97 | cout<<"wrong input... try again...Thank you..."; 98 | break; 99 | } 100 | } 101 | } 102 | int main() 103 | { 104 | trimenu(); 105 | int a, b, c, y; 106 | cin>>y; 107 | switch(y){ 108 | case 1:{ 109 | cout<<"enter the value of a side : "; 110 | cin>>a; 111 | triangle t(a,a,a); 112 | t.area(a); 113 | menu(); 114 | break; 115 | } 116 | case 2:{ 117 | cout<<"enter the value of the same side : "; 118 | cin>>a; 119 | cout<<"enter the value of the 3rd side : "; 120 | cin>>b; 121 | triangle(a,a,b); 122 | triangle t; 123 | t.area(a,b); 124 | menu(); 125 | break; 126 | } 127 | case 3:{ 128 | cout<<"enter the value of the 1st side : "; 129 | cin>>a; 130 | cout<<"enter the value of the 2nd side : "; 131 | cin>>b; 132 | cout<<"enter the value of the 3rd side : "; 133 | cin>>c; 134 | triangle t(a,b,c); 135 | t.area(a,b,c); 136 | menu(); 137 | break; 138 | } 139 | default:{ 140 | cout<<"wrong input try again...."; 141 | main(); 142 | exit ; 143 | } 144 | } 145 | 146 | return 0; 147 | } 148 | -------------------------------------------------------------------------------- /triangle.cpp: -------------------------------------------------------------------------------- 1 | //equilateral same 0 2 | //isoscale 2 same 1 3 | //scalen no same 2 4 | /*Create a class Traingle. Include overloaded functions for calculating area. Overload assignment operator and equality operator. */ 5 | 6 | #include"iostream" 7 | #include"math.h" 8 | using namespace std; 9 | class triangle 10 | { 11 | private:int area,a,b,c; 12 | public: triangle (int x,int y,int z) 13 | { 14 | this->a=x; 15 | this->b=y; 16 | this->a=z; 17 | } 18 | display() 19 | { 20 | cout<<"sides are : "<a<<" "<b<<" "<c<area; 44 | } 45 | friend operator=(triangle &t1); 46 | friend operator==(triangle &t1,triangle &t2); 47 | } 48 | friend triangle::operator==(triangle &t1,triangle &t2) 49 | { 50 | if(t1.getarea()=t2.getarea) 51 | return true; 52 | } 53 | 54 | friend triangle::operator=(triangle &t1) 55 | { 56 | this->a=t1.a; 57 | this->b=t1.b; 58 | this->c=t1.c; 59 | } 60 | void trimenu() 61 | { 62 | system("cls"); 63 | cout<<"what type of triangle you want.?"<>int y; 73 | switch(y) 74 | { 75 | case 1:{ 76 | triangle t2; 77 | t2=t; 78 | cout<<"assigned successfully"; 79 | cout<<"new triangle's "; 80 | t2.display(); 81 | break; 82 | } 83 | case 2:{ 84 | cout<<"enter the value of the 1st side : "; 85 | cin<>int y; 108 | switch(y){ 109 | case 1:{ 110 | cout<<"enter the value of a side : "; 111 | cin< 4 | class matrix{ 5 | private: T r,c,mat[10][10]; 6 | public: matrix(); 7 | matrix(int r,int c); 8 | display(); 9 | trans(); 10 | friend void diff(matrix &,matrix &,matrix &); 11 | friend void add(matrix &,matrix &,matrix &); 12 | friend void prod(matrix &,matrix &,matrix &); 13 | 14 | }; 15 | template 16 | matrix::matrix() 17 | { 18 | 19 | } 20 | template 21 | matrix::matrix(int r,int c) 22 | { 23 | for(int i=0;i>this->mat[i][j]; 29 | } 30 | } 31 | cout<<"value entered successfully..."; 32 | } 33 | template 34 | matrix::display() 35 | { 36 | for(int i=0;imat[i][j]<<" "; 41 | } 42 | cout<::trans() 89 | { 90 | 91 | for(int i=0;iresult[i][j]<<" "; 102 | } 103 | cout<>int c; 112 | return c; 113 | } 114 | 115 | int main() 116 | { 117 | do{ 118 | int c=menu(); 119 | switch(c){ 120 | case 1:{ 121 | cout<<"enter the value of row and colomn 1>int r>>int c; 123 | matrix<{int,int}>m1(r,c); 124 | matrix<{int,int}>m2(r,c); 125 | matrixresult(); 126 | sum(m1,m2,result); 127 | result.display(); 128 | break; 129 | } 130 | case 2:{ 131 | cout<<"enter the value of row and colomn 1>int r>>int c; 133 | matrix<{int,int}>m1(r,c); 134 | matrix<{int,int}>m2(r,c); 135 | matrixresult(); 136 | diff(m1,m2,result); 137 | result.display(); 138 | break; 139 | } 140 | case 3:{ 141 | cout<<"enter the value of row and colomn 1>int r>>int c; 143 | matrix<{int,int}>m1(r,c); 144 | matrix<{int,int}>m2(r,c); 145 | matrixresult(); 146 | prod(m1,m2,result); 147 | result.display(); 148 | break; 149 | } 150 | case 4:{ 151 | cout<<"enter the value of row and colomn 1>int r>>int c; 153 | matrix<{int,int}>m1(r,c); 154 | m1.trans(); 155 | break; 156 | } 157 | case 5:{ 158 | exit 0; 159 | } 160 | } 161 | cout<<"press any key to continue... press 0 to exit..." ; 162 | cin>> int y; 163 | }while(y!=0) 164 | return 0; 165 | } 166 | -------------------------------------------------------------------------------- /subham.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include "stdlib.h" 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *next; 8 | }; 9 | struct node *head; 10 | 11 | void beginsert(); 12 | void lastinsert(); 13 | void randominsert(); 14 | void display(); 15 | 16 | void main() 17 | { 18 | int ch = 0; 19 | while (ch != 5) 20 | { 21 | printf("\n*********Main Menu*********"); 22 | printf("\nChoose one option from the following list ..."); 23 | printf("\n==============================================="); 24 | printf("\n1. Insert at Beginning"); 25 | printf("\n2. Insert at End"); 26 | printf("\n3. Insert in Desired Location"); 27 | printf("\n4. Display"); 28 | printf("\n5. Exit"); 29 | printf("\n Enter Your Choich : "); 30 | scanf("%d",&ch); 31 | switch (ch) 32 | { 33 | case 1: 34 | beginsert(); 35 | break; 36 | case 2: 37 | lastinsert(); 38 | break; 39 | case 3: 40 | randominsert(); 41 | break; 42 | case 4: 43 | display(); 44 | break; 45 | case 5: 46 | exit(0); 47 | break; 48 | default: 49 | printf("wrong input try again."); 50 | ch = 0; 51 | break; 52 | } 53 | } 54 | } 55 | void beginsert() 56 | { 57 | struct node *ptr; 58 | int item; 59 | ptr = (struct node *)malloc(sizeof(struct node *)); 60 | if (ptr == NULL) 61 | { 62 | printf("\nOVERFLOW"); 63 | } 64 | else 65 | { 66 | printf("\nEnter value\n"); 67 | scanf("%d", &item); 68 | ptr->data = item; 69 | ptr->next = head; 70 | head = ptr; 71 | printf("\nNode inserted"); 72 | } 73 | } 74 | void lastinsert() 75 | { 76 | struct node *ptr, *temp; 77 | int item; 78 | ptr = (struct node *)malloc(sizeof(struct node)); 79 | if (ptr == NULL) 80 | { 81 | printf("\nOVERFLOW"); 82 | } 83 | else 84 | { 85 | printf("\nEnter value?\n"); 86 | scanf("%d", &item); 87 | ptr->data = item; 88 | if (head == NULL) 89 | { 90 | ptr->next = NULL; 91 | head = ptr; 92 | printf("\nNode inserted"); 93 | } 94 | else 95 | { 96 | temp = head; 97 | while (temp->next != NULL) 98 | { 99 | temp = temp->next; 100 | } 101 | temp->next = ptr; 102 | ptr->next = NULL; 103 | printf("\nNode inserted"); 104 | } 105 | } 106 | } 107 | void randominsert() 108 | { 109 | int i, loc, item; 110 | struct node *ptr, *temp; 111 | ptr = (struct node *)malloc(sizeof(struct node)); 112 | if (ptr == NULL) 113 | { 114 | printf("\nOVERFLOW"); 115 | } 116 | else 117 | { 118 | printf("\nEnter element value"); 119 | scanf("%d", &item); 120 | ptr->data = item; 121 | printf("\nEnter the location after which you want to insert "); 122 | scanf("\n%d", &loc); 123 | temp = head; 124 | for (i = 0; i < loc; i++) 125 | { 126 | temp = temp->next; 127 | if (temp == NULL) 128 | { 129 | printf("\ncan't insert\n"); 130 | return; 131 | } 132 | } 133 | ptr->next = temp->next; 134 | temp->next = ptr; 135 | printf("\nNode inserted"); 136 | } 137 | } 138 | void display() 139 | { 140 | struct node *ptr; 141 | ptr = head; 142 | if (ptr == NULL) 143 | { 144 | printf("Nothing to print"); 145 | } 146 | else 147 | { 148 | printf("\nprinting values . . . . .\n"); 149 | while (ptr != NULL) 150 | { 151 | printf("\n%d", ptr->data); 152 | ptr = ptr->next; 153 | } 154 | } 155 | } -------------------------------------------------------------------------------- /matrix menu.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | using namespace std; 3 | template 4 | class matrix{ 5 | private: T r,c,mat[10][10]; 6 | public: matrix(); 7 | matrix(matrix r,matrix c); 8 | display(); 9 | trans(); 10 | friend void diff(matrix &m1,matrix &m2,matrix &r); 11 | friend void add(matrix &m1,matrix &m2,matrix &r); 12 | friend void prod(matrix &m1,matrix &m2,matrix &result); 13 | 14 | }; 15 | template 16 | matrix::matrix() 17 | { 18 | 19 | } 20 | template 21 | matrix::matrix(T r,T c) 22 | { 23 | for(T i=0;i>this->mat[i][j]; 29 | } 30 | } 31 | cout<<"value entered successfully..."; 32 | } 33 | template 34 | matrix::display() 35 | { 36 | for(T i=0;imat[i][j]<<" "; 41 | } 42 | cout< 46 | friend matrix::sum(matrix m1,matrix m2,matrix result) 47 | { 48 | for(T i=0;i 58 | friend matrix::diff(matrix m1,matrix m2,matrix result) 59 | { 60 | for(T i=0;i 70 | friend matrix::prod(matrix m1,matrix m2,matrix result) 71 | { 72 | for(T i=0;i 88 | matrix::trans() 89 | { 90 | 91 | for(int i=0;iresult[i][j]<<" "; 102 | } 103 | cout<>int c; 112 | return c; 113 | } 114 | 115 | int main() 116 | { 117 | do{ 118 | int c=menu(); 119 | switch(c){ 120 | case 1:{ 121 | cout<<"enter the value of row and colomn 1>int r>>int c; 123 | matrixm1(r,c); 124 | matrixm2(r,c); 125 | matrixresult(); 126 | sum(m1,m2,result); 127 | result.display(); 128 | break; 129 | } 130 | case 2:{ 131 | cout<<"enter the value of row and colomn 1>int r>>int c; 133 | matrixm1(r,c); 134 | matrixm2(r,c); 135 | matrixresult(); 136 | diff(m1,m2,result); 137 | result.display(); 138 | break; 139 | } 140 | case 3:{ 141 | cout<<"enter the value of row and colomn 1>int r>>int c; 143 | matrixm1(r,c); 144 | matrixm2(r,c); 145 | matrixresult(); 146 | prod(m1,m2,result); 147 | result.display(); 148 | break; 149 | } 150 | case 4:{ 151 | cout<<"enter the value of row and colomn 1>int r>>int c; 153 | matrixm1(r,c); 154 | m1.trans(); 155 | break; 156 | } 157 | case 5:{ 158 | exit 0; 159 | } 160 | } 161 | cout<<"press any key to continue... press 0 to exit..." ; 162 | cin>>y; 163 | }while(y!=0) 164 | return 0; 165 | } 166 | -------------------------------------------------------------------------------- /subham.cpp: -------------------------------------------------------------------------------- 1 | #include "iostream" 2 | #include "stdlib.h" 3 | using namespace std; 4 | 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }; 10 | struct node *head; 11 | 12 | void beginsert(); 13 | void lastinsert(); 14 | void randominsert(); 15 | void display(); 16 | 17 | int main() 18 | { 19 | int ch = 0; 20 | while (ch != 5) 21 | { 22 | cout << "\n*********Main Menu*********"; 23 | cout << "\nChoose one option from the following list ..."; 24 | cout << "\n==============================================="; 25 | cout << "\n1. Insert at Beginning"; 26 | cout << "\n2. Insert at End"; 27 | cout << "\n3. Insert in Desired Location"; 28 | cout << "\n4. Display"; 29 | cout << "\n5. Exit"; 30 | cout << "\n Enter Your Choich : "; 31 | cin >> ch; 32 | switch (ch) 33 | { 34 | case 1: 35 | beginsert(); 36 | break; 37 | case 2: 38 | lastinsert(); 39 | break; 40 | case 3: 41 | randominsert(); 42 | break; 43 | case 4: 44 | display(); 45 | break; 46 | case 5: 47 | exit(0); 48 | break; 49 | default: 50 | cout << "wrong input try again."; 51 | ch = 0; 52 | break; 53 | } 54 | } 55 | return 0; 56 | } 57 | void beginsert() 58 | { 59 | struct node *ptr; 60 | int item; 61 | ptr = (struct node *)malloc(sizeof(struct node *)); 62 | if (ptr == NULL) 63 | { 64 | printf("\nOVERFLOW"); 65 | } 66 | else 67 | { 68 | printf("\nEnter value\n"); 69 | scanf("%d", &item); 70 | ptr->data = item; 71 | ptr->next = head; 72 | head = ptr; 73 | printf("\nNode inserted"); 74 | } 75 | } 76 | void lastinsert() 77 | { 78 | struct node *ptr, *temp; 79 | int item; 80 | ptr = (struct node *)malloc(sizeof(struct node)); 81 | if (ptr == NULL) 82 | { 83 | printf("\nOVERFLOW"); 84 | } 85 | else 86 | { 87 | printf("\nEnter value?\n"); 88 | scanf("%d", &item); 89 | ptr->data = item; 90 | if (head == NULL) 91 | { 92 | ptr->next = NULL; 93 | head = ptr; 94 | printf("\nNode inserted"); 95 | } 96 | else 97 | { 98 | temp = head; 99 | while (temp->next != NULL) 100 | { 101 | temp = temp->next; 102 | } 103 | temp->next = ptr; 104 | ptr->next = NULL; 105 | printf("\nNode inserted"); 106 | } 107 | } 108 | } 109 | void randominsert() 110 | { 111 | int i, loc, item; 112 | struct node *ptr, *temp; 113 | ptr = (struct node *)malloc(sizeof(struct node)); 114 | if (ptr == NULL) 115 | { 116 | printf("\nOVERFLOW"); 117 | } 118 | else 119 | { 120 | printf("\nEnter element value"); 121 | scanf("%d", &item); 122 | ptr->data = item; 123 | printf("\nEnter the location after which you want to insert "); 124 | scanf("\n%d", &loc); 125 | temp = head; 126 | for (i = 0; i < loc; i++) 127 | { 128 | temp = temp->next; 129 | if (temp == NULL) 130 | { 131 | printf("\ncan't insert\n"); 132 | return; 133 | } 134 | } 135 | ptr->next = temp->next; 136 | temp->next = ptr; 137 | printf("\nNode inserted"); 138 | } 139 | } 140 | void display() 141 | { 142 | struct node *ptr; 143 | ptr = head; 144 | if (ptr == NULL) 145 | { 146 | printf("Nothing to print"); 147 | } 148 | else 149 | { 150 | printf("\nprinting values . . . . .\n"); 151 | while (ptr != NULL) 152 | { 153 | printf("\n%d", ptr->data); 154 | ptr = ptr->next; 155 | } 156 | } 157 | } -------------------------------------------------------------------------------- /no10_metriangle.cpp: -------------------------------------------------------------------------------- 1 | /*Create a class Traingle. Include overloaded functions for calculating area. Overload assignment operator and equality operator. */ 2 | #include"iostream" 3 | #include"math.h" 4 | using namespace std; 5 | class triangle 6 | { 7 | private: 8 | int area1,a,b,c; 9 | public: 10 | triangle(){} 11 | triangle (int x,int y,int z) { 12 | a=x; 13 | b=y; 14 | c=z; 15 | } 16 | 17 | void display() { 18 | cout<<"sides are : "<a<<" "<b<<" "<c<area1; 44 | } 45 | friend bool operator==(triangle &t1,triangle &t2); 46 | void operator=(triangle &t1); 47 | }; 48 | bool operator==(triangle &t1,triangle &t2) { 49 | if(t1.getarea()==t2.getarea()) 50 | return true; 51 | else 52 | { 53 | return false; 54 | } 55 | 56 | } 57 | 58 | void triangle::operator=(triangle &t1) { 59 | a=t1.a; 60 | b=t1.b; 61 | c=t1.c; 62 | } 63 | 64 | void trimenu() 65 | { 66 | cout<<"what type of triangle you want.?"<>y; 78 | switch(y) 79 | { 80 | case 1:{ 81 | cout<<"enter the value of the 1st side : "; 82 | cin>>a; 83 | cout<<"enter the value of the 2nd side : "; 84 | cin>>b; 85 | cout<<"enter the value of the 3rd side : "; 86 | cin>>c; 87 | triangle t3(a,b,c); 88 | t2=t3; 89 | cout<<"assigned successfully"; 90 | cout<<"new triangle's "; 91 | t2.display(); 92 | break; 93 | } 94 | case 2:{ 95 | cout<<"enter the value of the 1st side : "; 96 | cin>>a; 97 | cout<<"enter the value of the 2nd side : "; 98 | cin>>b; 99 | cout<<"enter the value of the 3rd side : "; 100 | cin>>c; 101 | triangle t2(a,b,c); 102 | if(t2==t) 103 | cout<>y; 119 | switch(y){ 120 | case 1:{ 121 | cout<<"enter the value of a side : "; 122 | cin>>a; 123 | triangle t(a,a,a); 124 | t.area(a); 125 | menu(t); 126 | break; 127 | } 128 | case 2:{ 129 | cout<<"enter the value of the same side : "; 130 | cin>>a; 131 | cout<<"enter the value of the 3rd side : "; 132 | cin>>b; 133 | triangle(a,a,b); 134 | triangle t; 135 | t.area(a,b); 136 | menu(t); 137 | break; 138 | } 139 | case 3:{ 140 | cout<<"enter the value of the 1st side : "; 141 | cin>>a; 142 | cout<<"enter the value of the 2nd side : "; 143 | cin>>b; 144 | cout<<"enter the value of the 3rd side : "; 145 | cin>>c; 146 | triangle t(a,b,c); 147 | t.area(a,b,c); 148 | menu(t); 149 | break; 150 | } 151 | default:{ 152 | cout<<"wrong input try again...."; 153 | main(); 154 | exit ; 155 | } 156 | } 157 | 158 | return 0; 159 | } -------------------------------------------------------------------------------- /soumyaneel-emp.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | #include"conio.h" 3 | #include"stdio.h" 4 | #include"string.h" 5 | #define MAX 50 6 | using namespace std; 7 | 8 | static int x=0; 9 | 10 | class emp 11 | { 12 | private : char name[MAX],id[MAX],des[MAX]; 13 | int age,exp; 14 | public : void input(void) 15 | { 16 | cout<<"enter name: "; 17 | gets(name); 18 | cout<<"enter employee id: "; 19 | gets(id); 20 | cout<<"enter designation: "; 21 | gets(des); 22 | cout<<"enter age: "; 23 | cin>>age; 24 | cout<<"enter years of experience: "; 25 | cin>>exp; 26 | x++; 27 | } 28 | int getId() 29 | { 30 | return id; 31 | } 32 | void display(void) 33 | { 34 | cout<<"name: "; 35 | gets(name); 36 | cout<<"\n enter employee id: "; 37 | gets(id); 38 | cout<<"\n enter designation: "; 39 | gets(des); 40 | cout<<"\n enter age: "; 41 | cin>>age; 42 | cout<<"\n enter years of experience: "; 43 | cin>>exp; 44 | } 45 | 46 | 47 | 48 | }; 49 | 50 | 51 | 52 | 53 | 54 | 55 | void del(emp e[],int index) 56 | { 57 | char c; 58 | e[index].display(); 59 | for(int i=index; i>c; 72 | if(c=='y') 73 | { 74 | e[index].input(); 75 | } 76 | } 77 | void displaylist(emp e[]) 78 | { 79 | int i; 80 | if(x==0) 81 | cout<<"THE TABLE IS EMPTY"<>a; 98 | for(i=0;ie[j+1].id()) 124 | { 125 | emp t; 126 | t=e[j]; 127 | e[j]=e[j+1]; 128 | e[j+1]=t; 129 | } 130 | } 131 | } 132 | cout<<"The Sorted Table"< BUILD MASTER TABLE\n"; 149 | cout<<"2--> LIST TABLE\n"; 150 | cout<<"3--> INSERT NEW ENTRY\n"; 151 | cout<<"4--> DELETE OLD ENTRY\n"; 152 | cout<<"5--> EDIT AN ENTRY\n"; 153 | cout<<"6--> SEARCH ENTRY\n"; 154 | cout<<"7--> SORT ENTRIES\n"; 155 | cout<<"8--> EXIT\n"; 156 | cout<<"choose from 1-8: \n"; 157 | cin>>c; 158 | system("CLS"); 159 | switch(c){ 160 | case 1: cout<<"enter the size of the list: "; 161 | cin>>length; 162 | buildList(emp); 163 | break; 164 | case 2: showList(emp); 165 | break; 166 | case 3: emp[length+1].input(); 167 | ++length; 168 | break; 169 | case 4: 170 | index = searchList(emp); 171 | if(index!=-1) 172 | del(emp,index); 173 | break; 174 | case 5: 175 | index = searchList(emp); 176 | if(index!=-1) 177 | edit(emp,index); 178 | break; 179 | case 6: 180 | index = searchList(emp); 181 | if(index!=-1) 182 | emp[index].display(); 183 | break; 184 | case 7: sortList(emp); 185 | showList(emp); 186 | break; 187 | case 8: exit(0); 188 | } 189 | cout<<"press any key to go to main menu..."; 190 | getch(); 191 | goto menu; 192 | 193 | } 194 | -------------------------------------------------------------------------------- /sv_emp.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | #include"string" 3 | using namespace std; 4 | 5 | static int i=0; 6 | 7 | using namespace std; 8 | 9 | class emp 10 | { 11 | public: int age,exp,id; 12 | string name,des; 13 | emp() 14 | { 15 | age = exp = id = 0; 16 | name = des = ""; 17 | } 18 | void input() 19 | { 20 | cout<<"enter the name"; 21 | cin>>name; 22 | cout<<"enter age"; 23 | cin>>age; 24 | cout<<"enter employee id"; 25 | cin>>id; 26 | cout<<"enter employee designation"; 27 | cin>>des; 28 | cout<<"enter year of exprience"; 29 | cin>>exp; 30 | } 31 | void display() 32 | { 33 | cout<<"\n name is "<<(name); 34 | cout<<"\n age "<>x; 50 | for (j=0;j> Eid; 64 | for (j = 0; j < i; j++) 65 | if (e[j].gid()==Eid) 66 | { 67 | f = 1; 68 | break; 69 | } 70 | if (f==1) 71 | return j; 72 | else 73 | cout << "Invalid code!"; 74 | } 75 | void edit(emp e[], int e_id) 76 | { 77 | e[e_id].input(); 78 | } 79 | void sort(emp e[]) 80 | { 81 | int k,j; 82 | if(i==0) 83 | cout<<"TABLE EMPTY"<e[k+1].gid()) 91 | { 92 | emp t; 93 | t=e[k]; 94 | e[k]=e[k+1]; 95 | e[k+1]=t; 96 | } 97 | } 98 | } 99 | cout<<"The Sorted Table"<> c; 126 | switch (c) 127 | { 128 | case 1: 129 | { 130 | table(obj); 131 | break; 132 | } 133 | case 2: 134 | { 135 | printTable(obj); 136 | break; 137 | } 138 | case 3: 139 | { 140 | obj[i]; 141 | i++; 142 | break; 143 | } 144 | case 4: 145 | { 146 | cout<<"enter the id of the employee to delete the record"; 147 | cin>> id; 148 | for(int j=0;j> ch; 186 | if(ch=='Y') 187 | system("CLS"); 188 | else 189 | break; 190 | } while (true); 191 | return 0; 192 | 193 | } 194 | -------------------------------------------------------------------------------- /5.circular_linked_list.cpp: -------------------------------------------------------------------------------- 1 | //Circular Linked List using templates with functions for insertion, deletion and search of a number, reverse the list : 2 | /*---------------------------------------------------------------------------------------------------------------------*/ 3 | #include 4 | using namespace std; 5 | template 6 | class node 7 | { 8 | public: 9 | t data; 10 | node *next; 11 | }; 12 | template 13 | class clist 14 | { 15 | int n; 16 | node *first,*last; 17 | public: 18 | clist() 19 | { 20 | first=NULL; 21 | } 22 | void create() 23 | { 24 | node *current,*temp; 25 | cout<<"Enter how many nodes you want to enter in linked list:\n"; 26 | cin>>n; 27 | if(n>0) 28 | { 29 | if(first==NULL) 30 | { 31 | first=new node; 32 | cout<<"Enter data for first node:\n"; 33 | cin>>first->data; 34 | first->next=first; 35 | } 36 | current=first; 37 | for(int i=1;i; 41 | cin>>temp->data; 42 | temp->next=current->next; 43 | current->next=temp; 44 | current=current->next; 45 | } 46 | last=current; 47 | } 48 | } 49 | int count() 50 | { 51 | node *current; 52 | current=first; 53 | int c=0; 54 | while(current->next!=first) 55 | { 56 | c++; 57 | current=current->next; 58 | } 59 | c++; 60 | return c; 61 | } 62 | void insert() 63 | { 64 | cout<<"Enter the position of insertion;\n"; 65 | cin>>n; 66 | int b=count(); 67 | if(n<=b+1) 68 | { 69 | node *current,*temp; 70 | current=first; 71 | temp=new node; 72 | cout<<"Enter data:\n"; 73 | cin>>temp->data; 74 | temp->next=NULL; 75 | if(n==1) 76 | { 77 | temp->next=first; 78 | first=temp; 79 | last->next=first; 80 | } 81 | else 82 | { 83 | for(int i=1;inext; 85 | temp->next=current->next; 86 | current->next=temp; 87 | } 88 | } 89 | else 90 | cout<<"Can't be inserted\n"; 91 | } 92 | void search() 93 | { 94 | int flag=0; 95 | cout<<"Enter data to be searched:\n"; 96 | cin>>n; 97 | node *current,*prev,*temp; 98 | int b=count(); 99 | current=first; 100 | for(int i=1;i<=b;i++) 101 | { 102 | if(current->data==n) 103 | { 104 | flag=1; 105 | break; 106 | } 107 | prev=current; 108 | current=current->next; 109 | } 110 | if(flag==1) 111 | { 112 | int c; 113 | cout<<"Data found:\nEnter what you wannna do:\n 1.delete data\n 2.replace it\n 3.do nothing\n"; 114 | cin>>c; 115 | switch(c) 116 | { 117 | case 1:temp=current; 118 | prev->next=current->next; 119 | delete(temp); 120 | cout<<"Data deleted:\n"; 121 | break; 122 | case 2:cout<<"Enter new data:\n"; 123 | cin>>current->data; 124 | cout<<"data replaced:\n"; 125 | break; 126 | case 3:break; 127 | default:cout<<"wrong choice:\n"; 128 | } 129 | } 130 | else 131 | cout<<"Data not found:\n"; 132 | } 133 | void reverse() 134 | { 135 | node *a,*b,*temp; 136 | a=first; 137 | b=a->next; 138 | temp=b->next; 139 | a->next=NULL; 140 | while(temp!=first) 141 | { 142 | b->next=a; 143 | a=b; 144 | b=temp; 145 | temp=temp->next; 146 | } 147 | b->next=a; 148 | first->next=b; 149 | first=first->next; 150 | } 151 | void display() 152 | { 153 | node *current; 154 | current=first; 155 | while(current->next!=first) 156 | { 157 | cout<data<<" -> "; 158 | current=current->next; 159 | } 160 | cout<data<<" -> \n"; 161 | } 162 | }; 163 | int main() 164 | { 165 | char ch; 166 | clist l1; 167 | l1.create(); 168 | l1.display(); 169 | do 170 | { 171 | cout<<"Want to insert a node:\n"; 172 | cin>>ch; 173 | if(ch=='y') 174 | l1.insert(); 175 | }while(ch=='y'); 176 | cout<<"The linked list after all insertions:\n"; 177 | l1.display(); 178 | do 179 | { 180 | cout<<"Want to search a node:\n"; 181 | cin>>ch; 182 | if(ch=='y') 183 | l1.search(); 184 | }while(ch=='y'); 185 | cout<<"The linked list after all operations:\n"; 186 | l1.display(); 187 | cout<<"Want to see reversed linked list:\n"; 188 | cin>>ch; 189 | if(ch=='y') 190 | { 191 | l1.reverse(); 192 | } 193 | cout<<"The linked list after reversing:-\n"; 194 | l1.display(); 195 | } 196 | -------------------------------------------------------------------------------- /s_emp.cpp: -------------------------------------------------------------------------------- 1 | #include"iostream" 2 | #include"string" 3 | #include"stdlib.h" 4 | 5 | static int i=0; 6 | 7 | using namespace std; 8 | 9 | class emp 10 | { 11 | 12 | 13 | public: char name[50],des[50]; 14 | int age,exp,id; 15 | emp() 16 | { 17 | cout<<"enter the name"; 18 | gets(name); 19 | cout<<"enter age"; 20 | cin>>age; 21 | cout<<"enter employee id"; 22 | cin>>id; 23 | cout<<"enter employee designation"; 24 | gets(des); 25 | cout<<"enter year of exprience"; 26 | cin>>exp; 27 | } 28 | void display() 29 | { 30 | cout<<"\n name is "<>x; 46 | for (j=0;j>id; 56 | for( j=0;j>c; 63 | switch(c) 64 | { 65 | case 1: 66 | { 67 | cout<<"enter the name of the employee"; 68 | gets(e[j].name); 69 | break; 70 | } 71 | case 2: 72 | { 73 | cout<<"enter the designation"; 74 | gets(e[j].des); 75 | break; 76 | } 77 | case 3: 78 | { 79 | cout<<"enter the age"; 80 | cin>>e[j].age; 81 | break; 82 | } 83 | case 4: 84 | { 85 | cout<<"enter the year of exprience"; 86 | cin>>e[j].exp; 87 | } 88 | case 5: 89 | { 90 | exit (0); 91 | } 92 | default: 93 | { 94 | cout<<"wrong choich...try again"; 95 | system("cls"); 96 | exit(0); 97 | } 98 | } 99 | } 100 | 101 | cout<<"id not found"; 102 | } 103 | } 104 | void sort(emp e[]) 105 | { 106 | int k,j; 107 | if(i==0) 108 | cout<<"TABLE EMPTY"<e[k+1].gid()) 116 | { 117 | emp t; 118 | t=e[k]; 119 | e[k]=e[k+1]; 120 | e[k+1]=t; 121 | } 122 | } 123 | } 124 | cout<<"The Sorted Table"<> c; 153 | switch (c) 154 | { 155 | case 1: 156 | { 157 | table(e); 158 | break; 159 | } 160 | case 2: 161 | { 162 | for(int j=0;j>id; 180 | for(int j=0;j>id; 202 | for(int j=0;j> cs; 223 | if(c=='Y') 224 | system("CLS"); 225 | else 226 | break; 227 | } while (true); 228 | return 0; 229 | 230 | } 231 | -------------------------------------------------------------------------------- /4.doubly_linked_list.cpp: -------------------------------------------------------------------------------- 1 | //Doubly Linked List using templates with functions for insertion, deletion and search of a number, reverse the list. 2 | /*--------------------------------------------------------------------------------------------------------------------*/ 3 | 4 | #include 5 | using namespace std; 6 | template 7 | class node 8 | { 9 | public: 10 | t data; 11 | node *prev,*next; 12 | }; 13 | template 14 | class dlist 15 | { 16 | int n; 17 | node *first,*last; 18 | public: 19 | dlist() 20 | { 21 | first=NULL; 22 | last=NULL; 23 | } 24 | //create function 25 | void create() 26 | { 27 | node *current,*temp; 28 | char ch; 29 | first=new node; 30 | cout<<"Enter data for first node:\n"; 31 | cin>>first->data; 32 | current=first; 33 | first->next=NULL; 34 | first->prev=NULL; 35 | last=first; 36 | do 37 | { 38 | cout<<"Want to enter more data:\n"; 39 | cin>>ch; 40 | if(ch=='y') 41 | { 42 | n=count(); 43 | this->insert(n+1); 44 | } 45 | }while(ch=='y'); 46 | } 47 | //display function 48 | void display() 49 | { 50 | node *current; 51 | current=first; 52 | cout<<"The data in linked list:\n"; 53 | while(current!=NULL) 54 | { 55 | cout<data<<" <-> "; 56 | current=current->next; 57 | } 58 | cout<<"\n"; 59 | } 60 | //reverse function 61 | void reverse() 62 | { 63 | n=count(); 64 | node *current; 65 | current=last; 66 | cout<<"The data after reversing the linked list:\n"; 67 | for(int i=1;i<=n;i++) 68 | { 69 | cout<data<<" -> "; 70 | current=current->prev; 71 | } 72 | } 73 | //count function 74 | int count() 75 | { 76 | int c=0; 77 | node *current; 78 | current=first; 79 | while(current!=NULL) 80 | { 81 | c++; 82 | current=current->next; 83 | } 84 | return c; 85 | } 86 | //insert function 87 | void insert(int n1) 88 | { 89 | int b=count(); 90 | if(n1<=b+1) 91 | { 92 | node *current,*forward,*temp; 93 | current=first; 94 | temp=new node; 95 | cout<<"Enter data:\n"; 96 | cin>>temp->data; 97 | temp->next=temp->prev=NULL; 98 | if(n1==1) 99 | { 100 | temp->next=first; 101 | first->prev=temp; 102 | first=temp; 103 | } 104 | else if(n1<=b) 105 | { 106 | for(int i=1;inext; 108 | forward=current->next; 109 | temp->next=forward; 110 | current->next=temp; 111 | temp->prev=current; 112 | forward->prev=temp; 113 | } 114 | else 115 | { 116 | last->next=temp; 117 | temp->prev=last; 118 | last=temp; 119 | } 120 | } 121 | else 122 | cout<<"Can't be inserted\n"; 123 | } 124 | //search function 125 | void search() 126 | { 127 | int flag=0; 128 | cout<<"Enter data to be searched:\n"; 129 | cin>>n; 130 | node *current,*previ,*temp; 131 | int b=count(); 132 | current=first; 133 | for(int i=1;i<=b;i++) 134 | { 135 | if(current->data==n) 136 | { 137 | flag=1; 138 | break; 139 | } 140 | current=current->next; 141 | } 142 | if(flag!=0) 143 | { 144 | previ=current->prev; 145 | int c; 146 | cout<<"Data found:\nEnter what you wannna do:\n 1.delete data\n 2.replace it\n 3.do nothing\n"; 147 | cin>>c; 148 | switch(c) 149 | { 150 | case 1:temp=current; 151 | if(current->next!=NULL) 152 | { 153 | current=current->next; 154 | previ->next=current; 155 | current->prev=previ; 156 | } 157 | else 158 | { 159 | previ->next=NULL; 160 | current->prev=NULL; 161 | } 162 | delete(temp); 163 | cout<<"Data deleted:\n"; 164 | this->display(); 165 | break; 166 | case 2:cout<<"Enter new data:\n"; 167 | cin>>current->data; 168 | cout<<"data replaced:\n"; 169 | this->display(); 170 | break; 171 | case 3:break; 172 | default:cout<<"wrong choice:\n"; 173 | } 174 | } 175 | else 176 | cout<<"Data not found:\n"; 177 | } 178 | //overloading + operator 179 | dlist operator +(dlist l) 180 | { 181 | dlist l6; 182 | l6.first=first; 183 | l6.last=last; 184 | l6.last->next=l.first; 185 | l.first->prev=l6.last; 186 | return l6; 187 | } 188 | }; 189 | int main() 190 | { 191 | int n; 192 | char ch; 193 | dlist l1,l3,l2; 194 | l1.create(); 195 | l1.display(); 196 | //doing insertion 197 | do 198 | { 199 | cout<<"Want to insert a node:\n"; 200 | cin>>ch; 201 | if(ch=='y') 202 | { 203 | cout<<"Enter the position of insertion;\n"; 204 | cin>>n; 205 | l1.insert(n); 206 | } 207 | }while(ch=='y'); 208 | cout<<"The linked list after all insertions:\n"; 209 | l1.display(); 210 | //doing searching,deleting,replacing 211 | do 212 | { 213 | cout<<"Want to search a data:\n"; 214 | cin>>ch; 215 | if(ch=='y') 216 | l1.search(); 217 | }while(ch=='y'); 218 | cout<<"The linked list after searching and as so:\n"; 219 | l1.display(); 220 | //creating new linked list 221 | cout<<"Want to concatenate new linked list:\n"; 222 | char cht; 223 | cin>>cht; 224 | if(cht=='y') 225 | { 226 | l2.create(); 227 | l2.display(); 228 | l3=l1+l2; 229 | cout<<"new linked list after concatenation:\n"; 230 | l3.display(); 231 | } 232 | return 0; 233 | } 234 | -------------------------------------------------------------------------------- /3.linked_list.cpp: -------------------------------------------------------------------------------- 1 | //Linked list with functions for insert search deletion concatenation reverse 2 | /*---------------------------------------------------------------------------------------------------------------------*/ 3 | 4 | #include 5 | using namespace std; 6 | template 7 | class node 8 | { 9 | public: 10 | t data; 11 | node *next; 12 | }; 13 | template 14 | class list 15 | { 16 | 17 | int n; 18 | node *first; 19 | 20 | public: 21 | list() 22 | { 23 | first = NULL; 24 | } 25 | 26 | void create() 27 | { 28 | char ch; 29 | node *current, *temp; 30 | cout << "Enter data:\n"; 31 | first = new node; 32 | cin >> first->data; 33 | first->next = NULL; 34 | current = first; 35 | do 36 | { 37 | cout << "Want to enter more data:\n"; 38 | cin >> ch; 39 | if (ch == 'y') 40 | { 41 | n = count(); 42 | this->insert(n + 1); 43 | } 44 | } while (ch == 'y'); 45 | } 46 | 47 | void create_beg() 48 | { 49 | cout << "Enter how many nodes u want to enter in linked list:\n"; 50 | cin >> n; 51 | node *temp; 52 | first = new node; 53 | cout << "Enter first node data:\n"; 54 | cin >> first->data; 55 | first->next = NULL; 56 | for (int i = 1; i < n; i++) 57 | { 58 | temp = new node; 59 | cout << "Enter node data:\n"; 60 | cin >> temp->data; 61 | temp->next = first; 62 | first = temp; 63 | } 64 | } 65 | 66 | void insert(t1 n) 67 | { 68 | int b = count(); 69 | if (n <= b + 1) 70 | { 71 | node *current, *temp; 72 | current = first; 73 | temp = new node; 74 | cout << "Enter data:\n"; 75 | cin >> temp->data; 76 | temp->next = NULL; 77 | if (n == 1) 78 | { 79 | temp->next = first; 80 | first = temp; 81 | } 82 | else 83 | { 84 | for (int i = 1; i < n - 1; i++) 85 | current = current->next; 86 | 87 | temp->next = current->next; 88 | current->next = temp; 89 | } 90 | } 91 | else 92 | cout << "Can't be inserted\n"; 93 | } 94 | 95 | //search function 96 | void search() 97 | { 98 | int flag = 0; 99 | cout << "Enter data to be searched:\n"; 100 | cin >> n; 101 | node *current, *prev, *temp; 102 | int b = count(); 103 | current = first; 104 | for (int i = 1; i <= b; i++) 105 | { 106 | if (current->data == n) 107 | { 108 | flag = 1; 109 | break; 110 | } 111 | prev = current; 112 | current = current->next; 113 | } 114 | if (flag == 1) 115 | { 116 | int c; 117 | cout << "Data found:\nEnter what you wannna do:\n 1.delete data\n 2.replace it\n 3.do nothing\n"; 118 | cin >> c; 119 | switch (c) 120 | { 121 | case 1: 122 | temp = current; 123 | prev->next = current->next; 124 | delete (temp); 125 | cout << "Data deleted:\n"; 126 | break; 127 | case 2: 128 | cout << "Enter new data:\n"; 129 | cin >> current->data; 130 | cout << "data replaced:\n"; 131 | break; 132 | case 3: 133 | break; 134 | default: 135 | cout << "wrong choice:\n"; 136 | } 137 | } 138 | else 139 | cout << "Data not found:\n"; 140 | } 141 | 142 | //overloading + operator 143 | list *operator+(list l1) 144 | { 145 | list l2; 146 | l2.first = first; 147 | node *current; 148 | current = l2.first; 149 | while (current->next != NULL) 150 | current = current->next; 151 | current->next = l1.first; 152 | return this; 153 | } 154 | 155 | //reverse function 156 | void reverse() 157 | { 158 | //fflush(stdin); 159 | node *a, *b, *temp; 160 | a = first; 161 | b = a->next; 162 | temp = b->next; 163 | a->next = NULL; 164 | while (temp != NULL) 165 | { 166 | //fflush(stdin); 167 | b->next = a; 168 | a = b; 169 | b = temp; 170 | temp = temp->next; 171 | //this->display(); 172 | } 173 | b->next = a; 174 | first = b; 175 | } 176 | 177 | //counting nodes in a linked list 178 | int count() 179 | { 180 | node *current; 181 | int c = 0; 182 | current = first; 183 | while (current != NULL) 184 | { 185 | c++; 186 | current = current->next; 187 | } 188 | return c; 189 | } 190 | 191 | //function to display linked list 192 | void display() 193 | { 194 | //getchar(); 195 | //fflush(stdin); 196 | node *current; 197 | current = first; 198 | cout << "The data in linked list:\n"; 199 | while (current != NULL) 200 | { 201 | cout << current->data << " -> "; 202 | current = current->next; 203 | } 204 | cout << endl; 205 | } 206 | }; 207 | int main() 208 | { 209 | //initial declarations 210 | int cho, n, s; 211 | char ch; 212 | list l1, l2, *l3; 213 | 214 | //creating linked list and working on them 215 | cout << "Want to create linked list:\n1.Simple\n2.from beginning:\n"; 216 | cin >> cho; 217 | switch (cho) 218 | { 219 | case 1: 220 | l1.create(); 221 | break; 222 | case 2: 223 | l1.create_beg(); 224 | break; 225 | default: 226 | cout << "Wrong choice:\n"; 227 | } 228 | 229 | if ((cho == 1) || (cho == 2)) 230 | { 231 | l1.display(); 232 | 233 | //doing insertion 234 | do 235 | { 236 | cout << "Want to insert a node:\n"; 237 | cin >> ch; 238 | if (ch == 'y') 239 | { 240 | cout << "Enter the position of insertion;\n"; 241 | cin >> n; 242 | l1.insert(n); 243 | } 244 | } while (ch == 'y'); 245 | 246 | cout << "The linked list after all insertions:\n"; 247 | l1.display(); 248 | 249 | //doing searching,deleting,replacing 250 | do 251 | { 252 | cout << "Want to search a data:\n"; 253 | cin >> ch; 254 | if (ch == 'y') 255 | l1.search(); 256 | } while (ch == 'y'); 257 | 258 | cout << "The linked list after searching and as so:\n"; 259 | l1.display(); 260 | 261 | //creating new linked list 262 | cout << "Want to create new linked list:\n"; 263 | char cht; 264 | cin >> cht; 265 | if (cht == 'y') 266 | { 267 | l2.create(); 268 | l2.display(); 269 | 270 | //concatenating strings 271 | cout << "Want to concatenate two linked lists:\n"; 272 | cin >> ch; 273 | if (ch == 'y') 274 | { 275 | l3 = l1 + l2; 276 | s = 1; 277 | cout << "new linked list after concatenation:\n"; 278 | l3->display(); 279 | } 280 | } 281 | 282 | //reversing linked lists 283 | cout << "Want to reverse the linked list:\n"; 284 | cin >> ch; 285 | if (ch == 'y') 286 | { 287 | cout << "Which linked list\n1."; 288 | l1.display(); 289 | if (cht == 'y') 290 | { 291 | cout << "\n2."; 292 | l2.display(); 293 | if (s == 1) 294 | { 295 | cout << "\n3."; 296 | l3->display(); 297 | } 298 | } 299 | } 300 | cin >> cho; 301 | if (cho == 1) 302 | { 303 | l1.reverse(); 304 | cout << "\nnew linked list after reversing:\n"; 305 | l1.display(); 306 | } 307 | else if (cht == 'y' && cho >= 2) 308 | { 309 | switch (cho) 310 | { 311 | case 2: 312 | l2.reverse(); 313 | cout << "\nnew linked list after reversing:\n"; 314 | l2.display(); 315 | break; 316 | case 3: 317 | l3->reverse(); 318 | cout << "\nnew linked list after reversing:\n"; 319 | l3->display(); 320 | break; 321 | default: 322 | cout << "Wrong choice entered:\n"; 323 | } 324 | } 325 | 326 | else 327 | cout << "Wrong choice entered:\n"; 328 | } 329 | } 330 | --------------------------------------------------------------------------------