Thread: Code converting decimal numbers to Roman nomerals

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    3

    Question Code converting decimal numbers to Roman nomerals

    I write this code, but it doesn`t work and i don`t understand why

    Code:
    #include<stdio.h>
    
    
    int main(void) {
        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, num;
    
    
        printf("Enter a number: \n");
        scanf_s("%d", &num);
        printf("The Roman numeral is: \n");
        while (num != 0) {                               //repeat process until num is not 0
            if (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
        }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    > 'CM'
    You need to express these as strings, not as wide character constants.

    So "CM" not 'CM'

    Note that something like 300 would be printed out as "CCC", but your code doesn't take that into account.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    3
    Quote Originally Posted by Salem View Post
    > 'CM'
    You need to express these as strings, not as wide character constants.

    So "CM" not 'CM'

    Note that something like 300 would be printed out as "CCC", but your code doesn't take that into account.
    If I write "CM" for example, visual studio say that it`s error(

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    Mmmm
    Code:
    const char *symbol[] = {
      "M",
      // etc
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    3
    Ooh, thanks it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-23-2013, 06:30 AM
  2. help me in converting ordinary numbers to roman numerals..
    By Carr Paulo in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2011, 07:15 AM
  3. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  4. Converting Decimal to Binary Numbers using Recursion
    By jaisch in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2005, 10:33 PM
  5. converting arabic to roman numerals
    By kaisha8 in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2004, 01:02 AM

Tags for this Thread