Wednesday, March 28, 2012

C Language Program On Decimal to Roman Number Conversion


This is a program on number conversion using C programming language.


#include< stdio.h>
#include< string.h>
void main()
{
char r[13][10]  = {{"M"}, {"CM"}, {"D"}, {"CD"}, {"C"}, {"XC"}, {"L"}, {"XL"}, {"X"}, {"IX"}, {"V"}, {"IV"}, {"I"}};
    int d[]  = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
    int i,b;
   
char str[100];
while(1)
{
printf("Enter the decomal number:");
scanf("%d",&b);
if(b>0 && b<4000)
break;
}
for ( i = 0; i < 13; i++)
{
 while (b >= d[i])
 {
   b = b-d[i];
   strcat(str,r[i]);
 }
 }
        printf("Equivalent roman=%s",str);
getch();
    }  
    
Technical analysis of program


Both the decimal numbers and their equaivalent roman numbers are stored in two separate arrays. The outer loop is used to access the roman numbers and the inner loop extracts the decimal parts from the entered number and the matching roman number is concatenated on a char type array which holds the equivalent roman number.


2 comments:

  1. hello ... i m new learner of c language n m not able to create this output (in opposite v shape)
    1
    2 2
    3 3
    4 4
    5 5

    ReplyDelete
    Replies
    1. From today onwards C and C++ programs will be posted in my schooljava.blogspot.com. You will get the codes there in 'Programs using C Language' category.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner