Hey all,
I'm kinda new in C program. So I'm suppose to convert a value to a base number from 1 - 16.
The program will compile, but output is wrong. I'm not sure which part did i do wrong, whether isit the reverse part or the code for conversion.
Really hope I could get help from all the members here. Thanks
I'm kinda new in C program. So I'm suppose to convert a value to a base number from 1 - 16.
The program will compile, but output is wrong. I'm not sure which part did i do wrong, whether isit the reverse part or the code for conversion.
Really hope I could get help from all the members here. Thanks
#include<stdio.h>
void DivAlg(int,int);
int main()
{
int val,base; //variables declaration
printf("Enter any non-negative value: "); //Print a statement to ask user for non-negative input
scanf("%d",&val); //prompt user for non negative value input
while(val<0) //If the value entered is negative integer, prompt user for positive integer/value
{
printf("Enter a positive integer: ");
scanf("%d",&val);
}
printf("Enter a base value between (1-16)"); //Print a statement to ask user for base input
scanf("%d",&base);
while(base<1 || base>16)
{
printf("Enter the base value within the range(1-16)");
scanf("%d",&base);
}
DivAlg(val,base);
}
void DivAlg(int val,int base)
{
{
int converted_number[64];
int next_digit, index=0;
char TABLE[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(val != 0)
{
converted_number[index] = val % base;
val = val/base;
++index;
}
--index;
printf("\n\nConverted Number = ");
for( ; index>0; index--)
{
printf("%c", TABLE[converted_number[index]]);
}
}
printf("\n");
}