Hi,
I'm currently writing a program that takes a number (n) and a base (b) between [1, 10] from the user's input and convert it into base 10. However, I have to make sure that all the digits of n has to be between [0, b-1] with the exception of n being 1. For example:
n = 012 and b =3, output is 5
n = 00110 and b=2, output is 6
n = 00110 and b=1, output is 2
for base (b) = 1, the current program cannot check to see if the digits of 'n' is greater than 1 or not because of this code that i use to check it: output[index] = n%b. For example:
n=23 and b = 2 : output[index] = 23%2 = 3 ; and 3 is greater than base 2
BUT if n = 23 and b =1; ouput[index] = 23%1 = 0; and I CANNOT TAKE THE 3 to compare to base 1 so that 3 is greater than 1.
Please help me! The following is my code:
Thank you!Code:#include<stdio.h> int main(void) { int n, b; int output[64]; int index = 0; printf("Enter base(b) between [1,10], and a number(n) between [0,b-1] in this format 'n b': "); scanf("%d %d", &n, &b); printf("n is: %d\n", n); //Test printf("b is: %d\n", b); //TEST if ((b < 1) || (b > 10)) { printf("Your base is not between 1 and 10"); return 0; } if (n < 0) { printf("n must be positive"); return 0; } while (n != 0) { output[index] = n % b; if ((output[index] >= b) && (output[index] != 1)) //TROUBLE AREA RIGHT HERE { printf("one of the digits is greater than the base"); return 0; } n = n/b; ++index; printf("number to convert: %d\n", n); } return 0; }



LinkBack URL
About LinkBacks



