Hey i'm writing a program that converts decimal numbers into any base unit from binary to hexadecimal (2,3,4,....,15,16). This is what I have so far, running any number from 2 - 15 results in an infinite loop. I was wondering if you had any advice for the rest of my project. When run, this will ask you for two integers, the first should be a decimal integer > 0, the second should be the base type you want it converted to. Any and all advice for completing this would be greatly appreciated! Thank you.
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x, y, z, c;
printf("Please enter two integers: ");
scanf("%d", &x);
scanf("%d", &y);
printf("%d\n", x);
printf("%d\n", y);
printf(" \n");
if(y < 2 | y > 16){
printf("You have entered incorrect information.\n");
return 0;
}
else if(y > 1 && y < 16){
while(z != 0){
z = (x/y);
c = (x%y);
printf("%d\n", z);
}
printf("%d\n", z);
printf("%d\n", c);
}
else if(y == 16){
printf("%X\n", x);
}
}
**********************edit**********************
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x, y, z, c, i;
printf("Please enter two integers: ");
scanf("%d", &x);
scanf("%d", &y);
printf("%d\n", x);
printf("%d\n", y);
printf(" \n");
if(y < 2 || y > 16){
printf("You have entered incorrect information.\n");
return 0;
}
else if(y > 1 && y < 17){
while(x != 0){
c = (x%y);
x = (x/y);
if( c > 1 && c < 10){
printf("%d", c);
}else if( c == 10){
c = printf("A");
}else if( c == 11){
c = printf("B");
}else if( c == 12){
c = printf("C");
}else if( c == 13){
c = printf("D");
}else if( c == 14){
c = printf("E");
}else if( c == 15){
c = printf("F");
}
}
printf("\n");
}
This is the progress i've made so far. My problems are this: I cannot get the values A-F to output correctly to represent numbers 10-15. Also I cannot get the integers to be displayed the correct way. I think this is an easy fix, but a command I'm not used to yet. Thank you all for your help, it's been extremely beneficial.
*********edit 2 ************
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x, y, z, c; //Sets up variables to be used in program
printf("Please enter two integers: "); //Asks for user input
scanf("%d", &x);
scanf("%d", &y); //stores input
printf("%d\n", x);
printf("%d\n", y);
printf(" \n");
if(y < 2 || y > 16){
printf("You have entered incorrect information.\n");
return 0;
} //bug checks
else if(y > 1 && y < 17){
while(x != 0){
c = (x%y);
x = (x/y); // Loops numbers until a 0 value is reached, can be used with
// any Base
if( c == 10){
c = printf("A");
}else if( c == 11){
c = printf("B");
}else if( c == 12){
c = printf("C");
}else if( c == 13){
c = printf("D");
}else if( c == 14){
c = printf("E");
}else if( c == 15){
c = printf("F");
}else{
printf("%d", c);
}
// Returns for each remainer option
}
printf("\n");
}
}
OKay third time is the charm. The only problem I have with my code now is that i cannot figure out how to make it output in reverse order, the proper order. Ignore my second edit, I solved that myself. Any input would be awesome. I don't really know how I'm going to get it to come out reversed. Thanks everyone!