Operator Overloading in C++

#include<iostream>

using namespace std;


class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i = 0){

      real = r; imag = i;

}

Complex operator - (Complex const &obj) {

Complex res;

res.real = real - obj.real;

res.imag = imag - obj.imag;

return res;

}

Complex operator + (Complex const &obj) {

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

}

Complex operator * (Complex const &obj) {

Complex res;

res.real = real * obj.real;

res.imag = imag * obj.imag;

return res;

}

Complex operator / (Complex const &obj) {

Complex res;

res.real = real / obj.real;

res.imag = imag / obj.imag;

return res;

}

void print() {

      cout <<"Result is: "<< real << " + i" << imag << '\n';

}

};


int main()

{

Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;

c3.print();

Complex c4 = c1 - c2;

c4.print();

Complex c5 = c1 * c2;

c5.print();

Complex c6 = c1 / c2;

c6.print();

}


Function overriding in C++ (basics)

 #include <iostream>

#include <string>

using namespace std;

class student{

      public:

      void displaystu(){

            cout<<"Hi! My name is Vaibhav Yadav and I'm from ECE Department, First Year' 25"<<endl;

      }

};

class update: public student{

      public:

      void displaystu(){

            student::displaystu();

            cout<<"Details UPDATED"<<endl<<"Hi! My name is Vaibhav Yadav and I'm from 'IT' Department, First Year' 25"<<endl;

      }

};

int main()

{

    update up;

    up.displaystu();

    return 0;

}


Program for different levels inheritance is given here

 #include <iostream>

#include <string>

using namespace std;

class College{

      public:

      int Roll_no;

      string Name, Department;

      College(){

            cout<<"This is MMMUT Gorakhpur"<<endl;

      }

};

class student:public College{

      public:

      student(int R, string n, string d){

            Roll_no = R;

            Name = n;

            Department = d;

      }

      void display(){

            cout<<"Student Name is: "<<Name<<endl<<"Roll No: "<<Roll_no<<endl<<"Department: "<<Department<<endl;

      }

      

};





int main()

{

    student stu1(2021071042, "Vaibhav Yadav", "IT");

    stu1.display();

    student stu2(2021071060, "Ayush Verma", "IT");

    stu2.display();

    student stu3(2021071052, "Anurag Kumar Singh", "IT");

    stu3.display();

//     student stu;

    return 0;

}


Program to catch all type of Exceptions in C++

 #include <iostream>

using namespace std;

#include <string>

int main()

{

      string Name = "Vaibhav_Yadav", R_Name;

      int Roll = 2021071042, roll_no;

      cout<<"Authentication Please Verify Yourself"<<endl;

      try{

            cout<<"Enter Your Name: ";

            getline(cin, R_Name);

            cout<<"Enter Your Roll No: ";

            cin>>roll_no;

            if(R_Name == Name && Roll == roll_no){

                  cout<<"Login Success"<<endl;

            }else{

                  throw 401;

            }

      }

      catch(...){

            cout<<"Authentication failed"<<endl;

            cout<<"Access Not Allowed"<<endl;

      }


    return 0;

}


Input file Writing and Reading in C++

 #include <iostream>

#include  <bits/stdc++.h>

#include <fstream>

#include <string>

using namespace std;


int main()

{

      while(1){

            int ch;

      cout<<endl;

      cout<<"Enter Your Choice: "<<endl;

      cout<<"1. Write Into file"<<endl;

      cout<<"2. Read From The file"<<endl;

      cin>>ch;

      switch(ch){

            case 1:{

            string Inputline;

            cout<<"Enter What you want to insert to file"<<endl;

            cout<<"Default Input will be 'This is Vaibhav Yadav and today I'm learning file handling in c++ with fstream'"<<endl;

            // getline(cin, Inputline);

            Inputline = "This is Vaibhav Yadav and today I'm learning file handling in c++ with fstream";

            ofstream W_myfile("Name.txt");

            W_myfile<<Inputline;

            W_myfile.close();

            }

            break;

            

            case 2:    

            {

            string NameTxt;

            ifstream R_myfile("Name.txt");

            while(getline(R_myfile, NameTxt)){

            cout<<NameTxt;

            }

            R_myfile.close();

            }

            break;

            

            default:

            cout<<"Wrong Input Yrr :("<<endl;

            

      }

      }

    return 0;

}


