Skip to content

Commit 5fb0450

Browse files
Merge pull request #29 from divyanshu-29-7/leetd
Leet Code solution for integer to roman conversion.
2 parents 939cd7b + 0c393d4 commit 5fb0450

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

integertoroman.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<stdio.h>
2+
typedef struct{
3+
char *sym;
4+
int val;
5+
}numeral;
6+
int maxNume(numeral *nu, int num){
7+
int i, index;
8+
for(i = 0; i<15; i++){//15 numerals in array
9+
if(nu[i].val <= num)
10+
index = i;
11+
}
12+
//gretest value numeral index, not greater than number
13+
return index;
14+
}
15+
void decToRoman(numeral *nu, int num){
16+
int max;
17+
if(num != 0){
18+
max = maxNume(nu, num);
19+
printf("%s", nu[max].sym);
20+
num -= nu[max].val;//decrease number
21+
decToRoman(nu, num);//recursively print numerals
22+
}
23+
}
24+
main(){
25+
int number;
26+
numeral nume[15] = {{"I",1},{"IV",4},{"V",5},{"IX",9}, {"X",10},{"XL",40},{"L",50},{"XC",90},
27+
{"C",100},{"CD",400},{"D",500},{"CM",900},{"M",1000},{"MMMM",4000},{"V'",5000}};
28+
printf("Enter a decimal number: ");
29+
scanf("%d", &number);
30+
if(number >0 && number <= 5000){//checking input number
31+
printf("The Roman equivalent of %d is ", number);
32+
decToRoman(nume, number);
33+
}
34+
else{
35+
printf("Invalid Input");
36+
}
37+
printf("\n");
38+
}

0 commit comments

Comments
 (0)