Thursday, July 31, 2025

HYBRID CALCULATOR WITH TEMPLATES(C++)

#include <iostream>
#include<cmath>
 using namespace std;
 template <class T>
  class calculator{//making base class
public:
T sum(T a,T b){
return a+b;
}
T difference(T a,T b){
   
    return a-b;
}
T product(T a,T b){
    return a*b;
}
T quotient(T a,T b){
   
    return a/b;
}
T remainder(T a,T b){
   
return a%b;
}
 virtual void display(T a,T b){//displaying main arithemetic operations-addition,subtraction,division and multiplication
    cout<<" the sum of two numbers "<<a<<" and " <<b<<" is "<<sum(a,b)<<endl;
    cout<<" the difference of two numbers " <<a<<" and "<< b<< " is "<<difference(a,b)<<endl;
    cout<<" the product of two numbers "<<a<< " and "<< b<<" is "<<product(a,b)<<endl;
    cout<<" the quotient of two numbers "<<a<<" and "<< b<<" is  "<<quotient(a,b)<<endl;
    cout<<" the remainder of two numbers "<<a<<" and "<< b<<" is "<<remainder(a,b)<<endl;
}
 };
 template <class T>
 class scientificCalcualtor:virtual public calculator<T>{//making derived class
public:
T square(T a){
    return pow(a,2);
}
T cube(T a){
    return pow(a,3);
}
T squareroot(T a){
return sqrt(a);
}
void display(T a){//displaying sceintific operations implemented using cmath
cout<<" the square of "<<a<<" is "<<square(a)<<endl;
cout<<" the cube of " <<a<<" is "<<cube(a)<<endl;
cout<<" the squareroot of "<<a<< " is "<<squareroot(a)<<endl;
}
void display(T a,T b){//drawing from base class
    calculator<T>::display(a,b);
}

 };
 template <class T>
 class hybridCalc:virtual public calculator<T>,virtual public scientificCalcualtor<T>{//multilevelinheritance
    public:
void display(T a,T b){//using display function from display classes
calculator<T>::display(a,b);
scientificCalcualtor<T>::display(a);
scientificCalcualtor<T>::display(b);

}



 };
 
 int main(){
hybridCalc<int> H;
H.display(76,45);

 return 0;
 }

Sunday, July 27, 2025

CALCULATOR(WITH TEMPLATES) IN C++

 #include <iostream>

 using namespace std;
 template <class T>
class sum{
T c;
    public:
 sum (T a,T b){
     c=a+b;
   
}
T display(){
    cout<<" value of c is "<<c<<endl;
    T d=c*c;
    cout<<"Value of c's square is "<<d<<endl;
    return d ;
 }
};
template <class T>
class calculator{
T a;
T b;
public:
T sum(T c,T d){
    a=c;
    b=d;
    return a+b;
}
T difference(T c,T d){
    a=c;
    b=d;
    return a-b;
}
T product(T c,T d){
    a=c;
    b=d;
    return a*b;
}
T quotient(T c,T d){
    a=c;
    b=d;
    return a/b;
}

void display(T c,T d){
    a=c;
    b=d;
    cout<<" the sum of a and b is "<<sum(c,d)<<endl;
    cout<<" the difference of a and b is "<<difference(c,d)<<endl;
    cout<<" the product of a and b is "<<product(c,d)<<endl;
    cout<<" the quotient of a and b is "<<quotient(c,d)<<endl;
}
};
 int main() {
 calculator<double> c;
 c.display(5,6);
 return 0;
 }

TEXT ADVENTURE GAME(IN JAVA)





import java.util.*; //importing for user input




public class GAME2 {


    static Scanner s = new Scanner(System.in);


    static int playerHp;


    static String playerName;


    static String playerWeapon;


    static Random rc = new Random();


    static int monsterHp = rc.nextInt(6) + 5; // Random HP between 5 and 10


    static int siverRing;




    public static void main(String[] args) {


        playerSetup();


        TownGate(); // Start game at town gate


    }




