Thank you very much ! The program is ok.
The following should be my final code.
Code:
#include<stdio.h>
int romanize(int q[], int base)
{
int counter, temp;
printf("\nThe Romanize number is ");
if(base == 4)
{
// 4 digit
for(counter = 1; counter <= q[0]; counter++)
{
printf("m");
}
// 3 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[3] >= 5)
{
temp = q[3] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[3] < 5)
{
for(counter = 1; counter <= q[3]; counter++)
{
printf("i");
}
}
}
if(base == 3)
{
// 3 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("i");
}
}
}
if(base == 2)
{
// 2 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("l\n");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("i");
}
}
}
if(base == 1)
{
// 1 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("i");
}
}
}
}
main()
{
int i, j, n, ch, count, hexadecimal, decimal, number[10], temp[10];
int hex;
decimal=0, count=0, i=0, j=0;
printf("Enter in hexadecimal format ? (Y/N):");
hex = getchar();
printf("\n");
while(hex != 'Y' && hex != 'y' && hex != 'N' && hex != 'n')
{
getchar();
printf("Please enter the correct key! (Y/N):");
hex = getchar();
printf("\n");
}
//if yes then propmt user for hexadecimal input
if(hex == 'Y' || hex == 'y')
{
getchar();
printf("Please enter in hexadecimal format : ");
hexadecimal = getchar();
while(hexadecimal != '\n')
{
if('0' <= hexadecimal && hexadecimal <= '9')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - '0');
}
else if('A' <= hexadecimal && hexadecimal <= 'F')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'A')+10;
}
else if('a' <= hexadecimal && hexadecimal <= 'f')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'a')+10;
}
else
{
break;
}
hexadecimal = getchar();
}
printf("\nyour input number in decimal format is %d\n",decimal);
while(decimal != 0)
{
temp[i] = decimal%10;
decimal= decimal/10;
i++;
count++;
}
i=i-1;
for(j=0; j < count; j++)
{
number[j]=temp[i];
i--;
}
if(count < 5 ) // Handle number up to 9999 only
{
romanize(number, count);
}
else
printf("\nPlease enter hexadecimal number up to 270F only.\n");
}
else
{
ch = getchar();
printf("Please enter in decimal format : ");
ch = getchar();
while(ch!='\n')
{
if('0' <= ch && ch <= '9')
{
//This is to count the number of digit the user has enter//
n = 0;
n = n * 10;
n = n + (ch - '0');
number[i] = n; //Store each entered digit into array "intnumber[]"//
count++;
i++;
}
ch = getchar();
}
if(count < 5 ) // Handle number up to 9999 only
{
romanize(number, count);
}
else
printf("\nPlease enter number up to 9999 only.\n");
}
}