Hi ,
I have completed a programming. Just wondering if anyone have any idea on how to reduce the source code to a more simple one(display part, red color). For this program , i check for whether there is any hexadecimal 10~15. if yes, i display accordingly.
That mean i am now using 16 "if" function. Not a very tidy programming.
Anyone with a much better idea ???
Code:
#include<stdio.h>
main()
{
int i,j,ch,decimal,count,search;
int temp;
int hex[10];
int number[10];
i=0;
count=0;
decimal=0;
printf("\nPlease enter your input in decimal form: ");
ch = getchar();
while(ch != '\n')
{
if('0' <= ch && ch <= '9')
{
decimal = decimal * 10;
decimal = decimal + (ch - '0');
}
ch = getchar();
}
printf("\nInput? %d\n",decimal);
while(decimal != 0)
{
hex[i] = decimal%16;
decimal= decimal/16;
i++;
count++;
}
i=i-1;
for(j=0; j < count; j++,i--)
{
number[j]=hex[i];
}
printf("\nThe hexadecimal is : ");
for(j=0; j < count; j++)
{
if(number[j]==0) {putchar('0');}
else if(number[j]==1) {putchar('1');}
else if(number[j]==2) {putchar('2');}
else if(number[j]==3) {putchar('3');}
else if(number[j]==4) {putchar('4');}
else if(number[j]==5) {putchar('5');}
else if(number[j]==6) {putchar('6');}
else if(number[j]==7) {putchar('7');}
else if(number[j]==8) {putchar('8');}
else if(number[j]==9) {putchar('9');}
else if(number[j]==10) {putchar('A');}
else if(number[j]==11) {putchar('B');}
else if(number[j]==12) {putchar('C');}
else if(number[j]==13) {putchar('D');}
else if(number[j]==14) {putchar('E');}
else {putchar('F');}
}
putchar('\n');
}