Código C++ – Convertir Decimales a Números Romanos

//Codificado por: Beastieux
//CONVIERTE DECIMALES EN NUMEROS ROMANOS

#include <iostream>
using namespace std;

int main() {
    int x;
    cout << "ingrese un numero" << endl;
    cin >> x;

    if ((x < 1) || (x > 999)) cout << "Ingrese un numero entre 1-999" << endl;
    else {
        if (x >= 900) { cout << "CM"; x = x - 900; }
        if (x >= 500) { cout << "D"; x = x - 500; }
        if (x >= 400) { cout << "CD"; x = x - 400; }
        if (x >= 300) { cout << "C"; x = x - 100; }
        if (x >= 200) { cout << "C"; x = x - 100; }
        if (x >= 100) { cout << "C"; x = x - 100; }
        if (x >= 90)  { cout << "XC"; x = x - 90;  }
        if (x >= 50)  { cout << "L"; x = x - 50;  }
        if (x >= 40)  { cout << "XL"; x = x - 40;  }
        if (x >= 30)  { cout << "X"; x = x - 10;  }
        if (x >= 20)  { cout << "X"; x = x - 10;  }
        if (x >= 10)  { cout << "X"; x = x - 10;  }
        if (x >= 9)   { cout << "IX"; x = x - 9;   }
        if (x >= 5)   { cout << "V"; x = x - 5;   }
        if (x >= 4)   { cout << "IV"; x = x - 4;   }
        if (x >= 3)   { cout << "III"; x = x - 3;   }
        if (x >= 2)   { cout << "II"; x = x - 2;   }
        if (x >= 1)   { cout << "I"; x = x - 1;   }
    }
    cout << endl;
    cin.ignore();
    return 0;
}

Pueder ir al artículo principal:

Códigos Sencillos hechos en C++

