fork download
  1. #include<stdio.h>
  2.  
  3. int stack[4],size,num;
  4. int top=-1;
  5.  
  6. void push()
  7. {
  8. printf("enter the no. you want to push :");
  9. scanf("%d",&num);
  10. if(top>=size-1)
  11. {
  12. printf("stack is full");
  13. }
  14. else
  15. {
  16. top++;
  17. stack[top]=num;
  18. //printf("%d pushed to stack\n",num);
  19. }
  20. }
  21.  
  22. void display() {
  23. if (top == -1)
  24. {
  25. printf("Stack is empty\n");
  26. }
  27. else
  28. {
  29. printf("Stack elements are:\n");
  30. for (int i = 0; i <= top; i++)
  31. {
  32. printf("%d\n",stack[i]);
  33. }
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. printf("enter the size :");
  40. scanf("%d",&size);
  41. printf("enter the number :");
  42.  
  43. for (int i = 0; i < size; i++) {
  44. top++;
  45. scanf("%d", &stack[top]);
  46. }
  47.  
  48.  
  49.  
  50. push();
  51. display();
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0.02s 25472KB
stdin
Standard input is empty
stdout
#include<stdio.h>

int stack[4],size,num;
int top=-1;

void push()
{
    printf("enter the no. you want to push :");
    scanf("%d",&num);
    if(top>=size-1)
    {
        printf("stack is full");
    }
    else
    {
        top++;
        stack[top]=num;
        //printf("%d pushed to stack\n",num);
    }
}

void display() {
    if (top == -1)
    {
        printf("Stack is empty\n");
    }
    else
    {
        printf("Stack elements are:\n");
        for (int i = 0; i <= top; i++)
        {
            printf("%d\n",stack[i]);
        }
    }
}
   
int main()
{
        printf("enter the size :");
        scanf("%d",&size);
        printf("enter the number :");
       
        for (int i = 0; i < size; i++) {
        top++; 
        scanf("%d", &stack[top]); 
}

        

    push();
    display();
   
    return 0;
}