Class Constructor and Destructor for this Class

 #include<iostream>

using namespace std;

class Student

{

string name;

public:

Student(){

    name = "Vaibhav Yadav";

    cout<<"Constructor of Student Class has been Called"<<endl;

    cout<<"Student Name is: "<<name<<endl;

}

~Student(){

    cout<<"---Destructor Of Student Class has been called---"<<endl;

    cout<<"---Memory for 'obj name' has been deallocated and memory has been released---"<<endl;

}

};


main()

{

Student stu;

return 0;

}


Program to demonstrate the friend function and friend class in c++

 #include<iostream>

using namespace std;

class Student

{

string Stu_name = "Vaibhav Yadav";

string branch = "Information Technology";

friend class MMMUT;

friend void studisplay(Student);

};

void studisplay(Student t){

    cout<<endl<<"Student branch is: "<<t.branch;

}

class MMMUT

{

public:

void display(Student t)

{

cout<<endl<<"Student Name is: "<<t.Stu_name;

}

};

main()

{

Student stu;

MMMUT clgstu;

clgstu.display(stu);

studisplay(stu);

return 0;

}


Employee Details Using Structure (c++)

 #include <iostream>

#include <string>

using namespace std;

struct Employee{

    int Emp_ID, age;

    string fname, lname, department;

    char sex;

};


int main() {

  struct Employee em;

  cout<<"Enter Your Employee_ID: ";

  cin>>em.Emp_ID;

  cout<<"Enter Your Employee Age: ";

  cin>>em.age;

  cout<<"Enter Your First Name and Last Name: ";

  cin>>em.fname>>em.lname;

  cout<<"Enter Employee Department: ";

  cin>>em.department;

  cout<<"Enter Your Sex ";

  cin>>em.sex;

  cout<<endl;

  cout<<"Employee ID is:"<<em.Emp_ID<<endl;

  cout<<"Employee Age is: "<<em.age<<endl;

  cout<<"Employee First Name is "<<em.fname<<endl<<"Employee Last Name is " <<em.lname<<endl;

  cout<<"Employee Department: "<<em.department<<endl;

  cout<<"Employee Sex is: "<<em.sex<<endl;

    return 0;

}

Fibonacci using recursion(c++)

 #include <iostream>

using namespace std;

int fibo(int n)

{

        if((n==1)||(n==0))

        {

                return(n);

        }

        else

        {

                return(fibo(n-1)+fibo(n-2));

        }

}

int main() {

    int n = 5,i=0;

    while(i<n){

        cout<<" "<<fibo(i);

        i++;

    }

    return 0;

}

Program to access Element in Array

 #include <iostream>

#define m 100

using namespace std;


int main()

{

    int arr[m][m],r,c,i,j;

    cout<<"Enter Your Number of Row and Column: ";

    cin>>r>>c;

    for(i=0;i<r;i++){

          for(j=0;j<c;j++){

                cout<<"Enter "<<i<<" Row "<<j<<" Column :";

                cin>>arr[i][j];

          }

    }

    for(i=0;i<r;i++){

    for(j=0;j<c;j++){

          cout<<arr[i][j]<<" ";

      }

    cout<<endl;

    }

    cout<<"Enter Your Row and Column and you want to access: ";

    cin>>r>>c;

    cout<<"Your Element is "<<arr[r][c];

    return 0;

}


Check Palindrome Using c++

 #include <iostream>


using namespace std;


int main()

{

    int n,num=0,r;

    cin>>n;

    int a = n;

    while(n>0){

         r = n%10;

         num = num*10 + r;

         n/=10;

    }

//     cout<<num;

    if(num==a)

    cout<<"Palindrome";

    else

    cout<<"Not Palindrome";

    cout<<endl;


    return 0;

}


Circular Queue Implementations Using Array

 #include <stdio.h>

#include <stdlib.h>

struct queue{

      int front,rear;

      int capacity;

      int size;

      int *array;

};

struct queue *create(int capacity){

      struct queue *q = malloc(sizeof(struct queue));

      if(!q){

            printf("Not enough Space\n");

            return NULL;

      }

