Thursday, 24 August 2023

STACK

 #include <stdio.h>

#include <stdlib.h>  // For system("clear")

#define Max 10


int st[Max], top = -1;


void push(int st[], int val);

int pop(int st[]);

int peak(int st[]);

void display(int st[]);


int main() {

    int val, option;

    system("clear");


    do {

        printf("*****CHOOSE OPTION******\n");

        printf("1. Push\n");

        printf("2. Pop\n");

        printf("3. Peak\n");

        printf("4. Display\n");

        printf("5. Exit\n");


        printf("Enter your option: ");

        scanf("%d", &option);


        switch(option) {

            case 1:

                printf("Enter the number to Push: ");

                scanf("%d", &val);

                push(st, val);

                break;   ////

  • case 1:: Handles the case where the user chooses to push a value onto the stack.

  • printf("Enter the number to Push: ");: Prompts the user to enter a value to push onto the stack.

  • scanf("%d", &val);: Reads the value from the user and stores it in the val variable.

  • push(st, val);: Calls the push function to push the entered value onto the stack.

  • break;:


            case 2:

                val = pop(st);

                if(val != -1)

                    printf("The value deleted from the stack is %d\n", val);

                break;

Case 2 Explanation (Pop Operation):

This case corresponds to the "Pop" operation in the menu. When the user selects option 2, the program performs the pop operation on the stack. Let's break down the lines of code within this case:

  • val = pop(st);: This line calls the pop function with the st array as an argument. The pop function returns the value that was removed from the stack (or -1 in case of underflow) and assigns it to the val variable. This allows you to capture the value that was popped from the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an underflow condition, which means the stack was empty and the pop operation could not be performed. If val is not -1, it means the pop operation was successful.

  • printf("The value deleted from the stack is %d\n", val);: If the pop operation was successful (i.e., val is not -1), this line prints the value that was popped from the stack. This provides feedback to the user about the removed element.

  • break;: This line ends the execution of case 2. After performing the pop operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.



            case 3:

                val = peak(st);

                if(val != -1)

                    printf("The value on top is %d\n", val);

                break;


Case 3 Explanation (Peak Operation):

This case corresponds to the "Peak" operation in the menu. When the user selects option 3, the program performs the peak operation on the stack. The peak operation retrieves the value at the top of the stack without removing it. Let's break down the lines of code within this case:

  • val = peak(st);: This line calls the peak function with the st array as an argument. The peak function returns the value at the top of the stack (or -1 in case of an empty stack) and assigns it to the val variable. This allows you to capture the value at the top of the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an empty stack, and the peak operation cannot be performed.

  • printf("The value on top is %d\n", val);: If the peak operation was successful (i.e., val is not -1), this line prints the value that is currently at the top of the stack. This provides feedback to the user about the top element without removing it.

  • break;: This line ends the execution of case 3. After performing the peak operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.

            case 4:

                display(st);

                break;

        }

    } while(option != 5);


    getchar();  // Wait for a keypress

    return 0;

}


void push(int st[], int val) {

    if(top == Max - 1) {

        printf("Stack is overflow or full\n");

    } else {

        top++;

        st[top] = val;

    }

}


int pop(int st[]) {

    int val;

    if(top == -1) {

        printf("\nStack underflow or empty\n");

        return -1;

    } else {

        val = st[top];

        top--;

        return val;

    }

}


void display(int st[]) {

    int i;

    if(top == -1) {

        printf("\nStack is underflow or empty\n");

    } else {

        for(i = top; i >= 0; i--)

            printf("\n%d", st[i]);

        printf("\n");

    }

}


int peak(int st[]) {

    if(top == -1) {

        printf("\nStack is empty\n");

        return -1;

    } else {

        return st[top];

    }

}


Sunday, 20 August 2023

Structures ONE SHOT

                                                   Structures 


USER DEFINED DATA TYPE 

struct employee {

int emp_id; 

char name ;

float salary;

};



#include<stdio.h>


struct pokemon{

    int attack;

    int speed; 

    int hp;

    char class;  //A B C 

    

};


int main(){

    

    struct pokemon pika;

    pika.attack=60;

    pika.speed=40;

    pika.hp=30;

    pika.class= 'A';

    

    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);

    

    struct pokemon ash;

    ash.attack=30;

    ash.speed= 50;

    ash.hp=90;

     ash.class= 'B';

     printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);


}