15 thoughts on “Código C++ – Convertir Decimales a Números Romanos

  1. #include
    using namespace std;
    #include
    /* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

    int main(int argc, char** argv) {
    int numerosRomanos( int numero,unidades,decenas,cent,millar;
    cout<<" Bienvenido al conversor de decimal a romano!\n";
    cout<>numero;

    unidades=numero%10; numero /=10;
    decenas=numero%10; numero /=10;
    cent=numero%10; numero /=10;
    millar=numero%10; numero /=10;
    switch(millar){
    case 1: cout<<"M"; break;
    case 2: cout<<"MM"; break;
    case 3: cout<<"MMM"; break;
    }
    switch(cent)
    {
    case 1: cout<<"C"; break;
    case 2: cout<<"CC"; break;
    case 3: cout<<"CCC"; break;
    case 4: cout<<"CD"; break;
    case 5: cout<<"D"; break;
    case 6: cout<<"DC"; break;
    case 7: cout<<"DCC"; break;
    case 8: cout<<"DCCC";break;
    case 9: cout<<"CM"; break;
    }
    switch(decenas)
    {
    case 1: cout<<"X"; break;
    case 2: cout<<"XX"; break;
    case 3: cout<<"XXX"; break;
    case 4: cout<<"XL"; break;
    case 5: cout<<"L"; break;
    case 6: cout<<"LX"; break;
    case 7: cout<<"LXX"; break;
    case 8: cout<<"LXXX";break;
    case 9: cout<<"XC"; break;
    }
    switch(unidades)
    {
    case 1: cout<<"I"; break;
    case 2: cout<<"II"; break;
    case 3: cout<<"III"; break;
    case 4: cout<<"IV"; break;
    case 5: cout<<"V"; break;
    case 6: cout<<"VI"; break;
    case 7: cout<<"VII"; break;
    case 8: cout<<"VIII";break;
    case 9: cout<<"IX"; break;
    })

    return 0;
    }

  2. #include
    #include
    #include
    #include

    using namespace std;
    #define N 100

    class numero
    {
    private:
    int n;
    public:
    numero();
    numero(int a);
    ~numero();
    void ingresar();
    int imprimir();

    };

    class romano: public numero
    {
    private:
    char A[50];
    public:
    romano(int a1,char m[]);
    ~romano();
    char convertir();
    void imprime();

    };
    numero::numero()
    {
    n=0;
    }
    numero::numero(int a)
    {
    n=a;
    }
    numero::~numero()
    {

    }
    void numero::ingresar()
    {
    cout<<"ingresar numero"<>n;
    }
    int numero::imprimir()
    {
    return n;
    }
    romano::romano(int b,char m[]):numero(b)
    {
    strcpy(A,m);
    }
    romano:: ~romano()
    {

    }
    char romano::convertir()
    {
    int num=imprimir(), u=0, d=0, c=0, mill=0;
    char p[1000];
    strcpy(p,””);
    mill=num / 1000;
    num = num % 1000;
    c=num / 100;
    num = num % 100;
    d=num / 10;
    u = num % 10;

    switch(mill)
    {
    case 1:
    strcat(p,”M”);
    break;
    case 2:
    strcat(p,”MM”);
    break;
    case 3:
    strcat(p,”MMM”);
    break;

    default:
    break;
    }
    switch(c)
    {
    case 1:
    strcat(p,”C”);
    break;
    case 2:
    strcat(p,”CC”);
    break;
    case 3:
    strcat(p,”CCC”);
    break;
    case 4:
    strcat(p,”CD”);
    break;
    case 5:
    strcat(p,”D”);
    break;
    case 6:
    strcat(p,”DC”);
    break;
    case 7:
    strcat(p,”DCC”);
    break;
    case 8:
    strcat(p,”DCCC”);
    break;
    case 9:
    strcat(p,”CM”);
    break;

    default:
    break;
    }
    switch(d)
    {
    case 1:
    strcat(p,”X”);
    break;
    case 2:
    strcat(p,”XX”);
    break;
    case 3:
    strcat(p,”XXX”);
    break;
    case 4:
    strcat(p,”XL”);
    break;
    case 5:
    strcat(p,”L”);
    break;
    case 6:
    strcat(p,”LX”);
    break;
    case 7:
    strcat(p,”LXX”);
    break;
    case 8:
    strcat(p,”LXXX”);
    break;
    case 9:
    strcat(p,”XC”);
    break;

    default:
    break;
    }
    switch(u)
    {

    case 1:
    strcat(p,”I”);
    break;
    case 2:
    strcat(p,”II”);
    break;
    case 3:
    strcat(p,”III”);
    break;
    case 4:
    strcat(p,”IV”);
    break;
    case 5:
    strcat(p,”V”);
    break;
    case 6:
    strcat(p,”VI”);
    break;
    case 7:
    strcat(p,”VII”);
    break;
    case 8:
    strcat(p,”VIII”);
    break;
    case 9:
    strcat(p,”IX”);
    break;
    default:
    break;

    }
    strcpy(A,p);
    }

    void romano::imprime()
    {
    cout<<"el numero romano es "<<A<<endl;
    }

    int main()
    {
    char v[2]={"V"};
    romano M(5,v);
    M.ingresar();
    M.imprimir();
    M.convertir();
    M.imprime();
    }
    ahi esta 🙂

  3. perdon mi ignorancia pero soy muy nuevo en esto y pss al momento de que ejecuto el programa todo bien pero se cierra muy rapido alguien me puede dar una respuesta gracias

    1. ESTE PARE QUE NO SE TE CIEERRE NESESITAS LA LIBRERIA
      #include//este es el que le debes aumentar.
      #include
      using namespace std;
      int main()
      {
      int x;
      cout<<"ingrese un numero"<>x;
      if((x999))
      cout<<"Ingrese un numero entre 0-999"<=900) {cout<=500) {cout<=400) {cout<=300) {cout<=200) {cout<=100) {cout<=90) {cout<=50) {cout<=40) {cout<=30) {cout<=20) {cout<=10) {cout<=9) {cout<=5) {cout<=4) {cout<=3) {cout<=2) {cout<=1) {cout<<"I" ;x=x-1; }

      }
      cout<<endl;
      cin.ignore();return 0;
      getch();//tambien este aumentalo.
      }

Deja un comentario