      else{

            q->capacity = capacity;

            q->front = q->rear = -1;

            q->size = 0;

            q->array = malloc(sizeof(int)*q->capacity);

            if(!q){

                  printf("Not enough Space!!\n");

                  return NULL;

            }

            return q;

      }

}

int size(struct queue *q){

      return q->size;

}

int frontelement(struct queue *q){

      return q->array[q->front];

}

int rearelement(struct queue *q){

      return q->array[q->rear];

}

int fullqueue(struct queue *q){

      return q->size==q->capacity;

}

int emptyqueue(struct queue *q){

      return q->size==0;

}

void enqueue(struct queue *q,int data){

      if(fullqueue(q)){

            printf("Queue Overflow\n");

            return;

      }

      q->rear = (q->rear+1)%(q->capacity);

      q->array[q->rear] = data;

      if(q->front==-1){

            q->front=q->rear;

            q->size+=1;

      }

}

int dequeue(struct queue *q){

      if(emptyqueue(q)){

            printf("Queue is empty\n");

            return -1;

      }

      int data;

      data = q->array[q->front];

      if(q->front == q->rear){

            q->front = q->rear = -1;

            q->size = 0;

      }else{

            q->front = (q->front+1)%(q->capacity);

            q->size-=1;

      }

      return data;

}

void delete(struct queue *q){

      if(q){

            if(q->array)

            free(q->array);

            free(q);

      }

}


int main()

{

      int capacity = 5,i;

      struct queue *qu = create(capacity);

      for(i=0;i<capacity;i++){

            enqueue(qu,i);

      }

      printf("FrontElement is %d \n",frontelement(qu));

      printf("rearElement is %d \n",rearelement(qu));

    return 0;

}


Matrix Addition and Mulitplication Is given here

 #include <stdio.h>

#define max 100

int main() {

    int arr1[max][max],arr2[max][max],arr3[max][max],arr4[max][max];

    int k,i,j,n;

    printf("Enter Row and Column of Matrix(both Should be equal to perform the Operation): ");

    scanf("%d",&n);

    printf("Enter Your Array 1 Element\n");

    for(i=0;i<n;i++)

    for(j=0;j<n;j++){

        printf("Enter %d Row %d Column: ",i,j);

        scanf("%d",&arr1[i][j]);

    }

    printf("Enter Your Array 2 Element\n");

    for(i=0;i<n;i++)

    for(j=0;j<n;j++){

        printf("Enter %d Row %d Column: ",i,j);

        scanf("%d",&arr2[i][j]);

    }

    printf("\nYour Matrix 1 is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr1[i][j]);

        }

        printf("\n");

    }

    printf("Your Matrix 2 is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr2[i][j]);

        }

        printf("\n");

    }

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            arr3[i][j]+=arr1[i][j]+arr2[i][j];

        }

    }

    printf("\nYour Addition of Matrix is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr3[i][j]);

        }

        printf("\n");

    }

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            arr4[i][j]=0;

            for(k=0;k<n;k++)

            arr4[i][j]+=arr1[i][k]*arr2[k][j];

        }

    }

    printf("\nYour mutiplication of Matrix is \n");

    for(i=0;i<n;i++){

        for(j=0;j<n;j++){

            printf("%d ",arr4[i][j]);

        }

        printf("\n");

    }

    


    return 0;

}

Array Implementation of Stack (Using predefined Value)

// program to implement array

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

struct stack{

      int top;

      int capacity;

      int *array;

};

struct stack *create(int size){

      struct stack *s = (struct stack *)malloc(sizeof(struct stack));

      if(!s){

            printf("Not enough space\n");

            return NULL;

      }

      s->capacity = size;

      s->top = -1;

      s->array = malloc(s->capacity*sizeof(int));

      if(!s->array){

            printf("Not enough space\n");

            return NULL;

      }

      return s;

}

int isempty(struct stack *s){

      return (s->top==-1);

}

int isfull(struct stack *s){

      return (s->top==s->capacity-1);

}

int size(struct stack *s){

      return (s->top+1);

}

void push(struct stack *s,int data){

      if(isfull(s)){

            printf("stack overflow\n");

            return;

      }

      else

      s->array[++s->top]=data;

}

