Upload files to "DS/C/Lab/Week3"
This commit is contained in:
parent
c23e09cb90
commit
a033c5175f
4 changed files with 194 additions and 0 deletions
52
DS/C/Lab/Week3/DecBaseCon.c
Normal file
52
DS/C/Lab/Week3/DecBaseCon.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue