Saturday, November 14, 2020

Stack program - push,pop,peep,display,change,length,middle_element....... in c language.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define MAX 10

int stack[MAX],top = -1;

void push();

int pop();

int peek();

void display();

void change();

int length();

int middle_ele();


void main()

{

    int ch;

    while(1)

    {

    printf("\n1.push \n2.pop \n3.peek \n4.display \n5.change \n6.length \n7.middle_element \n8.exit\n");

    printf("\nenter the choice : ");

    scanf("%d",&ch);

    switch(ch)

    {

        case 1: push(); break;

        case 2: pop(); break;

        case 3: peek(); break;

        case 4: display(); break;

        case 5: change(); break;

        case 6: length(); break;

        case 7: middle_ele(); break;

        case 8: exit(0);

        default : printf("\n invalid choice\n\n");

    }

  }

}

void push()

{

    int data;

    printf("enter the data : ");

    scanf("%d",&data);

    if(top==MAX-1)

    {

        printf("\n stack is full");

    }

    else

    {

        top++;

        stack[top]=data;

        printf("%d is inserted.\n",data);

    }

}

int pop()

{

    if(top==-1)

    {

        printf("stack is empty\n");

    }

    else

    {

        printf("%d is deleted.\n ",stack[top]);

        top--;


    }

}

int peek()

{

    if(top==-1)

     {

       printf("stack in empty.\n");

     }

    else

    {

       printf("%d is peek element.",stack[top]);

    }

}

void display()

{

    int i;

    if(top==-1)

      {

        printf("stack is empty");

      }

    else

    {

        printf("stack elements are :\t");

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

        {

            printf("%d\t",stack[i]);

        }

    }

}

void change()

{

   int p,ele;

   printf("enter the position where you wants to change the element :");

   scanf("%d",&p);

   if(top-p<-1)

   {

        printf("stack is underflow.\n");

   }

   else

   {

       printf("enter the new element : ");

       scanf("%d",&ele);

       stack[p-1]=ele;

       printf("~new element %d is inserted: ",stack[p-1]);

   }


}

int length()

{

    int count=0,i;

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

    {

        count=count+1;

    }

    printf("Length of the stack is : %d\n",count);

    return count;

}

int middle_ele()

{

    int n,ele,ele1,ele2;

    if(length()%2==0)

    {

        n=length()/2;

        ele1 = stack[n-1];

        ele2 = stack[n];

        printf("Middle element's of the stack are : %d  %d\n ",ele1,ele2);

    }

    else

    {

        n=length()/2;

        ele = stack[n];

        printf("Middle element of the stack is : %d\n",ele);

    }

}

OUTPUT :



Friday, November 13, 2020

Calculator program in c language....using switch case and function.

 #include<stdio.h>

#include<stdlib.h>

void add();

void sub();

void multi();

void divi();

void modu();

int a,b;


void main()

{

    int ch;

   while(1)

   {

       printf("\n\n Enter the first no: ");

       scanf("%d",&a);

       printf(" Enter the second no: ");

       scanf("%d",&b);

       printf("\n1.addition\n2.substraction\n3.multiplication\n4.division\n5.modulus\n6.exit");

       printf("\n\n Enter the choice :");

       scanf("%d",&ch);

       switch(ch)

       {

           case 1:

           add();

           break;

           case 2:

           sub();

           break;

           case 3:

           multi();

           break;

           case 4:

           divi();

           break;

           case 5:

           modu();

           break;

           case 6:

           exit(0);

           break;

           default:

           printf("\n Invalid choice");

       }printf("\n!..............End of the previous operation..............!");

       printf("\n!.............Now move to the next operation............!");

   }

}

void add()

{

   printf(" addition of %d and %d : %d",a,b,a+b);

}

void sub()

{

    printf(" substraction of %d and %d : %d",a,b,a-b);

}

void multi()

{

    printf(" multiplication of %d and %d : %d",a,b,a*b);

}

void divi()

{

    printf(" division of %d and %d : %d",a,b,a/b);

}

void modu()

{

    printf(" modulus of %d nad %d : %d",a,b,a%b);

}

OUTPUT :




Matrix C programming ....in which diagonal ele == 0, rowcol == 1.

 #include<stdio.h>

#include<conio.h>

void main()

{

    int i,j,mat[10][10],row,col;

    printf("Enter the no of row and column :");

    scanf("%d%d",&row,&col);

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

    {

        for(j=0;j<col;j++)

        {

            if(i==j)

               mat[i][j]=0;

            else if(i<j)

               mat[i][j]=-1;

            else

               mat[i][j]=1;

        } 

    }

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

   {

      printf("\n");

      for(j=0;j<col;j++)

      {

          printf("%d\t",mat[i][j]);

      }

   }

}

OUTPUT :




Stack program - push,pop,peep,display,change,length,middle_element....... in c language.

#include<stdio.h> #include<stdlib.h> #include<conio.h> #define MAX 10 int stack[MAX],top = -1; void push(); int pop(); int...