Convert any number into roman number


#include <stdio.h>
int numbercount(int);//function to count the number of digits.
int main (int argc, const char * argv[])
{
  
    int number;
    printf("\nEnter the number:");    scanf("%d",&number);
  
    //number = numbercount(number);
    // for calculating 1000th position
    //----------------------------------
    int quotient = number/1000;
    if (quotient>=1)
    {
        number = number%1000;
        for (int i=1; i<=quotient; i++)
        {
            printf("m");
        }
    }
  
    //for calculating 100th position
    //-----------------------------------
    int hunquotient = number/100;
  
    if (hunquotient>=1)
    {
      
        if (hunquotient>=5)
        {
            printf("d");
            hunquotient = hunquotient - 5;
        }
      
        number = number%100;
        for (int j=1; j<=hunquotient && hunquotient!=0; j++)
        {
            printf("c");
        }
    }
  
    //for calculating 10th position
    //-----------------------------------
  
    int tenquotient = number/10;
  
    if (tenquotient>=1)
    {
        if (tenquotient>=5) {
            printf("l");
            tenquotient = tenquotient-5;
        }
      
        number = number%10;
        for (int k=1; k<=tenquotient && tenquotient!=0; k++)
        {
            printf("x");
        }
    }
  
    //for calculating last digit
    //-------------------------------------
  
    if (number>=5) {
        number = number - 5;
        printf("v");
    }
    for (int l=1; l<=number; l++) {
        printf("i");
    }

    return 0;
}
//function to count the number of digits
int numbercount(int num)
{
    int count = 0;
    while (num>0)
    {
        num = num / 10;
        count++;
    }
  
    return count;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

2 comments:

Unknown said...

Nyc Programs

Ashar Khan said...

I created an alternative:

int main()
{
int num;
printf("Enter a number\n");
scanf(" %d", &num);
roman(num);
}


roman(num)
int num;
{
if (num == 0)
printf("0");
while (num >= 1000)
{
printf("M");
num = num - 1000;
}
while (num >= 500)
{
if (num >= 900)
{
printf("CM");
num = num - 900;
}
else
{
printf("D");
num = num - 500;
}
}
while (num >= 100)
{
if (num >= 400)
{
printf("CD");
num = num - 400;
}
else
{
printf("C");
num = num - 100;
}
}
while (num >= 50)
{
if (num >= 90)
{
printf("XC");
num = num - 90;
}
else
{
printf("L");
num = num - 50;
}
}
while (num >= 10)
{
if (num >= 40)
{
printf("XL");
num = num - 40;
}
else
{
printf("X");
num = num - 10;
}
}
while (num >= 5)
{
if (num == 9)
{
printf("IX");
num = num - 9;
}
else
{
printf("V");
num = num - 5;
}
}
while (num >= 1)
{
if (num == 4)
{
printf("IV");
num = num - 4;
}
else
{
printf("I");
num = num - 1;
}
}

}

Post a Comment