int pop(struct stack *s){

      if(isempty(s)){

            printf("Stack is underflow\n");

            return -1;

      }

      return s->array[s->top--];

}

int peek(struct stack *s){

      if(isempty(s)){

            printf("stack is Empty\n");

            return -1;

      }

      else

      return (s->array[s->top]);

}

int main()

{

      int capacity=15,i;

      struct stack *stk = create(capacity);

      for(i=0;i<capacity;i++){

            push(stk,i);

      }

      printf("Top element in stack %d \n",peek(stk));

      printf("Deleted element in stack %d \n",pop(stk));

      printf("Top element in stack %d \n",peek(stk));

      printf("No of Element in Stack %d \n",size(stk));


    return 0;

}


Program to find the last index of an element of array using recursive function.

 // coded by Vaibhav Yadav


#include <iostream>

#define ll long long

ll index(ll a[],ll size,ll x);

using namespace std;

int main() {

    ll n,i,x;

    cout<<"Enter Your Number of Elements in array: ";

    cin>>n;

    ll a[n];

    for(i=0;i<n;i++){

        cout<<"Enter Your Element no "<<i+1<<": ";

        cin>>a[i];

    }

    cout<<"Enter the element you want to search for: ";

    cin>>x;

    if(index(a,n,x)>=0)

    cout<<"Element Is found at position "<<index(a,n,x)<<endl;

    else

    cout<<"Element is not found"<<endl;

    return 0;

}

ll index(ll a[], ll size, ll x){

    if(a[size-1]==x)

    return size-1;

    else

    index(a,size-1,x);

}

Singly Linked List Operation.

 // creation of singly linked lists

// coded by Vaibhav Yadav

#include<stdio.h>

#include<stdlib.h>

// Declaring the structure that will contain the info part only and linked List

struct node

{

    int info;

    struct node *link;

};

struct node *create_list(struct node *start);

struct node *addat_beg(struct node *start,int data);

struct node *addat_end(struct node *start,int data);

void display(struct node *start);

void search(struct node *start,int item);

struct node *reverse(struct node *start);

struct node *delete(struct node *start,int item);

struct node *addat_after(struct node *start,int data,int item);

struct node *addat_before(struct node *start, int data ,int item);


int main(void){

    struct node *start  = NULL;

    int choice,data,item,pos;

    while(1){

        printf("\n");

        printf("====================================\n");

        printf("Enter Your Operation From the below\n");

        printf("1.Create Linked  List\n");

        printf("2.Reversal of Linked List\n");

        printf("3.Searching of an element\n");

        printf("4.Delete an element\n");

        printf("5.Add at after the data\n");

        printf("6.Add at before the data\n");

        printf("7.Display the data\n");

        

        printf("Enter Your choice: ");

        scanf("%d",&choice);

        printf("\n");

        

        switch(choice){

            case 1: start = create_list(start);

                    break;

            case 2: start = reverse(start);

                    break;

            case 3: printf("Enter the element you want to search for\n");

                    scanf("%d",&item);

                    search(start,item);

                    break;

            case 4: printf("Enter the item you want to delete\n");

                    scanf("%d",&item);

                    start = delete(start,item);

                    break;

            case 5: printf("Enter the item you want to insert\n");

                    scanf("%d",&item);

                    printf("which data you want to insert after\n");

                    scanf("%d",&data);

                    start = addat_after(start,data,item);

                    break;

            case 6: printf("Enter the item you want to insert\n");

                    scanf("%d",&item);

                    printf("Enter data you want to enter before\n");

                    scanf("%d",&data);

                    start = addat_before(start,data,item);

                    break;

            case 7: display(start);

                    break;

            default:

            printf("Wrong Choice Bro\n");

        }

    }

    return 0 ;

}

// Definition of all the function starts from here 

// This Funciton will add the element at beginning of the List

struct node *addat_beg(struct node *start, int data){

    struct node *tmp;

    tmp = (struct node *)malloc(sizeof(struct node));

    tmp -> info = data;

    tmp -> link = start;

    start = tmp;

    return start;

} // end of beginning of the List


// this function will add the element at the end of List

struct node *addat_end(struct node *start, int data){

    struct node *tmp,*p;

    p = start;

    tmp = (struct node *)malloc(sizeof(struct node));

    tmp -> info = data;