//AISE HI AAP SIRF STRUCT POKEMON AUR USKE AAGE EK PECIFIER LAGA KE INFINITY INFORMATION STORE KAR SAKTE HAI 


YE JUB HUME INPUT LEA HO VALUES KO  note %c likh rehe ho tho ek space dedena kyu ki koi white space name ki chiz hoti hai   NOTE =%C BUS EK CHARACTER PRINT KARE GA  BUT TUMEHE PURA NAME PRINT KAR NA HO THO EK CHAR ARRAY BA NAO USKA SIZE RAKHO AUR %S KE SAATH PRINT KARAO.

   printf("enter attack speed hp and name ");

    scanf("%d%d%d %c", &pika.attack,&pika.speed,&pika.hp,&pika.class);

    

    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);



SAME PROGRAME HAI BUS PIKA   AUR   ASH UPPAR HI DEFIE KAR DIYA 


#include<stdio.h>




struct pokemon{


    int attack;


    int speed; 


    int hp;


    char class;  //A B C 


    


} pika, ash;              // ye TYPEDEF nhi hai samajh bhai niche dekh compare kar ye thoda alag hai  



int main(){


    


    


    pika.attack=60;


    pika.speed=40;


    pika.hp=30;


    pika.class= 'A';


    


    printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);


    


   ;


    ash.attack=30;


    ash.speed= 50;


    ash.hp=90;


     ash.class= 'B';


     printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);


}



FOR PRINTING NAME 

strcpy(a1.name,"habits ");


NICHE WALE CODE MAI WITHOUT STRCPY USE KAR KE NAME  INPUT LIYE AUR PRINT KARWAYA 

#include<stdio.h>

#include<string.h>

struct Person{

    char name[100];

    int salary;

    int age;

    

}a1;


int main(){

   printf("enter employee details 1)name 2)salary 3)age \n");

   scanf("%s %d %d", a1.name, &a1.salary, &a1.age);  //a1.name ke pehele '&' nhi use hoga 

   

   

   

   printf("your input is \n %s\n %d\n %d\n ",a1.name, a1.salary, a1.age);

    

}



TYPEDEF KEYWORD 

typedef   is  used  for  renaming  something 

NOTE :look  apna collage notes. 

#include<stdio.h>

typedef float realNumber;


int main(){

    

    int x;

    realNumber y = 3.14;  //simpally humne float ko ek naya name de diya       realNumber ye jo realNumber hai vo float ka kam kare ga 

                   //basically typedef is used for renaming something.

    printf("%f",y);

}

TYPEDEF KEYWORD  apna collage

used to create a alias for data types 

in simple word jo bhi hum structure ana rahe hai uske liye ek nick name dena for example agar hum ne koi bade nam ka structure banaya par hum uss bade name ko bar bar likh na nhi chahate so uske liye ek shortform bana denge 

#include <stdio.h>

#include <string.h>

