Thursday 13 September 2012

To convert roman number to it’s decimal equivalent


Description:
In this program we have to take the roman value. This value is converted into a it’s equivalent decimal number.
 Ex:   X=10
Algoritham:
Step 1: Start
Step 2: read the roman numerical as string
Step 3: find length of roman numerical
Step 4: for each charcter in the string
i)                    if(char=I) then decimal=1
ii)                  if(char=V) then decimal=5
iii)                if(char=X) then decimal=10
iv)                if(char=L) then decimal=50
v)                  if(char=C) then decimal=100
vi)                if(char=D) then decimal=500
vii)              if(char=M) then decimal=1000
viii)            otherwise invalid character
Step 5: repeat step 4 until the length of the string
Step 6: k=char[length-1]
Step 7: for each character of decimal string
i)                    if(decimal[i]>dec[i-1]) then k=k-decimal[i-1]
ii)                  else if(decimal[i]=decimal[i-1 or decimal[i]<decimal[i-1) then k=k+decimall[i-1]
Step 8: repate step 7 until the length of decimal string
Step 9: print decimal value
Step 10: Stop


 Program:
        

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

void main()
{

 int *a,len,i,j,k;
 char *rom;

 clrscr();

 printf("Enter the Roman Numeral:");
 scanf("%s",rom);

 len=strlen(rom);

 for(i=0;i<len;i++) // loop will continue until I is not graterthan length.
 {
             if(rom[i]=='I')
             a[i]=1;
             else if(rom[i]=='V')
             a[i]=5;
             else if(rom[i]=='X')
  a[i]=10;
  else if(rom[i]=='L')
  a[i]=50;
             else if(rom[i]=='C')
  a[i]=100;
             else if(rom[i]=='D')
             a[i]=500;
  else if(rom[i]=='M')
  a[i]=1000;
  else
             {
                          printf("\nInvalid Value");
                           getch();
                          exit(0);
            }
}
k=a[len-1];
for(i=len-1;i>0;i--) // loop will continue until I lessthan zero
{
             if(a[i]>a[i-1])  // check the condition
            k=k-a[i-1];
  else if(a[i]==a[i-1] || a[i]<a[i-1])
            k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}

Output:

Enter the Roman Numeral:D

Its Decimal Equivalent is:500
Enter the Roman Numeral:X

Its Decimal Equivalent is:10
Enter the Roman Numeral:23

Invalid Value
Conclusion: The program is error free


VIVA QUESATIONS:

1) What is difference between structure and unions ?
Ans : The amount of memory required to store a structure variable is the sum of size all the members in addition to the padding bytes that may be provided by the compiler. In case of a union the amount of memory required is the same as that required by its largest member.
2) What are various operations performed on union ?
Ans:  i)An union variable can be assigned to another union variable
         ii) A union variable can be passed to a function as a parameter
         iii) The address of the union variable can be extracted by using the address of 
               operator (&).









No comments:

Post a Comment