MIT-Curricular/DS/C/Lab/Week3/DecBaseCon.c

53 lines
934 B
C
Raw Normal View History

2024-08-05 01:10:12 +05:30
#include <stdio.h>
#define MAX_SIZE 100
int top = -1;
char stack[MAX_SIZE];
void push(char c) {
if (top < MAX_SIZE - 1) {
stack[++top] = c;
}
}
char pop() {
if (top >= 0) {
return stack[top--];
}
return '\0';
}
void decimal_to_base(int decimal, int base) {
char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (decimal > 0) {
push(digits[decimal % base]);
decimal /= base;
}
printf("Converted number: ");
while (top >= 0) {
printf("%c", pop());
}
printf("\n");
}
int main() {
int decimal, base;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Enter the base to convert to (2-36): ");
scanf("%d", &base);
if (base < 2 || base > 36) {
printf("Invalid base. Please enter a base between 2 and 36.\n");
return 1;
}
decimal_to_base(decimal, base);
return 0;
}