typedef struct student { //bhai yaha type def likh na mat bhul na bus 

    int roll; 

    float cgpa;

    char name[100];

stu;


int main(){

    

    stu a1;   //ya struct student a1 ki jage bus stu a1 likha bus       

    a1.roll = 34;

    a1.cgpa = 8.1;

    strcpy(a1.name, "ashish");

    

    printf(" roll no is : %d", a1.roll);

 

    return 0;

}

thoda galat hai  janae se pehele kar fir se 

#include <stdio.h>

#include<string.h>

struct adress{

    int hoouseno;

    int blockno;

    char city[100];

    char state[100];

};

void print(struct address a1);


int main() {

    struct address a1[100];

    printf("enter info here for person 1");

    scanf("%d", &a1[0].houseno);

    scanf("%d",&a1[0].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

   

      printf("enter info here for person 2");

    scanf("%d", &a1[1].houseno);

    scanf("%d",&a1[1].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

    

     printf("enter info here for person 3");

    scanf("%d", &a1[2].houseno);

    scanf("%d",&a1[2].blockno);

    scanf("%d", a1.state);

    scanf("%d", a1.city);

    

    print(a1.[0]);

    print(a1.[1]);

     print(a1.[2]);


    return 0;

}

void print(struct address a1){

    

    printf("address is : %d, %d, %s, %s\n", a1.houseno, a1.blockno, a1.state, a1.city);


//make a structure to store bank account information of a customer of abc bank. also, make an alias for it 


#include<stdio.h>

#include<string.h>


typedef struct bankaccount{

    int accountno;

    char name[100];

}  acc;


int main(){

    

    acc a1 = {124,"ashish"};

    acc a2 = {234, "shradha"};

     acc a3 = {102, "shiv"};

     

     printf("acc no = %d \n",a1.accountno);

      printf("acc name = %s",a1.name);

      

      return 0;

}


                                                          ARRAY OF STRUCTURES 


#include<stdio.h>

int main(){
typedef struct pokemon{

    int speed;
    int attck;
    int hp;
    char class;
}pokemon;

pokemon arr[10];


    return 0;
}


#include<stdio.h>

#include<string.h>


int main(){


    typedef struct pokemon{

        int attack;

        int hp;

        int speed;

        char class;

        char name [15];

    }pokemon;


    pokemon arr[3];  // dekh yaHha 3n alag box bun gai 0,1,2


    arr[0].attack = 40;  //ab ye arr[0] ke box ke andar 4 type distribute hue hai 

    arr[0].hp = 30;

    arr[0].speed = 50;

    arr[0].class = 'A';

    // arr[0].name = "poki";  aise nhi likh sakte hai 

    

    strcpy(arr[0].name, "poki");


    arr[1].attack = 35;

    arr[1].hp = 64;

    arr[1].speed = 44;

    arr[1].class = 'B';

    strcpy(arr[1].name, "loki");


    arr[2].attack = 450;

    arr[2].hp = 305;

    arr[2].speed = 350;

    arr[2].class = 'C';

    strcpy(arr[2].name, "moki");

    

    for(int i=0;i<3;i++){  //ye ek loop se sari chiz print ho jaegi pehele 0 position pe check hoga aur print  fir 1 fir 2 

    

        printf("Name = %s\n", arr[i].name );    

        printf("Attack = %d\n", arr[i].attack);

        printf("Hp = %d\n", arr[i].hp);

        printf("Speed = %d\n", arr[i].speed);

        printf("Class = %c\n \n", arr[i].class);

        

    }

}

dekh humne uppar value declare kiya tha matlab attack =30 aise ab input lene hai tho bus printf ki jage scanf kar do LOOP   LAGA   KE


DECLARATION :   hum aise ek structure array ko declare kar te hai 

struct pokemon arr[size];  or 

agar typedef declare kiya hia tho pokemon arr[s];


ACCESS :   aur print kar na ho tho aise access kar te hai 

arr[i].attribute;



Q ) //A record contains name of cricketer his age number of test that he played and avarage reun . Creat  an array and then write a program to read these records


#include<stdio.h>

#include<string.h>


int main(){

    

    typedef struct cricketer {

        

        char name [20];

        int age;

        int nft;

        int avg;

        

    }cricketer;

    

    cricketer arr[10];

    

    printf("Enter player details 1) name 2) Age 3) No of test he played 4) His avarage \n ");

    

    for(int i=0;i<10;i++){

       scanf("%s %d %d %d \n", arr[i].name, &arr[i].age,&arr[i].nft, &arr[i].avg);

    }

    

    printf("Your player details are ");

    for(int i=0;i<10;i++){

        printf("Name = %s\n Age= %d\n No of test he played= %d\n His avarage= %d\n\n",arr[i].name,arr[i].age,arr[i].nft,arr[i].avg);

        

    }

    

}

//   PREFECTLLY RUN HO RAHA HAI EKDAM MUST CODE LIKHA HAI TUNE 




#include<stdio.h>


int main(){

    

    typedef struct date{

        int day;

        int month;

        int year;

        

    }date;

    date a,b;

    

    a.day = 12;

    a.month = 12;

    a.year = 2002;

    

    b.day = 12;

    b.month = 4;

    b.year = 2042;

    

    //if(a==b){

    //    printf("equal");

   // }                         a == b COMPAR NHI HO SAKTA HAI BUT USKE ANDAR KI CHIZ COMPARE HO SAKTI HAI JAISE HUMM NE NICHE KIYA HAI.

   // else{

   //     printf("not equal ");

  //  }

    

    

    if(a.day == b.day ){

        printf("equal");

    }

    else{

        printf("not equal")

    }

// hum ye bhi kar sakte hai 

date c;

c == a;


}


                                      NESTING ONE STRUCTURE WITH ANOTHER STRUCTURE                  

#include<stdio.h>

#include<string.h>


int main(){

    

    typedef struct pokemon{

        int hp;

        int speed;

        int attack;

        char name[20];

        

    }pokemon;   // ye hamare normal structure 

    

    typedef struct legendarypokemon{

        pokemon normal;     //yaha pokemon likh ne se hum upparwale pokemon ki               //sari ciz iss nested structure mai use kar sakte hai

        

        char ability[10];  // aur extra chiz bhi add kar sakte hai 

    }legendarypokemon;      // ye  first nested structure banaya humne

    

    typedef struct godpokemon{

        pokemon legend;   //ye pokemon likhne se upar wala main structure pokemon ki sari chiz issme aagai 

        

        int specialattack; //ye like ke maine is nested mai kuch extra add kiya

        

    } godpokemon;

    

    godpokemon az;

    

    az.specialattack = 300;

    

    strcpy(az.legend.ability, "turn anyone in stone");   // .legend likh ne se hune jo nseted loop banayaa tha legendary pokemon ka uske andar ki chiz use kar sak te hai jaise hum ne ussme ability banaya tha vo hum issme bhi use kar sakt te hai 

    

    az.legend.normal.attack = 500;

    

    

    

    

    

    legendarypokemon am;

    am.normal.hp = 150;    //aise hum access kar sakte hai upar wale pokemon ke                              // attributes

    strcpy(am.ability, "fire ball");

    am.normal.speed = 60;

    am.normal.attack = 50;

    strcpy(am.normal.name,"ash");

    

}




                                                         STRUCTURE AS FUNCTION                                                           



Friday, 18 August 2023

JAVA FUNCTION

                                                            FUNCTION

returnType functionName(type arg 1, type arg2....){

//operation 

}







import java.util.*;

public class Function{     //ye hum bata rahe hai ki abhi function declare hone wala hai
    public static void functionName(String name){   //main function yaha declare hota hai
        System.out.println(name);
        
        return;      //function mai hamesha return type dena hota hai  yaha kuch return nhi kar na hia tho                                                      bus return likh do 
    }
    
    public static void main(String args[]){
        System.out.println("enter your name\n");
        Scanner am = new Scanner(System.in);    // for taking input of string 
        String name = am.next();
        
        functionName(name);  //call kiya function ko uss function ke andar jo inputs hai vo pass kar denge 
    }
}


import java.util.*;

public class Function{
    public static int add(int a, int b) { // yaha main function declare hota hia jaise yaha humne int wala function declare kiya hai isleye argument mai bhi int as input liya hai 
    
    
   // System.out.println(a+b);  ek ye type ho gaya print kar ne ka
   
   int sum = a+b;  //aise bhi print kar sakte ho 
   return sum;  
    }
    
    public static void main(String args[]){
        System.out.println("enter two number to add\n");
        Scanner am =new Scanner(System.in);
        int a = am.nextInt();
        int b = am.nextInt();
        
        
        int result = add(a,b);
        System.out.println("Addition is : "+ result);
    }
}


Q)   find factorial 

import java.util.*;

public class Factorial{
    public static int factio(int n){
    
    int i;
    int fact=1;
    for(i=1;i<=n;i++){
    fact =fact*i;
    }
    
    // System.out.println(fact);    ye likh ne se aacha return type mai  fact return le 
                                                                    return fact;
}

public static void main(String args[]){
    System.out.println("Enter number ");
    Scanner am =new Scanner(System.in);
    int n=am.nextInt();
    
    int result = factio(n);
    System.out.println("Factorial of " + n + " is: " + result);
}
}

SAME CODE
import java.util.*;

public class Factorial{
    public static int factio(int n){
    
    if(n<0){
        System.out.println("galat number");
        return -1;
    }
    int i;
    int fact=1;
    for(i=1;i<=n;i++){
    fact =fact*i;
    }
    
    // System.out.println(fact); ye likh ne se aacha return type mai                   fact return le 
    return fact;
}

public static void main(String args[]){
    System.out.println("Enter number ");
    Scanner am =new Scanner(System.in);
    int n=am.nextInt();
    
    int result = factio(n);
    System.out.println("Factorial of " + n + " is: " + result);
}
}



JAVA PROGRAMMING

                                                                              CHAPTER  ONE


class firstClass {

    public static void main(String args[]) {

        System.out.println("Hello World ");

    }


Q) class hello{

    public static void main(String args[]){

        System.out.println("helllo world ");

         System.out.print("*\n**\n***\n****");

    }

}



class hello{

    public static void main(String args[]){

     int a=25;

     int b=10;

     double price =25.25;

     int sum =a+b;

     System.out.println(sum);

    }


Q)  

Scanner am = new Scanner(System.in);

String name = am.nextLine();   // for taking full name 

String gender = am.next();    // for taking only one word 

long moblieNumber = am.nextLong();

int a = am.nextInt();

double cgpa = am.nextDouble();    // for taking float as input 






import java.util.*;


public class main {

public static void main(String args[]){

    

    //   input 

  Scanner sc = new Scanner(System.in);

  String name = sc.next();

  System.out.println(name);

  }

}

OUTPUT 

ashish 

ashish


Q)   

import java.util.*;


public class main {

public static void main(String args[]){

    

    //  

  Scanner sc = new Scanner(System.in);

  String name = sc.nextLine();

  System.out.println(name);

  }

}

OUTPUT

ashish maurya 

ashish maurya 


Q)


