├── .gitignore ├── README.md ├── c++ ├── Ambiguity_resolving.cpp ├── Binding.cpp ├── ComplexNo.cpp ├── DiceInheritance.cpp ├── Exception.cpp ├── Hierarchical_Inheritance.cpp ├── InitializationFinalization.cpp ├── MethodOverloading.cpp ├── MultiLevel.cpp ├── Threading.cpp ├── encapsulation.cpp ├── inheritance.cpp ├── multipleinheritance.cpp ├── source.md └── virtualInheritance.cpp ├── c ├── CallValueReference.c ├── GlobalLocal.c └── MergeSort.c ├── haskell ├── Arithemetic.hs ├── Factorial.hs ├── HigherOrder.hs ├── ListOperations.hs ├── Map.hs ├── MultiplyList.hs ├── Recursive.hs ├── Sum2N.hs ├── caseSwitch.hs ├── function1.hs ├── function2.hs ├── function3.hs ├── if-else.hs ├── list.hs ├── loop.hs ├── operator.hs ├── record.hs ├── userInput.hs └── variables.hs ├── java ├── DestructorExample.java ├── ExceptionHandling.java ├── Thread1.java ├── Thread2.java └── mergeSort.java ├── javascript ├── CharCheck.html ├── PasswordValid.html ├── Vowel.html ├── prac1.html ├── prac2.html ├── prac4.html └── readme.md └── prolog ├── Factorial.pl ├── Fibonacci.pl ├── MergeSort.pl └── Rectangle.pl /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | **/*.exe 3 | **/*.class 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CPPL-LAB 2 | This repo is for college lab related stuffs for PCPF. 3 | As we know there is no particular questions in PCPF only topics are mentioned. 4 | So i will be trying to cover most common questions which might be asked or we have done in lab. 5 | -------------------------------------------------------------------------------- /c++/Ambiguity_resolving.cpp: -------------------------------------------------------------------------------- 1 | // Multiple inheritance leads to ambiguity sometimes 2 | //Multiple inheritance doesnt work in java 3 | //2 parents to 1 child 4 | #include 5 | using namespace std; 6 | 7 | class A 8 | { 9 | public: 10 | void func() 11 | { 12 | cout << "I am A " << endl; 13 | } 14 | }; 15 | 16 | class B 17 | { 18 | public: 19 | void func() 20 | { 21 | cout << "I am B" << endl; 22 | } 23 | }; 24 | 25 | class C : public B, public A 26 | { 27 | }; 28 | 29 | int main() 30 | { 31 | C obj1; 32 | // obj1.func(); 33 | //to avoid confusion which parent function to call 34 | //we specify the parent name::function_name 35 | obj1.A::func(); 36 | obj1.B::func(); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /c++/Binding.cpp: -------------------------------------------------------------------------------- 1 | // Static Binding: Compile time binding. As shown in all programs 2 | // Dynamic Binding: Runtime binding using base class pointer we can create and use derived class objects 3 | // base class pointer points to derived class at runtime 4 | #include 5 | using namespace std; 6 | 7 | class regpoly 8 | { 9 | private: 10 | int nsides; 11 | 12 | protected: 13 | int lside; 14 | 15 | public: 16 | virtual void show() 17 | { 18 | std::cout << "The regular polygon has " << nsides << " sides of length " << lside << " units" << std::endl; 19 | } 20 | }; 21 | 22 | class square : public regpoly 23 | { 24 | public: 25 | square(int l) { lside = l; } 26 | void show() // Overridden method of class square 27 | { 28 | std::cout << "The square has 4 sides of length " << lside << " units" << std::endl; 29 | } 30 | }; 31 | 32 | class equitri : public regpoly 33 | { 34 | public: 35 | equitri(int l) { lside = l; } 36 | void show() // Overridden method of class equitri 37 | { 38 | std::cout << "The equilateral triangle has 3 sides of length " << lside << " units" << std::endl; 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | regpoly *r, *s;//parent ptr 45 | r = new square(5);//point to child 46 | r->show(); 47 | 48 | s = new equitri(4); 49 | s->show(); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /c++/ComplexNo.cpp: -------------------------------------------------------------------------------- 1 | // Operator overloading program in c++ 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Comp 8 | { 9 | // this both are private since not mentioned 10 | float re; 11 | float im; 12 | 13 | public: 14 | // constructors 15 | Comp() 16 | { 17 | re = 0; 18 | im = 0; 19 | } 20 | 21 | Comp(float a, float b) 22 | { 23 | re = a; 24 | im = b; 25 | } 26 | 27 | void show() 28 | { 29 | cout << re << "+(" << im << ")i" << endl; 30 | } 31 | 32 | //we can change the meaning of a symbol 33 | void operator~() 34 | { 35 | im = -1 * im; 36 | } 37 | 38 | void operator&() 39 | { 40 | re = -re; 41 | im = -im; 42 | } 43 | 44 | // return type comp ,taking input comp type 45 | Comp operator+(Comp p) 46 | { 47 | Comp t; 48 | 49 | t.re = re + p.re; 50 | t.im = im + p.im; 51 | return t; 52 | } 53 | 54 | Comp operator-(Comp p) 55 | { 56 | Comp t; 57 | 58 | t.re = re - p.re; 59 | t.im = im - p.im; 60 | return t; 61 | } 62 | 63 | Comp operator*(Comp p) 64 | { 65 | Comp t; 66 | 67 | t.re = re * p.re - im * p.im; 68 | t.im = re * p.im + im * p.re; 69 | return t; 70 | } 71 | 72 | Comp operator/(Comp p) 73 | { 74 | Comp t; 75 | 76 | t.re = (re * p.re + im * p.im) / (p.re * p.re + p.im * p.im); 77 | t.im = im * p.re - re * p.im / (p.re * p.re + p.im * p.im); 78 | 79 | return t; 80 | } 81 | }; 82 | 83 | int main() 84 | { 85 | Comp x(4, 5), y(2, 3), z; 86 | cout << "Addition is:" << endl; 87 | z = x + y; 88 | z.show(); 89 | 90 | cout << "Subtraction is:" << endl; 91 | z = x - y; 92 | z.show(); 93 | 94 | cout << "multiply is:" << endl; 95 | z = x * y; 96 | z.show(); 97 | 98 | cout << "division is:" << endl; 99 | z = x / y; 100 | z.show(); 101 | 102 | cout << "conjugate is:" << endl; 103 | ~x; 104 | x.show(); 105 | 106 | cout << "negative is:" << endl; 107 | &y; 108 | y.show(); 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /c++/DiceInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Cube 6 | { 7 | protected: 8 | int s; 9 | int Area; 10 | 11 | public: 12 | void getSide() 13 | { 14 | cout << "Please enter side of dice\n"; 15 | cin >> s; 16 | } 17 | 18 | int areacal() 19 | { 20 | Area = 6 * s * s; 21 | return Area; 22 | } 23 | }; 24 | 25 | class Dice 26 | { 27 | protected: 28 | int random; 29 | 30 | public: 31 | int playDice() 32 | { 33 | // srand((unsigned)time(NULL)); 34 | random = (rand() % 6) + 1; 35 | return random; 36 | } 37 | }; 38 | 39 | class Display : public Dice, public Cube 40 | { 41 | 42 | public: 43 | Display(int Area, int random) 44 | { 45 | this->Area = Area; 46 | this->random = random; 47 | 48 | cout << "Area of dice is " << Area << "\n"; 49 | cout << "You got " << random << " on the Dice!\n"; 50 | } 51 | }; 52 | 53 | int main() 54 | { 55 | Cube c1; 56 | c1.getSide(); 57 | Dice d1; 58 | Display s1 = Display(c1.areacal(), d1.playDice()); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /c++/Exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a = 10; 7 | int b = 0; 8 | 9 | // code that may raise exception 10 | if (b == 0) 11 | { 12 | cout << "Division not possible denominator is zero" << endl; 13 | } 14 | else 15 | { 16 | int c = a / b; 17 | cout << "division is :" << c << endl; 18 | } 19 | 20 | // Array exception 21 | int size; 22 | cout << "Enter size of array..."<< endl; 23 | cin >> size; 24 | 25 | double c[size]; 26 | cout<<"Enter elements of array"<> c[i]; 30 | } 31 | 32 | 33 | // CODE THAT MIGHT LEAD TO AN EXCEPTION 34 | int index=11; 35 | if (index >= size) 36 | { 37 | cout << "ArrayIndexOutofBounds Exception..." << endl; 38 | } 39 | else{ 40 | cout<<"value at Index is:"< 2 | using namespace std; 3 | 4 | class regpoly 5 | { 6 | private: 7 | int nsides; 8 | 9 | protected: 10 | int lside; 11 | 12 | public: 13 | regpoly() {} 14 | regpoly(int n, int l) 15 | { 16 | nsides = n; 17 | lside = l; 18 | } 19 | void show() 20 | { 21 | cout << "The regular polygon has " << nsides << " sides of length " << lside << "units" << endl; 22 | } 23 | }; 24 | 25 | class square : public regpoly // square is-a regpoly 26 | { 27 | private: 28 | int area, peri; 29 | 30 | public: 31 | square(int l) { lside = l; } 32 | void calarea() { area = lside * lside; } 33 | void calperi() // Method of class square 34 | { 35 | peri = 4 * lside; 36 | } 37 | void show() // Overridden method of class square 38 | { 39 | cout << "The square has 4 sides of length " << lside << " units" << endl; 40 | } 41 | void showareaperi() // Method of class square 42 | { 43 | cout << "The area and perimeter of square are " << area << " sq units and" << peri << " units respectively " << endl; 44 | } 45 | }; 46 | 47 | class equitri : public regpoly // equitri is-a regpoly 48 | { 49 | private: 50 | int peri; 51 | float area; 52 | 53 | public: 54 | equitri(int l) { lside = l; } 55 | void calarea() // Method of class equitri 56 | { 57 | area = 1.732 * lside * lside / 4; 58 | } 59 | void calperi() // Method of class equitri 60 | { 61 | peri = 3 * lside; 62 | } 63 | void show() // Overridden method of class equitri 64 | { 65 | cout << "The equilateral triangle has 3 sides of length " << lside << "units " << endl; 66 | } 67 | void showareaperi() // Method of class square 68 | { 69 | cout << "The area and perimeter of equilateral triangle are " << area << "sq units and" << peri << " units respectively " << endl; 70 | } 71 | }; 72 | 73 | int main() 74 | { 75 | cout << "Regular polygon r" << endl; 76 | regpoly r(6, 7); 77 | r.show(); 78 | cout << endl<< "Square" << endl; 79 | square s(5); 80 | s.show(); 81 | s.calarea(); 82 | s.calperi(); 83 | s.showareaperi(); 84 | cout << endl<< "Equilateral triangle" << endl; 85 | equitri t(4); 86 | t.show(); 87 | t.calarea(); 88 | t.calperi(); 89 | t.showareaperi(); 90 | return 0; 91 | } -------------------------------------------------------------------------------- /c++/InitializationFinalization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class regpoly 5 | { 6 | private: 7 | int nsides; 8 | 9 | protected: 10 | int lside; 11 | 12 | public: 13 | // Constructor overloading 14 | /* 15 | regpoly() // default constructor 16 | { 17 | } 18 | 19 | */ 20 | 21 | regpoly(int n, int l) // constructor using this pointer 22 | { 23 | this->nsides = n; 24 | this->lside = l; 25 | } 26 | 27 | regpoly() // Interactive constructor 28 | { 29 | cout << "Enter number of sides and length of side of regular polygon" << endl; 30 | cin >> nsides >> lside; 31 | } 32 | 33 | regpoly(regpoly &p) // Copy Constructor 34 | { 35 | nsides = p.nsides; 36 | lside = p.lside; 37 | } 38 | 39 | void show() 40 | { 41 | cout << "The regular polygon has " << nsides << " sides of length " << lside << " units" << endl; 42 | } 43 | 44 | //In java Garbage collector uses finalize() method of class object by default. 45 | ~regpoly() // Destructor Finalization 46 | { 47 | cout << "Destructor called" << endl; 48 | } 49 | }; 50 | 51 | int main() 52 | { 53 | cout << "Interactive constructor r" << endl; 54 | regpoly r; 55 | r.show(); 56 | 57 | cout << "Parameterised constructor s" << endl; 58 | regpoly s(3, 4); 59 | s.show(); 60 | 61 | cout << "Copied object s in t" << endl; 62 | regpoly t(s); 63 | s.show(); 64 | 65 | //Destructor is called total 3 time 3 object by default at end 66 | 67 | //We can call it explicitly too for a object 68 | cout<< "Explicit Destructor call for s" << endl; 69 | s.regpoly::~regpoly(); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /c++/MethodOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class addovr 6 | { 7 | public: 8 | char r[10]; 9 | //same name different paramter taking and return type 10 | int add(int x, int y) { return (x + y); } 11 | 12 | double add(double x, double y) { return (x + y); } 13 | 14 | //string concat 15 | char *add(char *x, char *y) 16 | { 17 | int l = 0; 18 | 19 | while (*x != '\0'){ 20 | r[l++] = *x++; 21 | } 22 | 23 | while (*y != '\0'){ 24 | r[l++] = *y++; 25 | } 26 | 27 | r[l] = '\0'; 28 | return (r); 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | addovr a; // default constructor is added by compiler 35 | char c[5] = {'p', 'i', 'k', '\0', ' '}, d[5] = {'a', 'c', 'h', 'u', '\0'}; 36 | 37 | cout << "Integer Addition " << a.add(4, 3) << endl; 38 | cout << "Floating point Addition " << a.add(4.3, 3.3) << endl; 39 | cout << "String Concatenation " << a.add(c, d) << endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /c++/MultiLevel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | public: 7 | void func1() 8 | { 9 | cout << "Inside Function 1: " << endl; 10 | } 11 | }; 12 | 13 | class B : public A 14 | { 15 | public: 16 | void func2() 17 | { 18 | cout << "Inside Function 2: " << endl; 19 | } 20 | }; 21 | 22 | class C : public B 23 | { 24 | public: 25 | void func3() 26 | { 27 | cout << "Inside Function 3: " << endl; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | A obj1; 34 | obj1.func1(); 35 | 36 | B obj2; 37 | obj2.func1(); 38 | obj2.func2(); 39 | 40 | C obj3; 41 | obj3.func2(); 42 | obj3.func3(); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /c++/Threading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void task1(int n) 7 | { 8 | int i; 9 | for (i = 1; i < 11; i++) 10 | { 11 | sleep(1); 12 | cout << n << "*" << i << "=" << (i * n) << endl; 13 | } 14 | } 15 | 16 | void task2() 17 | { 18 | for (char c = 'A'; c <= 'Z'; c++) 19 | { 20 | sleep(2); 21 | cout << c << endl; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int num; 28 | cout << "Enter a number" << endl; 29 | cin >> num; 30 | 31 | thread t1(task1, num); 32 | thread t2(task2); 33 | 34 | t1.join(); 35 | t2.join(); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /c++/encapsulation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Students 5 | { 6 | private: 7 | string studentName; 8 | int rollNumber; 9 | int age; 10 | 11 | public: 12 | string getstudentName() 13 | { 14 | return studentName; 15 | } 16 | 17 | void setstudentName(string studentName) 18 | { 19 | this->studentName = studentName; 20 | } 21 | 22 | int getrollNumber() 23 | { 24 | return rollNumber; 25 | } 26 | 27 | void setrollNumber(int rollNumber) 28 | { 29 | this->rollNumber = rollNumber; 30 | } 31 | 32 | int getage() 33 | { 34 | return age; 35 | } 36 | void setage(int age) 37 | { 38 | this->age = age; 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | Students obj; 45 | obj.setstudentName("Sarah"); 46 | obj.setrollNumber(54); 47 | obj.setage(20); 48 | cout << "Student Name: " << obj.getstudentName() << endl; 49 | cout << "Student Roll No: " << obj.getrollNumber() << endl; 50 | cout << "Student Age: " << obj.getage() << endl; 51 | } 52 | -------------------------------------------------------------------------------- /c++/inheritance.cpp: -------------------------------------------------------------------------------- 1 | //inheritance 2 | /* inheritance is a process of inheriting properties and behaviours of existing class into new class. 3 | Can be defined as IS-A relationship(parent-child relationship) . 4 | eg Remember men and women class inherits from human. 5 | Advantages-1.write less code, code reusability 6 | Protected class is similar like private class except the child/derived class can access*/ 7 | #include 8 | using namespace std; 9 | //super/base class 10 | class Human{ 11 | public: 12 | int height; 13 | int weight; 14 | int age; 15 | 16 | public: 17 | int getage(){ 18 | return this -> age;; 19 | } 20 | 21 | void setweight(int w){ 22 | this -> weight=w; 23 | } 24 | }; 25 | //subclass 26 | class Male: public Human{ 27 | public: 28 | string color; 29 | 30 | void sleep(){ 31 | cout<<"Male sleeping"<height; 36 | // } 37 | 38 | int getheight(){ 39 | return this -> height; 40 | } 41 | }; 42 | class Rashid: public Male{ 43 | 44 | }; 45 | 46 | int main(){ 47 | Male object1; 48 | cout< 3 | using namespace std; 4 | 5 | class Animal 6 | { 7 | public: 8 | string color; 9 | 10 | public: 11 | void bark() 12 | { 13 | cout << "barking" << endl; 14 | } 15 | }; 16 | 17 | class Human 18 | { 19 | public: 20 | int height; 21 | int weight; 22 | int age; 23 | 24 | public: 25 | void speak() 26 | { 27 | 28 | cout << "speaking" << endl; 29 | } 30 | }; 31 | 32 | class Hybridd : public Animal, public Human 33 | { 34 | }; 35 | 36 | int main() 37 | { 38 | Hybridd obj1; 39 | obj1.speak(); 40 | obj1.bark(); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /c++/source.md: -------------------------------------------------------------------------------- 1 | 1) You can refer below for inheritance types theory
2 | https://www.javatpoint.com/cpp-inheritance 3 | -------------------------------------------------------------------------------- /c++/virtualInheritance.cpp: -------------------------------------------------------------------------------- 1 | // virtual inheritance ensuring single occurance of l in square 2 | // if virtual is not written , it creates ambiguity for l 3 | 4 | #include 5 | using namespace std; 6 | 7 | class twod 8 | { 9 | protected: 10 | int l; 11 | 12 | public: 13 | twod() {} 14 | void show() { cout << "This is 2D object" << endl; } 15 | }; 16 | 17 | class rectangle : virtual public twod 18 | { 19 | private: 20 | int b; 21 | 22 | public: 23 | rectangle() {} 24 | rectangle(int x, int y) 25 | { 26 | l = x; 27 | b = y; 28 | } 29 | void show() { cout << "The length and breadth of rectange are " << l << " and " << b << endl; } 30 | }; 31 | 32 | class rombus : virtual public twod 33 | { 34 | private: 35 | int ang; 36 | 37 | public: 38 | rombus() {} 39 | rombus(int a, int b) 40 | { 41 | l = a; 42 | ang = b; 43 | } 44 | void show() { cout << "The length and angle of rombus are " << l << " and " << ang << endl; } 45 | }; 46 | 47 | class square : public rectangle, public rombus 48 | { 49 | public: 50 | square(int a) { l = a; } 51 | void show() { cout << "The square has side of length " << l << endl; } 52 | }; 53 | 54 | int main() 55 | { 56 | cout << "Rectangle" << endl; 57 | rectangle p(3, 4); 58 | p.show(); 59 | cout << endl; 60 | 61 | cout << "Rombus" << endl; 62 | rombus q(5, 30); 63 | q.show(); 64 | cout << endl; 65 | 66 | cout << "square" << endl; 67 | square r(6); 68 | r.show(); 69 | cout << endl; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /c/CallValueReference.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swapp(int x, int y) 4 | { 5 | int temp; 6 | temp = x; 7 | x = y; 8 | y = temp; 9 | } 10 | 11 | void swap(int *x, int *y) 12 | { 13 | int temp; 14 | temp = *x; 15 | *x = *y; 16 | *y = temp; 17 | } 18 | 19 | int main() 20 | { 21 | int r = 10, v = 20; 22 | 23 | printf("\nCall by value function"); 24 | swapp(r, v); // passing value to function 25 | printf("\nValue of r after value call: %d", r); 26 | printf("\nValue of v after value call: %d", v); 27 | 28 | printf("\n\nCall by reference function"); 29 | swap(&r, &v); // passing address to function 30 | printf("\nValue of r after reeference call: %d", r); 31 | printf("\nValue of v after reeference call: %d", v); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /c/GlobalLocal.c: -------------------------------------------------------------------------------- 1 | #include 2 | // a,b,c,d,e are global variables 3 | int a = 10; 4 | int b = 20; 5 | char c = 'A'; 6 | float d = 13.5; 7 | float e; 8 | void scope() 9 | { 10 | // same name as of main function variable but different datatype 11 | char l = 'M'; 12 | float m = 3.5; 13 | int q = 4; 14 | printf("Performing operations on local variables of scope function\n"); 15 | printf("Character in variable l is :%c\n", l); 16 | printf("Product of m and q is :%f\n", m * q); 17 | } 18 | 19 | int main() 20 | { 21 | // l,m,p,q are local variable of main function 22 | int l = 20, m = 40; 23 | float p = 24.5; 24 | float q; 25 | 26 | { 27 | float l=20.3; 28 | float m=3.2; 29 | printf("Performing operations on local variables of block\n"); 30 | printf("Addition of l and m is :%f\n", l + m); 31 | } 32 | 33 | printf("Performing operations on local variables of main function\n"); 34 | printf("Addition of l and m is :%d\n", l + m); 35 | q = l + p; 36 | printf("Addition of p and l is :%f\n", q); 37 | printf("Performing operations on global variables \n"); 38 | printf("Addition of a and b is :%d\n", a + b); 39 | printf("The character stored in C is :%c\n", c); 40 | e = b + d; 41 | printf("Addition of b and d is :%f\n", e); 42 | 43 | scope(); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /c/MergeSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printArray(int *A, int n) 4 | { 5 | for (int i = 0; i < n; i++) 6 | { 7 | printf("%d ", A[i]); 8 | } 9 | printf("\n"); 10 | } 11 | 12 | void merge(int A[], int mid, int low, int high) 13 | { 14 | int i, j, k, B[100]; // B[high+1] 15 | i = low; 16 | j = mid + 1; 17 | k = low; 18 | 19 | // next array reach k pehle tak 20 | while (i <= mid && j <= high) 21 | { 22 | // dono array ka element compare kaunsa chota h 23 | if (A[i] < A[j]) 24 | { 25 | B[k] = A[i]; 26 | i++; // i ka chota toh i++ varna vice versa 27 | k++; 28 | } 29 | 30 | // idhar j ka chota h 31 | else 32 | { 33 | B[k] = A[j]; 34 | j++; 35 | k++; 36 | } 37 | } 38 | 39 | // agar 1 array khtm dusra nahi 40 | // toh remaining elements print krvane function 41 | while (i <= mid) 42 | { 43 | B[k] = A[i]; 44 | k++; 45 | i++; 46 | } 47 | 48 | // same upar ka comment 49 | while (j <= high) 50 | { 51 | B[k] = A[j]; 52 | k++; 53 | j++; 54 | } 55 | 56 | // B ke saare element A mai copy kr lo sort mil jaye taki 57 | for (int i = low; i <= high; i++) 58 | { 59 | A[i] = B[i]; 60 | } 61 | } 62 | 63 | void mergeSort(int A[], int low, int high) 64 | { 65 | int mid; 66 | if (low < high) 67 | { 68 | // middle 69 | mid = (low + high) / 2; 70 | 71 | // aadha aadha sort kro fhir merge kro 72 | mergeSort(A, low, mid); 73 | mergeSort(A, mid + 1, high); 74 | merge(A, mid, low, high); 75 | } 76 | } 77 | 78 | int main() 79 | { 80 | // int A[] = {9, 14, 4, 8, 7, 5, 6}; 81 | int A[] = {9, 1, 4, 14, 4, 15, 6}; 82 | int n = 7; 83 | printArray(A, n); 84 | mergeSort(A, 0, 6); 85 | printArray(A, n); 86 | return 0; 87 | } -------------------------------------------------------------------------------- /haskell/Arithemetic.hs: -------------------------------------------------------------------------------- 1 | -- evaluate following 2 | solve n = ((n*n)+(4*n)-5) 3 | 4 | main = do putStrLn "Enter value of x : " 5 | xin <- getLine 6 | let x = read xin::Int 7 | print(solve x) 8 | -------------------------------------------------------------------------------- /haskell/Factorial.hs: -------------------------------------------------------------------------------- 1 | fact :: Int->Int 2 | fact n|n==0=1 3 | fact n|n/=0=n*fact(n-1) 4 | 5 | main = do 6 | putStrLn "Enter number:" 7 | xin <- getLine 8 | let a = (read xin::Int) 9 | let c = fact a 10 | putStr "Factorial is :" 11 | print(c) -------------------------------------------------------------------------------- /haskell/HigherOrder.hs: -------------------------------------------------------------------------------- 1 | -- Repeating a particular function twice 2 | --reapply is not a keyword 3 | 4 | -- reapply::(a->a)->a->a 5 | reapply f x=f(f x) 6 | square x=x*x 7 | double x=x*2 8 | half x=x/2 9 | 10 | main=do 11 | putStrLn"Enter number :" 12 | ain<-getLine 13 | let a=(read ain) 14 | print(reapply square a) 15 | print(reapply double a) 16 | print(reapply half a) 17 | -------------------------------------------------------------------------------- /haskell/ListOperations.hs: -------------------------------------------------------------------------------- 1 | --Takes a list input and gives output as list 2 | 3 | double[]=[] 4 | -- here x is head xs is remaining elements 5 | --after performing it calls same function with tail elements 6 | 7 | double (x:xs) =[x+x] ++double(xs) 8 | main = do 9 | putStrLn"Enter 1st num:" 10 | pin<-getLine 11 | let p=(read pin ::Int) 12 | 13 | putStrLn"Enter 2nd num:" 14 | iin<-getLine 15 | let i=(read iin :: Int) 16 | putStrLn"Enter 3rd num:" 17 | kin<-getLine 18 | let k=(read kin :: Int) 19 | putStrLn"Enter 4th num:" 20 | ain<-getLine 21 | let a=(read ain :: Int) 22 | 23 | --Here pikachu is name of list 24 | let pikachu=[p,i,k,a] 25 | print(double pikachu) 26 | -------------------------------------------------------------------------------- /haskell/Map.hs: -------------------------------------------------------------------------------- 1 | -- higher order functions 2 | 3 | main = do 4 | print(map (*3)[1,4,6]) 5 | print(map (+2)[1,4,6]) 6 | 7 | --Above,map is a inbuilt function for direct calculation 8 | 9 | --Lets make a user defined function for operations on list 10 | 11 | mapf::(a->b)->[a]->[b] 12 | mapf f xs=[f x | x <-xs] 13 | 14 | main = do 15 | print(mapf (*3)[1,4,6]) 16 | print(mapf (/2)[2,4,6]) -------------------------------------------------------------------------------- /haskell/MultiplyList.hs: -------------------------------------------------------------------------------- 1 | --Takes two list input and gives output as list 2 | 3 | mult [][]=[] 4 | mult (x:xs)(y:ys) =[x*y] ++ mult xs ys 5 | 6 | -- here x is head xs is remaining elements,similarly y 7 | --after performing it calls same function with tail elements 8 | 9 | main = do 10 | putStrLn"List1" 11 | putStrLn"Enter 1st num:" 12 | pin<-getLine 13 | let p=(read pin ::Int) 14 | 15 | putStrLn"Enter 2nd num:" 16 | iin<-getLine 17 | let i=(read iin :: Int) 18 | putStrLn"Enter 3rd num:" 19 | kin<-getLine 20 | let k=(read kin :: Int) 21 | 22 | let list1=[p,i,k] 23 | 24 | putStrLn"List2" 25 | putStrLn ("Enter first number: ") 26 | ain <- getLine 27 | let a = (read ain :: Int) 28 | putStrLn ("Enter second number: ") 29 | bin <- getLine 30 | let b = (read bin :: Int) 31 | putStrLn ("Enter third number: ") 32 | cin <- getLine 33 | let c = (read cin :: Int) 34 | 35 | let list2=[a,b,c] 36 | 37 | print(mult list1 list2) 38 | -------------------------------------------------------------------------------- /haskell/Recursive.hs: -------------------------------------------------------------------------------- 1 | --Fibonacci Series print 2 | -- 1) input is int and returns int 3 | fib :: Int -> Int 4 | fib 0 = 0 5 | fib 1 = 1 6 | fib n = fib (n-1) + fib (n - 2) 7 | 8 | --works like array to store multiple values 9 | fibList n = map fib[0..n] 10 | 11 | main = 12 | do 13 | putStrLn "Enter number of fibo : " 14 | x <- readLn 15 | print (fibList x) 16 | 17 | 18 | -------------------------- 19 | 20 | -- Merge Sort using recursion 21 | -- mergIst::[Int]->[Int]->[Int] 22 | 23 | mergIst [][]z = reverse z 24 | mergIst (x:xs)[] z= mergIst xs [] (x:z) 25 | mergIst[] (y:ys) z= mergIst [] ys (y:z) 26 | mergIst (x:xs) (y:ys) z= if x<=y then mergIst xs (y:ys) (x:z) else mergIst (x:xs) ys (y:z) 27 | 28 | main = print(mergIst[1,4,8,20][18,0,6,9,30][]) 29 | 30 | -------------------------------------------------------------------------------- /haskell/Sum2N.hs: -------------------------------------------------------------------------------- 1 | summ :: Int -> Int 2 | summ n| n==1=1 3 | summ n| n/=1=n+summ(n-1) 4 | main = do 5 | putStrLn "Enter number :" 6 | xin <- getLine 7 | let a = (read xin :: Int) 8 | let v = summ a 9 | putStr "Sum of values are :" 10 | print(v) -------------------------------------------------------------------------------- /haskell/caseSwitch.hs: -------------------------------------------------------------------------------- 1 | main = do 2 | let grade = "f" 3 | 4 | --switch case 5 | case grade of 6 | "a" -> putStrLn "best" 7 | "b" -> putStrLn "better" 8 | "c" -> putStrLn "good" 9 | "d" -> putStrLn "ok" 10 | --default condition 11 | _ -> putStrLn "invalid failed" -------------------------------------------------------------------------------- /haskell/function1.hs: -------------------------------------------------------------------------------- 1 | -- fun :: IO() 2 | fun = do 3 | putStrLn "Hello pikachu" 4 | putStrLn "heyy" 5 | 6 | main :: IO () 7 | main = do 8 | putStrLn "hii" 9 | fun 10 | putStrLn "end" -------------------------------------------------------------------------------- /haskell/function2.hs: -------------------------------------------------------------------------------- 1 | --taking string as input and printing something on console 2 | sayHi ::String -> IO() 3 | sayHi name = do 4 | putStrLn ("Hello "++ name) 5 | putStrLn " heyy" 6 | 7 | 8 | --taking string then int as input and printing something on console 9 | sayHey ::String ->Int -> IO() 10 | sayHey name age = do 11 | putStrLn ("Heyy bro "++ name) 12 | putStrLn (show age ++ " babz") 13 | 14 | main :: IO () 15 | main = do 16 | putStrLn "hii" 17 | 18 | --giving string input (name) 19 | sayHi "altaf" 20 | sayHey "pikachu" 18 21 | 22 | putStrLn "end" -------------------------------------------------------------------------------- /haskell/function3.hs: -------------------------------------------------------------------------------- 1 | -- int passed int returned 2 | cube :: Int -> Int 3 | cube num = num*num*num 4 | 5 | sayHi :: String->String 6 | sayHi name ="hello " ++ name 7 | 8 | main = do 9 | let a=3 10 | print(cube a) 11 | let b= sayHi "altaf" 12 | putStrLn (b) -------------------------------------------------------------------------------- /haskell/if-else.hs: -------------------------------------------------------------------------------- 1 | work :: String-> IO() 2 | work name =do 3 | if name =="sunday" 4 | then putStrLn "holidayyy" 5 | else if name =="saturday" 6 | then putStrLn "partial holiday" 7 | else putStrLn "work boring" 8 | 9 | main = do 10 | work "saturday" 11 | 12 | -- similarly we can use && || things and bool -------------------------------------------------------------------------------- /haskell/list.hs: -------------------------------------------------------------------------------- 1 | -- lists like arrays same index 2 | --list of int type 3 | scores :: [Int] 4 | scores = [17,23,32,1,5] 5 | main =do 6 | -- score at 0 index 7 | print (scores !! 0) 8 | --output in same format square brack 9 | print(scores) 10 | 11 | -- score at head index 12 | print (head scores) 13 | -- score at last index 14 | print (last scores) 15 | -- score at tail everything except head 16 | print (tail scores) 17 | -- score at init everything except head and tail 18 | print (init scores) -------------------------------------------------------------------------------- /haskell/loop.hs: -------------------------------------------------------------------------------- 1 | printto100 :: Int -> IO() 2 | 3 | --this will run once only hence lil changes needed 4 | -- printto100 num = do 5 | -- if num<=100 6 | -- then print(num) 7 | -- else putStrLn "complete loop" 8 | 9 | --loop using recursion 10 | printto100 num = do 11 | if num<=100 12 | then do 13 | print(num) 14 | printto100 (num+1) 15 | else putStrLn "complete loop" 16 | 17 | main = do 18 | printto100 1 -------------------------------------------------------------------------------- /haskell/operator.hs: -------------------------------------------------------------------------------- 1 | main =do 2 | print(5+6) 3 | print(6-2) 4 | print(6*2) 5 | -- floating 6 | print(6/3) 7 | --3 raised to 2 8 | print(3**2) 9 | print(sqrt 36) 10 | print(round 3.9) 11 | -- similarly fllor ceiling -------------------------------------------------------------------------------- /haskell/record.hs: -------------------------------------------------------------------------------- 1 | --records just like struct any datatypes 2 | data Student = Student { 3 | name ::String , age :: Int , gpa :: Double 4 | }deriving Show 5 | 6 | main = do 7 | let student2 = Student { 8 | name="altaf ", age=19,gpa=9.67 9 | } 10 | let student1 = Student { 11 | name="sarah ", age=19,gpa=9.50 12 | } 13 | --similarly we can make multiple students 14 | print student1 15 | 16 | --finding something inside this 17 | putStrLn(name student1) 18 | print(gpa student1) -------------------------------------------------------------------------------- /haskell/userInput.hs: -------------------------------------------------------------------------------- 1 | main =do 2 | putStrLn "enter name" 3 | name <- getLine 4 | putStrLn "age" 5 | cage <- getLine 6 | let age = read cage::Int 7 | putStr(name) 8 | print (age) -------------------------------------------------------------------------------- /haskell/variables.hs: -------------------------------------------------------------------------------- 1 | -- out of main function variable declare 2 | -- a ::String --optional when reqd 3 | -- a="altaf" 4 | 5 | main = do 6 | let a="altaf" 7 | -- concatenation string ++ used 8 | putStrLn (a ++ " hello piko " ++ a ++ " pikachu") 9 | putStrLn a 10 | putStrLn " " 11 | 12 | let b = 28 13 | -- print gives line break 14 | print b 15 | putStrLn(show b) 16 | -- concatenation string and number 17 | putStrLn (show b ++ " hello piko " ++show b ++ " pikachu") -------------------------------------------------------------------------------- /java/DestructorExample.java: -------------------------------------------------------------------------------- 1 | public class DestructorExample { 2 | public static void main(String[] args) { 3 | DestructorExample de = new DestructorExample(); 4 | de.finalize(); 5 | de = null; 6 | System.gc(); 7 | System.out.println("Inside the main() method"); 8 | } 9 | protected void finalize() { 10 | System.out.println("Object is destroyed by the Garbage Collector"); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /java/ExceptionHandling.java: -------------------------------------------------------------------------------- 1 | public class ExceptionHandling { 2 | public static void main(String args[]) { 3 | int a = 10; 4 | int b = 0; 5 | try { 6 | // code that may raise exception 7 | int c = a / b; 8 | System.out.println("division is :"+c); 9 | } catch (Exception e) { 10 | System.out.println(e); 11 | } 12 | // rest code of the program 13 | System.out.println("rest of program"); 14 | } 15 | } -------------------------------------------------------------------------------- /java/Thread1.java: -------------------------------------------------------------------------------- 1 | import java.lang.*; 2 | 3 | //within a process for multitasking,faster execution 4 | //chat kr rha kabhi aur cooking kabhi not simultaneously 5 | //concurrently both threads starts running 6 | //2 ways - extend Thread ,implement Runnable 7 | 8 | class Mythread1 extends Thread{ 9 | //overriding thread.java 10 | @Override 11 | public void run() { 12 | int i=1; 13 | while(i<13) { 14 | System.out.println("4 * "+ i +"= "+4*i); 15 | i++; 16 | try { 17 | Thread.sleep(100); 18 | } catch (InterruptedException e) { 19 | throw new RuntimeException(e); 20 | } 21 | } 22 | } 23 | } 24 | 25 | class Mythread2 extends Thread{ 26 | @Override 27 | public void run() { 28 | char c; 29 | 30 | for(c = 'A'; c <= 'Z'; ++c) 31 | System.out.println(c + " "); 32 | try { 33 | Thread.sleep(100); 34 | } catch (InterruptedException e) { 35 | throw new RuntimeException(e); 36 | } 37 | } 38 | } 39 | 40 | public class Thread1 { 41 | public static void main(String[] args) { 42 | 43 | Mythread1 t1 = new Mythread1(); 44 | Mythread2 t2 = new Mythread2(); 45 | //start is used to run thread 46 | t1.start(); 47 | t2.start(); 48 | } 49 | } -------------------------------------------------------------------------------- /java/Thread2.java: -------------------------------------------------------------------------------- 1 | import java.lang.*; 2 | 3 | class Mythread1 implements Runnable{ 4 | 5 | public void run() { 6 | int i=1; 7 | while(i<13) { 8 | System.out.println("4 * "+ i +"= "+4*i); 9 | i++; 10 | try { 11 | Thread.sleep(100); 12 | } catch (InterruptedException e) { 13 | throw new RuntimeException(e); 14 | } 15 | } 16 | } 17 | } 18 | 19 | class Mythread2 implements Runnable{ 20 | 21 | public void run() { 22 | char c; 23 | 24 | for(c = 'A'; c <= 'Z'; ++c) 25 | System.out.println(c + " "); 26 | try { 27 | Thread.sleep(100); 28 | } catch (InterruptedException e) { 29 | throw new RuntimeException(e); 30 | } 31 | } 32 | } 33 | 34 | public class Thread2 { 35 | public static void main(String[] args) { 36 | //runnable doesnt runn directly 37 | //bullet banao fhir gun mai bullet dalo 38 | Mythread1 r1 = new Mythread1(); 39 | //method taking runnable input 40 | Thread t1 = new Thread(r1); 41 | 42 | Mythread2 r2 = new Mythread2(); 43 | Thread t2 = new Thread(r2); 44 | 45 | t1.start(); 46 | t2.start(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /java/mergeSort.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | // void mergesort(int a[],int low,int high); 5 | // void printarray(int a[] , int n); 6 | // void merge(int a[],int mid,int low,int high); 7 | 8 | public void Main(int a[],int low ,int high) 9 | { 10 | int mid; 11 | if(low 2 | 3 | 4 | 5 |