    while(p->link!=NULL)

    p = p -> link;

    p->link = tmp;

    tmp -> link = NULL;

    return start;

} // end of the function


// this funciton will displya the content of the page in the linke List

void display(struct node *start){

    int count = 0;

    struct node *p;

    if(start == NULL){

        printf("linked list is empty\n");

        return;

    }

    p = start;

    printf("List is : \n ");

    while(p!=NULL){

        printf("%d  ",p->info);

        p = p->link;

        count++;

    }

    printf("\nIt contain total of %d element\n",count);

    printf("\n\n");

} //end of the function

// Now this function will create a linked List

struct node *create_list(struct node *start){

    int data,i,n;

    printf("Enter Your the number of nodes\n");

    scanf("%d",&n);

    start = NULL;

    if(n==0){

        return start;

    }

    printf("Enter the 1 element to be inserted : ");

    scanf("%d",&data);

    start = addat_beg(start,data);

    for(i=2;i<=n;i++){

        printf("\nEnter the %d element to be inserted: ",i);

        scanf("%d",&data);

        start = addat_end(start,data);

    }

    display(start);

    return start;

} // end of create_list


// this function will reverse the linked List

struct node *reverse(struct node *start){

    struct node *prev, *ptr, *next;

    prev = NULL;

    ptr = start;

    while(ptr!=NULL){

        next = ptr->link;

        ptr->link=prev;

        prev = ptr;

        ptr = next;

    }

    start = prev;

    display(start);

    return start;

} // end of reverse function


// this funcion will search the linked list for an item in item

void search(struct node *start, int item){

    struct node *p;

    if(start == NULL){

        printf("List is empty");

        return;

    }

    int pos = 1;

    p=start;

    while(p!=NULL){

        if(p->info==item){

            printf("Element is found at position %d: \n",pos);

            return;

        }

        p=p->link;

        pos++;

    }

    printf("Element is not found in the linked list\n");

} //end of search function 


// this function will delete the element from the linked list

struct node *delete(struct node *start, int item){

    struct node *p,*tmp;

    if(start==NULL){

        printf("List is empty no operation can be performed here\n");

        return start;

    }

    // check wheather element is not the first element of the List if first then delete the node exactly here

    if(start->info==item){

        tmp = start;

        start = tmp->link;

        free(tmp);

        return start;

    }

    p = start;

    while(p!=NULL){

        if(p->link->info == item){

            tmp = p->link;

            p->link = tmp->link;

            free(tmp);

            display(start);

            return start;

        }

        p=p->link;

    }

    printf("Element is not found at the list\n");

    return start;

} // end of delete function 



// this funciton will add item before a data

struct node *addat_after(struct node *start,int data, int item){

    struct node *p,*tmp;

    if(start ==NULL){

        printf("List is empty\n");

        return start;

    }

    // if item to be inserted before a node

    if(start->info == data){

        tmp = (struct node *)malloc(sizeof(struct node));

        tmp->info = item;

        tmp->link = start;

        start = tmp;

        return start;

    }

    // if data is not present in first node then this code  will be executed

    p=start;

    while(p!=NULL){

        if(p->info == data){

            tmp = (struct node *)malloc(sizeof(struct node));

            tmp->info = item;

            tmp->link = p->link;

            p->link = tmp;

            printf("Element is Inserted\n");

            return start;

        }

        p=p->link;

    }

    printf("Give Element is not present in this list so data can't be inserted in this\n");

    return start;

} // end of the function


// this function will add item before an element

struct node *addat_before(struct node *start, int data , int item){

    struct node *p,*tmp;

    if(start==NULL){

        printf("List is empty no operation could be performed in this condtion\n");

        return start;

    }

    if(start->info == data){

        printf("Element is inserted at position 1\n");

        tmp = (struct node *)malloc(sizeof(struct node));

        tmp->info=item;

        tmp->link = start;

        start = tmp;

        return start;

    }

    p=start;

    while(p!=NULL){

        if(p->link->info==data){

            printf("Item has been inserted\n");

            tmp = (struct node *)malloc(sizeof(struct node));

            tmp->info=item;

            tmp->link = p->link;

            p->link=tmp;

            return start;

        }

        p=p->link;

    }