import java.util.*;


public class main {

public static void main(String args[]){

    

    Scanner sc = new Scanner(System.in);

     System.out.println("enter first number");

    int a =sc.nextInt(); // nextInt is to take input of integer value

    System.out.println("enter second number");

    int b = sc.nextInt();

    int sum= a+b;

    System.out.println("sum is " + sum);   //  there should be space is +  and sum

    }

}


Conditional Statement


import java.util.*;
public class condition{
    public static void main(String args[]){
        Scanner am = new Scanner(System.in);
        System.out.println("enter your age ");
        int x=am.nextInt();
        if(x>18){
            System.out.println("You are  adult ");
        }
        else{
            System.out.println("You are not adult");
        }
    }
}





Q)
import java.util.*;
public class condition{
    public static void main(String args[]){
        Scanner am = new Scanner(System.in);
        System.out.println("enter your number ");
        int x=am.nextInt();
        if(x%2==0){
            System.out.println("EVEN  ");
        }
        else{
            System.out.println("ODD");
        }
    }
}


import java.util.*;
public class condition{
    public static void main(String args[]){
        Scanner am = new Scanner(System.in);
        System.out.println("enter first your number ");
        int x=am.nextInt();
        System.out.println("enter second your number ");
        int y=am.nextInt();
        if (x==y){
            System.out.println("equal");
        }
        else if (x>y){
            System.out.println("Greater");
        }
        else{
            System.out.println("Smaller");
        }
    }
}



