Untitled
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
#include <stdio.h> #define MAX_SIZE 100 // Stack variables int stack[MAX_SIZE]; int top = -1; // Check if stack is empty int isEmpty() { return top == -1; } // Check if stack is full int isFull() { return top == MAX_SIZE - 1; } // Push an element to the stack void push(int data) { if (isFull()) { printf("Stack Overflow\n"); return; } stack[++top] = data; } // Pop an element from the stack int pop() { if (isEmpty()) { printf("Stack Underflow\n"); return -1; } return stack[top--]; } // Function to compute binary equivalent void decimalToBinary(int number) { int binary[MAX_SIZE]; int index = 0; while (number > 0) { binary[index++] = number % 2; number = number / 2; } // Print binary equivalent printf("The binary equivalent is: "); while (index > 0) { printf("%d", binary[--index]); } } // Main function int main() { int decimalNumber = 42; decimalToBinary(decimalNumber); return 0; }
Editor is loading...