    public static void playerSetup() {


        playerHp = 10;


        playerWeapon = "AXE"; // Remove extra spaces for string matching


        System.out.println("YOUR HP IS " + playerHp);


        System.out.println("YOUR WEAPON IS " + playerWeapon);


        System.out.println("ENTER YOUR NAME PLEASE");


        playerName = s.nextLine();


        System.out.println("Hello, " + playerName + ". Let's start your adventure!");


    }


// starting of game


    public static void TownGate() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You are at the town gate.");


        System.out.println("A guard is in front of the gate.");


        System.out.println("What do you want to do?");


        System.out.println("1. Talk to the guard");


        System.out.println("2. Attack the guard");


        System.out.println("3. Leave");




        int choice = s.nextInt();


        s.nextLine();




        if (choice == 1) {


            System.out.println("Guard: Hello there, stranger. So, your name is " + playerName + "?\nSorry, we cannot let a stranger enter our town.");


            TownGate();


        } else if (choice == 2) {


            playerHp -= 1;


            System.out.println("Guard: What the hell?! Don't be stupid!");


            System.out.println("The guard hits you hard. You lose 1 HP.");


            System.out.println("Your HP: " + playerHp);


            TownGate();


        } else if (choice == 3) {


            CrossRoad();


        } else {


            TownGate();


        }


    }


//At crossroad


    public static void CrossRoad() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You are at a crossroad.");


        System.out.println("1. Go north");


        System.out.println("2. Go south");


        System.out.println("3. Go east");


        System.out.println("4. Go west");




        int option = s.nextInt();


        s.nextLine(); // consume newline




        if (option == 1) {


            north();


        } else if (option == 2) {


            south();


        } else if (option == 3) {


            east();


        } else if (option == 4) {


            west();


        } else {


            CrossRoad();


        }


    }


//going to north and hp retaining mechanism


    public static void north() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You find a peaceful river. You rest and drink some water.");


        if (playerHp < 10) {


            playerHp += 1;


            System.out.println("You have recovered 1 HP. Your HP is now " + playerHp);


        } else {


            System.out.println("Your HP is already full.");


        }


        System.out.println("1. Go back to the crossroad");


        System.out.println("2. Stay here ");


        int choice = s.nextInt();


        s.nextLine();


        if (choice == 1) {


            CrossRoad();


        } else {


            north();


        }


    }


//go to south and visit town again


    public static void south() {


        TownGate();


    }


//finding new weapon in east 


    public static void east() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You walked into a forest and found a Long Sword!");


        playerWeapon = "Long Sword";


        System.out.println("Your new weapon is: " + playerWeapon);


        System.out.println("1. Go back to the crossroad");


        int choice = s.nextInt();


        s.nextLine();


        if (choice == 1) {


            CrossRoad();


        } else {


            east();


        }


    }


//fighting enemy in west


    public static void west() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You encounter a goblin!");


        System.out.println("1. Fight");


        System.out.println("2. Run");




        int choice = s.nextInt();


        s.nextLine();


        if (choice == 1) {


            fight();


        } else if (choice == 2) {


            CrossRoad();


        } else {


            west();


        }


    }




    public static void fight() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("Your HP: " + playerHp);


        System.out.println("Goblin HP: " + monsterHp);


        System.out.println("1. Attack");


        System.out.println("2. Run");




        int choice = s.nextInt();


        s.nextLine();


        if (choice == 1) {


            attack();


        } else if (choice == 2) {


            CrossRoad();


        } else {


            fight();


        }


    }


//attack and fighting mechanism


    public static void attack() {


        int weaponDamage = 0;


        if (playerWeapon.equals("AXE")) {


            weaponDamage = rc.nextInt(5) + 1; // 1-5


        } else if (playerWeapon.equals("Long Sword")) {


            weaponDamage = rc.nextInt(6) + 4; // 4-9

        }




        System.out.println("You attack the goblin and deal " + weaponDamage + " damage.");


        monsterHp -= weaponDamage;




        if (monsterHp < 1) {


            win();


            return;


        }




        System.out.println("Goblin HP is now: " + monsterHp);




        // Monster counterattacks


        int monsterDamage = rc.nextInt(5) + 1;


        playerHp -= monsterDamage;


        System.out.println("The goblin attacks you back and deals " + monsterDamage + " damage.");


        System.out.println("Your HP is now: " + playerHp);


        s.nextLine();




        if (playerHp < 1) {


            lose();


        } else {


            fight();


        }


    }