    return start;

} // end of the function







Round G 2022 - Kick Start 2022 Walktober Problem (C Code)

// Below is the Solution of the Round G 2022 -kickstart 2022 :) 

// Contact me if any question persist in your mind, I'm ready to help you :) 

// HAPPY CODING

#include <stdio.h>

int main(){

    int t,count=0;

    scanf("%d",&t);

    while(t--){

        int M,N,P,k=0,i,j;

        scanf("%d%d%d",&M,&N,&P);

        int max[N],pl[N],n[N];

        for(i=0;i<N;i++)

        max[i]=0;

        for(i=0;i<M;i++){

            for(j=0;j<N;j++){

                scanf("%d",&n[j]);

                if((i+1)!=P){

                if(max[j]<n[j])

                max[j]=n[j];

                }

                else{

                    pl[j]=n[j];

                }

            }

        }

        int ans=0;

        for(i=0;i<N;i++){

            if(max[i]>pl[i])

            ans+=(max[i]-pl[i]);

            else

            ans+=0;

        }

        printf("Case #%d: %d\n",++count,ans);

    }

    return 0;

}

Program to print the smallest word in an sentence (Coding Ninja Solutions)

#include <stdio.h>

#include <string.h> // Here I've to add this header file for strcat() function

int main() {

    char name[]="Vaibhav Yadav is good boy";

    char ccname[]=" "; // This is to add space at the end of sentence so that condition can read it.

    strcat(name,ccname); // this functions append the space to last character

    int i,j,k,min=50,count=0;

    for(i=0;name[i]!='\0';i++){

        if(name[i]!=' '){

        count++;

        }

        else{

            if(count<min)

            {

                min=count;

                k  = i;

                k-=count;

            }

                count=0;

        }

    }

    

    // printf("%d ",min);

    for(i=k;name[i]!=' ';i++)

    printf("%c",name[i]);

    k = 0;

    return 0;

}

ROCK PAPER SCISSOR GAME BUILD USING HTML, CSS and Javascript


 This Rock Paper Scissor Game Build Using HTML CSS and Javascript .

Click Here to View the live website

Click Here to view the original source code at github

Below is the Source Code of Above game

<!--------------------------------- HTML CODE ------------------------->

<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" href="style.css">

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Rockpaperscissor</title>

</head>

<body>

    <div class="container">

        <button class="game" value="Rock">✊</button>

        <button class="game" value="Paper">✋</button>

        <button class="game" value="Scissors">✌</button>

        <div class="showresult">

        <p id="player-score"></p>

        <p id="hands"></p>

        <p id="result"></p>

        </div>

        <button class="reset games" id="endGameButton">🔴</button>

    </div>

    <script src="javascr.js"></script>

    <footer>Made By Vaibhav Yadav with ❤❤</footer>

</body>

</html>

//                                    Javascript Code 

const totalscore = { computerScore: 0, playerScore: 0}


function getcomputerchoice() {

    const rpschoice = ['Rock','Paper','Scissors']

    const randomnumber = Math.floor( Math.random()*3 )

    return rpschoice[randomnumber]

}

function getResult(playerChoice, computerChoice) {

    let score;

    if( playerChoice == computerChoice){ 

    score = 0

    totalscore['computerScore']+= 0

    }

    else if ( playerChoice == 'Rock' && computerChoice == 'Scissors'){

        score = 1

    }

    else if ( playerChoice == 'Paper' && computerChoice == 'Rock'){

        score = 1

    } else if ( playerChoice == 'Scissors' && computerChoice == 'Paper'){

        score = 1

    }

    else{

       score = -1

       totalscore['computerScore']+= 1

 }

    return score

}

function showresult(score,playerChoice,computerChoice)

{

    const resultdiv = document.getElementById('result')

    const handdiv = document.getElementById('hands')

    const playerscorediv = document.getElementById('player-score')

    if(score == -1 ) {

        resultdiv.innerText = "You Lose"

    }

    else if(score == 0 ){

        resultdiv.innerText = "It's a tie"

    }

    else{

        resultdiv.innerText = "You Win"

    }

    handdiv.innerText = "Computer Choice is: " + computerChoice

    playerscorediv.innerText =  "Your Score is : " + totalscore["playerScore"] + "  Computer Score is : " +totalscore["computerScore"]

    

}

