Ad Code

Responsive Advertisement

Integer to Roman using C program

 

Integer to Roman Conversion 

Using C program

Integer to Roman Numerals Conversion


Program for converting Integer to equivalent Roman:

C Program:

Output 1:

Output


Explanation:     

SYMBOL

VALUE

I

1

IV

4

V

5

IX

9

X

10

XL

40

L

50

XC

90

C

100

CD

400

D

500

CM

900

M

1000


consider the above table,

ü Symbol values are stored in the array sym[][20] and value are stored in the array a[].

ü a[j] is the character to be compared with a[i].

ü For loop is used for the iteration. since symbol are 13 values, i value starts with 12 to perform the loop with larger value then moving to the smaller value till '0'.


ü Let's consider 36 for num.

Process is described below in the  table,

ITERATIONS

i

i>=0

div=num/a[i]

num=num%a[i]

While

(div--)

Print sym[i]

i--

1st iteration

12

12>=0

true

div=36/a[12]

div=36/1000

div=0

num=36%a[12]

num=36%1000

num=36

While(0--)

false

Does not execute

11

2nd iteration

11

11>=0

true

div=36/a[11]

div=36/900

div=0

num=36%a[11]

num=36%900

num=36

While(0--)

false

Does not execute

10

3rd iteration

10

10>=0

true

div=36/a[10]

div=36/500

div=0

num=36%a[10]

num=36%500

num=36

While(0--)

false

Does not execute

9

4th  iteration

9

9>=0

true

div=36/a[9]

div=36/400

div=0

num=36%a[9]

num=36%400

num=36

While(0--)

false

Does not execute

8

5th iteration

8

8>=0

true

div=36/a[8]

div=36/100

div=0

num=36%a[8]

num=36%100

num=36

While(0--)

false

Does not execute

7

6th  iteration

7

7>=0

true

div=36/a[7]

div=36/90

div=0

num=36%a[7]

num=36%90

num=36

While(0--)

false

Does not execute

6

7th  iteration

6

6>=0

true

div=36/a[6]

div=36/50

div=0

num=36%a[6]

num=36%50

num=36

While(0--)

false

Does not execute

5

8th  iteration

5

5>=0

true

div=36/a[5]

div=36/40

div=0

num=36%a[5]

num=36%40

num=36

While(0--)

false

Does not execute

4

9th  iteration

4

4>=0

true

div=36/a[4]

div=36/10

div=3

 

num=36%a[4]

num=36%10

num=6

While(3--)

true

Sym[4]

Print X

 

 

 

 

 

 

While(2--)

true

Sym[4]

Print X

 

 

 

 

 

 

While(1--)

true

Sym[4]

Print X

 

 

 

 

 

 

While(0--)

false

Does not execute

3

10th  iteration

3

3>=0

true

div=6/a[3]

div=6/9

div=0

num=6%a[3]

num=6%9

num=6

While(0--)

false

Does not execute

2

11th  iteration

2

2>=0

true

div=36/a[2]

div=6/5

div=1

num=6%a[2]

num=6%5

num=1

While(1--)

true

Sym[2]

Print V

 

 

 

 

 

 

While(0--)

false

Does not execute

1

12th  iteration

1

1>=0

true

div=1/a[1]

div=1/4

div=0

num=1%a[1]

num=1%4

num=1

While(0--)

false

Does not execute

0

13th  iteration

0

0>=0

true

div=1/a[0]

div=1/1

div=1

num=1%a[0]

num=1%1

num=0

While(1--)

true

Sym[0]

Print I

 

 

 

 

 

 

While(0--)

false

Does not execute

-1

14th  iteration

-1

-1>=0

false

Comes out of the loop

ü Thus the final output will be as XXXVI.

Note: If you are using mobile then view in desktop site to get the better view of the table.



Post a Comment

0 Comments

Close Menu