bishwobista / crack-code

Crack all the coding questions.
7 stars 43 forks source link

Write a Program to convert Integer to Roman in C #92

Open bishwobista opened 11 months ago

bishwobista commented 11 months ago

Given an integer, convert it to a roman numeral using C language.

alok5050 commented 11 months ago

Can you please assign me this issue, I would like to work on it?

bishwobista commented 11 months ago

Can you please assign me this issue, I would like to work on it?

Sure @alok5050, go ahead.

AshaHolla commented 11 months ago

Can I have a go at this issue as well? Thanks

KAITO-2002 commented 11 months ago

Could you assign me this issue as well?

hidden-profile commented 11 months ago

can i please try to solve this issue

hidden-profile commented 11 months ago

include

// Function to convert an integer to a Roman numeral void intToRoman(int num) { int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; char* numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

if (num <= 0 || num >= 4000) {
    printf("Invalid input. Roman numerals are only defined for values between 1 and 3999.\n");
    return;
}

printf("Roman numeral for %d is: ", num);

for (int i = 0; num > 0; i++) {
    while (num >= values[i]) {
        printf("%s", numerals[i]);
        num -= values[i];
    }
}

printf("\n");

}

int main() { int num;

printf("Enter an integer: ");
scanf("%d", &num);

intToRoman(num);

return 0;

}