function onClickgames(playerChoice){

    // console.log({playerChoice})

    const computerChoice = getcomputerchoice()

    // console.log({computerChoice})

    const score = getResult(playerChoice, computerChoice)

    totalscore['playerScore'] += score

    // console.log({score})

    // console.log(totalscore) 

    showresult(score,playerChoice,computerChoice)

}

function playGame(){

    const games = document.querySelectorAll('.game')

    // games[0].onclick = () => alert(games[0].value)

    

    games.forEach(game => {

        game.onclick = () => onClickgames(game.value)

    })

}

const endgamebutton = document.getElementById("endGameButton")

endgamebutton.onclick = () =>endgame(totalscore)

function endgame(totalscore)

{

    totalscore['playerScore'] = 0

    totalscore['computerScore'] = 0


    const resultdiv = document.getElementById('result')

    const handdiv = document.getElementById('hands')

    const playerscorediv = document.getElementById('player-score')


    resultdiv.innerText = ''

    handdiv.innerText = ''

    playerscorediv.innerText = ''

}

playGame()

/*                              CSS CODE                    */

body{

    background-color: black;

    margin: 0;

    padding: 0;

    font-weight: bolder;

    font-family: sans-serif;

}

.container{

    border-radius: 4%;

    box-shadow: 10px 10px 50px black;

    padding: 2vh;

    width: 80vh;

    text-align: center;

    background-color: steelblue;

    margin: 20vh auto;

}

.container button{

    background-color: transparent;

    border: none;

    

    margin: 3vh;

}

.game {

    box-shadow: 5px 5px 9px black;

    border-radius: 9%;

    /* background-color: red; */

    font-size: 70px;

    cursor: pointer;

}

.reset{

    

    font-size: 50px;

    margin: 9vh 0 0 31vh;

    width: 20vh;

}

/* .container span:hover { 

    transform: rotate(-40deg);

} */

.showresult{

    padding: 2px;

    border-radius: 9px;

    box-shadow: 5px 5px 13px black;

}

.showresultnone{

    height: 0;

    display: none;

}

.games{

    box-shadow: 5px 5px 9px black;

    border-radius: 9%;

    /* background-color: red; */

    font-size: 70px;

    cursor: pointer;

}

button:active{

    transform: translate(2px, 2px);

}

footer{

    text-align: center;

    background-color: wheat;


ROCK PAPER SCISSOR Game using python Language

Below 👇 is the source code for famous ROCK PAPER SCISSOR Game in Python Language.  

Just copy and paste in your Editor and you are ready to go :) 

If you have any query related to this code you are free to ask me, just put your doubt in comment section and I will try to answer it as fast as possible.☺☺

import random, sys

print("----------- Welcome to ROCK-PAPER-SCISSOR Games ------------")

wins = 0 

losses = 0 

tie = 0

while True:

    print("%s wins, %s losses, %s tie" %(wins,losses,tie))

    while True:

        print("Enter Your moves : (r)ock, (s)cissor,(p)aper or (q)uit")

        playerinput = input()

        if(playerinput =='q'):

            sys.exit()

        if playerinput == 'r' or playerinput == 's' or playerinput == 'p':

            break

        print("Type one of r,p,s or q")

        

        

        

    if playerinput == 'r':

        print("ROCK VERSUS...... ")

    elif playerinput == 'p':

        print("PAPER versus......")

    elif playerinput == 's':

        print("SCISSOR versus.....")

        

        #choice of computer starts here :

        

    randomnumber = random.randint(1,3)

    if randomnumber == 1:

        computermove = 'r'

        print("ROCK")

    elif randomnumber == 2:

        computermove = 'p'

        print("PAPER")

    elif randomnumber == 3:

        computermove = 's'

        print("SCISSOR")

        

        #count and display all movees of user and computer

        

    if playerinput == computermove:

        print("It's is a tie ")

        tie+=1

    elif playerinput == 'r' and computermove == 's':

        print("You won")

        wins+=1

    elif playerinput == 'p' and computermove == 'r':

        print("You won")

        wins+=1

    elif playerinput == 's' and computermove == 'p':

        print("You won")

        wins+=1

    else:

        losses +=1

Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...