SWITCH  CASE BAKIHAI 



LOoPS

import java.util.*;

public class loops{
    public static void main(String args[]){
        Scanner as = new Scanner(System.in);
        System.out.println("enter a number ");
        int n= as.nextInt();
        for (int i=1;i<=n;i++){
             System.out.println("hello world \n ");
        }
    }
}




Q)  while loop


int i=0    // while loop mai int while ke upar hi likh te hai 

while( condition){     //while means jubta

      printf()   // 
      
     i++   //aur i ko update kare ge kuch print kar ke ya kuch kam kar ke 

}


simplly =  dekh i lo intialize while loop ke uppar kar na hai  
                  fir while likh na hai aur ussme conditionn likh ni hai 
                  fir jake kuch kam karwa na hai ya kuch prit kar na hai 
                  fir i++ kar ke loop ko aage kar na hai 



public class ab{
    public static void main(String args[]){
        
        int i=0;
        
        while(i < 11) {
            System.out.println("hello");
            i++;  // or i = i+1
        }
    }
}




Do   while   looop 


int i = 0    //pehele i ko initialize karo fir 

do {

    printf  ( );     //do something 
    
     i=i+1      // i ko update humm do ke andar hi karenge 

   }  while (condition); 




public class ab{
    public static void main(String args[]){
        
        int i=0;
        
       do{
           System.out.println(i);
           i=i+1;
       } while(i<11);
    }
}


Q) print table 
import java.util.*;

public class table{
    public static void main(String args[]){
    Scanner am = new Scanner(System.in);
    System.out.println("enter number");
    int x= am.nextInt();
    for(int i=1;i<=10;i++){
        System.out.println(i*x);
    }
        
        
    }
    
  end