//on defeating goblin


    public static void win() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You defeated the goblin!");


        System.out.println("He dropped a ring");


        System.out.println("You got a siver ring");


        siverRing=1;


        System.out.println("1.go south to town");


        int choice=s.nextInt();


        if(choice==1) {


        Town();


        }else {


        win();


        }


        monsterHp = rc.nextInt(6) + 5; // Reset for another fight if needed


        CrossRoad();


    }




    


//on getting defeated by goblin


public static void lose() {


        System.out.println("\n-----------------------------------------\n");


        System.out.println("You have been defeated by the goblin...");


        System.out.println("GAME OVER");


        System.exit(0);


    }//after defeating goblin and arriving at town


public static void Town() {


System.out.println("\n-----------------------------------------\n");


System.out.println("you go to town and go to guard ");


System.out.println("Would you like to:\n1.show your ring\n2.leave");


int choice=s.nextInt();


if(choice==1) {


TownElder();


}


else if(choice==2) {


CrossRoad();


}else {


Town();


}



System.out.println("\n-----------------------------------------\n");




}




public static void TownElder() {


System.out.println("You show silver ring to guard \n he looks surprised!!!!!!!!!!!!!!!");


System.out.println("He takes you directly to TownELder,te head of town");


System.out.println("TownElder sees your ring and says\n\"this ring is very imprtant for us as it was crafted by our ancestors\nand it has been preserved by us for generations");


System.out.println("Recently,a very powerful goblin stole it from us and we could not get it back");


System.out.println("We would like you to become out town's protector,so say,what was your name again?" +playerName+ " yeah,so "+playerName+" ,would you like to be our protector");


System.out.println("what would you do?\n1.become the protector\n2.leave the town");


int choice=s.nextInt();


if(choice==1) {


ending();



}


else if(choice==2) {


CrossRoad();


}


else {


TownElder();


}


System.out.println("\n-----------------------------------------\n");




}


//ending of game


public static void ending() {


System.out.println("So,from here now you will be given your title according to your choice,choose one");


System.out.println("Choose one from them-:\n1.The Silver Lion of Valor\n2.Knight of the Eternal Oath\n3.Warden of the Broken Sigil\n4.The Crimson Bladebearer\n5.Champion of the Hollow Crown");


String playerTitle="";


int choice=s.nextInt();


switch(choice) {


case 1:playerTitle="The Silver Lion of Valor";


break;


case 2:playerTitle="Knight of the Eternal Oath";


break;


case 3:playerTitle="Warden of the Broken Sigil";


break;


case 4:playerTitle="The Crimson Bladebearer";


break;


case 5:playerTitle="Champion of the Hollow Crown";


break;


default:


System.out.println("Could not decide one,huh!,well I have a name for you,you get the title of Spadidus,meaning\"descending from sparda\" ,the warrior who founded this town  ");


playerTitle="Spadidus";


}


System.out.println("So,now you hold the title of "+playerTitle);


System.out.println("congratulations,you won " +playerName+ " with the title of "+playerTitle);


System.out.println("GAME OVER");


System.exit(0);


}


}

Wednesday, July 2, 2025

GUESS THE NUMBER

 //creating a guess the number game

import java.util.*;
public class GuesstheNumber {
    public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      Random rc=new Random();
      System.out.println("Welcome to my game \n\t how many rounds do you want to play \n\tplease choose a number from 0 to 10");
      //taking inputs and randomly generating no ,also deciding no of rounds player wants to play
        int round=sc.nextInt();
      int input=sc.nextInt();
   
      int computer=rc.nextInt(11);
      int score=0;
      System.out.println("YOU CHOSE "+input);
      System.out.println("COMPUTER CHOSE "+computer);
      //setting rules for game
      for(int i=0;i<round;i++){
      if(input==computer){
        System.out.println("YOU HAVE WON THE GAME");
        score=score+4;
        break;
      }else{
        System.out.println("OH DEAR,YOU HAVE LOST THE GAME");
        score--;
        break;
      }
     
    }
    System.out.println(score);
}
}

MergeSort

  #include < iostream >   using namespace std ;   void merge ( vector < int >& n , int low , int mid , int high ){   ...