JavaScript Regular Expressions

6 |

Do a global search for any of the specified alternatives for two symbol (!|@):

7 |

8 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 31 | 32 | -------------------------------------------------------------------------------- /javascript/PasswordValid.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Password Validation 5 | 6 | 7 | 8 |

Check lower & uppercase ,more than 8 word,min 1 number

9 |

Enter Password

10 | 11 |
12 | 13 | 14 |
15 | 16 | 34 | 35 | -------------------------------------------------------------------------------- /javascript/Vowel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Vowel Count 6 | 7 | 8 | 9 | 10 | 11 | 12 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /javascript/prac1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

JavaScript

6 |

When adding a number and a string, JavaScript will treat the number as a 7 | string and concatinate.

8 |

9 |

When adding 2 numbers, JavaScript will perform numeric addition.

10 |

11 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /javascript/prac2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

JavaScript Objects

6 |

7 |

8 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /javascript/prac4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

JavaScript Regular Expressions

6 |

Do a global search for digits in a string:

7 |

8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /javascript/readme.md: -------------------------------------------------------------------------------- 1 | Javascript completed from my side. 2 | -------------------------------------------------------------------------------- /prolog/Factorial.pl: -------------------------------------------------------------------------------- 1 | factorial(0,1). 2 | factorial(N,Ans) :- 3 | N>0, 4 | N1 is N-1, 5 | factorial(N1, V), 6 | Ans is N*V. 7 | 8 | # factorial(5,X). 9 | # X = 120 10 | -------------------------------------------------------------------------------- /prolog/Fibonacci.pl: -------------------------------------------------------------------------------- 1 | fibo(0,0). 2 | fibo(1,1). 3 | fibo(N,Ans):- 4 | N >1, 5 | N1 is N-1, 6 | N2 is N-2, 7 | fibo(N1,A1), 8 | fibo(N2,A2), 9 | Ans is A1+A2. 10 | 11 | # fibo(6,Ans). 12 | # Ans=8. -------------------------------------------------------------------------------- /prolog/MergeSort.pl: -------------------------------------------------------------------------------- 1 | merge(List, List, []). 2 | merge(List, [], List). 3 | merge([MinList1|RestMerged], [MinList1|RestList1], [MinList2|RestList2]) :- 4 | MinList1 =< MinList2, 5 | merge(RestMerged,RestList1,[MinList2|RestList2]). 6 | merge([MinList2|RestMerged], [MinList1|RestList1], [MinList2|RestList2]) :- 7 | MinList2 =< MinList1, 8 | merge(RestMerged,[MinList1|RestList1],RestList2). 9 | 10 | merge(What, [1, 3, 5, 6], [2, 4, 7]). 11 | -------------------------------------------------------------------------------- /prolog/Rectangle.pl: -------------------------------------------------------------------------------- 1 | rect:- 2 | write('enter length'), 3 | read(L), 4 | write('enter breadth'), 5 | read(B), 6 | A is L*B, 7 | P is 2*(L+B), 8 | write('area of rect is:'), 9 | write(A),nl, 10 | write('perimeter of rect is: '), 11 | write(P). 12 | 13 | # enter length 14 | # 3 15 | # enter breadth 16 | # 2 17 | # area of rect is:6 18 | # perimeter of rect is: 10 19 | # true 20 | --------------------------------------------------------------------------------