Roman numeral addition

#include<stdio.h>

#include<string.h>



int digit(char);

void decimal2roman(int num){
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base values
char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //roman symbols
int i = 0;

while(num){ //repeat process until num is not 0
while(num/decimal[i]){ //first base value that divides num is largest base value
printf("%s",symbol[i]); //print roman symbol equivalent to largest base value
num -= decimal[i]; //subtract largest base value from num
}
i++; //move to next base value to divide num
}
}

int main(){



char roman_Number[1000];

int i=0;

long int number =0;



printf("Enter FIRST roman number (Valid digits are I, V, X, L, C, D, M): \n");

scanf("%s",roman_Number);



while(roman_Number[i]){



if(digit(roman_Number[i]) < 0){

printf("Invalid roman digit : %c",roman_Number[i]);

return 0;

}



if((strlen(roman_Number) -i) > 2){

if(digit(roman_Number[i]) < digit(roman_Number[i+2])){

printf("Invalid roman number");

return 0;

}

}



if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))

number = number + digit(roman_Number[i]);

else{

number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));

i++;

}

i++;

}
int number1= number;

// 2ND NUMBER
printf("Enter SECOND roman number (Valid digits are I, V, X, L, C, D, M): \n");

scanf("%s",roman_Number);



while(roman_Number[i]){



if(digit(roman_Number[i]) < 0){

printf("Invalid roman digit : %c",roman_Number[i]);

return 0;

}



if((strlen(roman_Number) -i) > 2){

if(digit(roman_Number[i]) < digit(roman_Number[i+2])){

printf("Invalid roman number");

return 0;

}

}



if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))

number = number + digit(roman_Number[i]);

else{

number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));

i++;

}

i++;

}
int number2= number;
int answer = number1+number2;
printf("Its answer value is : ");
decimal2roman(answer);


return 0;



}



int digit(char c){



int value=0;



switch(c){

case 'I': value = 1; break;

case 'V': value = 5; break;

case 'X': value = 10; break;

case 'L': value = 50; break;

case 'C': value = 100; break;

case 'D': value = 500; break;

case 'M': value = 1000; break;

case '\0': value = 0; break;

default: value = -1;

}



return value;

}



//Made this code that makes roman number addition, i want to put an if statement that "The numbers in the range from 1 to 1999."

// My code needs to make XLV + LIV = XCIX but instead the results it gives is XC only.
12th post and still no code tags. http://www.cplusplus.com/forum/articles/16853/
We shouldn't help people that refuse to use them.
are you required to add them the hard way? Convert to normal, add, convert back to roman, print :P

Part of learning to program is thinking about the simplest/best way to solve a problem.
Last edited on
you have used wrong int num of base values you have to find the number value then you can put char value I, II, IV, IX, X, XL, XC, CM, CD, M, and more roman symbols.
Here used while loop with num/decimal with repeat num until it is not null.

#Here used printf loop also not correct for a valuable value which is used in this programming like

printf("Enter FIRST roman number (Valid digits are I, V, X, L, C, D, M): \n");

scanf("%s",roman_Number);


while(roman_Number[i]){

While loop also not well performed the roman number[i],
and many more.

if(digit(roman_Number[i]) < 0){

printf("Invalid roman digit : %c",roman_Number[i]);

return 0;


#include<stdio.h>

#include<string.h>



int digit(char);

void decimal2roman(int num){
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base values
char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //roman symbols
int i = 0;

while(num){ //repeat process until num is not 0
while(num/decimal[i]){ //first base value that divides num is largest base value
printf("%s",symbol[i]); //print roman symbol equivalent to largest base value
num -= decimal[i]; //subtract largest base value from num
}
i++; //move to next base value to divide num
}
}

int main(){



char roman_Number[1000];

int i=0;

long int number =0;



printf("Enter FIRST roman number (Valid digits are I, V, X, L, C, D, M): \n");

scanf("%s",roman_Number);



while(roman_Number[i]){



if(digit(roman_Number[i]) < 0){

printf("Invalid roman digit : %c",roman_Number[i]);

return 0;

}
return 0;

For more info visit here:- http://www.traininginlucknow.in/best-c-language-training-in-Lucknow.html

Topic archived. No new replies allowed.