logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Convert Roman Number to Decimal Number

Posted on February 14, 2013 by staff10

This is a C Program to Convert Roman Number to Decimal Number.

Problem Description

This program takes a roman number as input and converts it to decimal number.

Problem Solution

1. Take a roman number as input.
2. Using switch statement define the value of each roman digit.
3. Through switch statement access each digit of a roman number and compute the value.
4. Print the value and exit.

Program/Source Code

Here is source code of the C program to Convert Roman Number to Decimal Number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2. /*
  3.  * C Program to Convert Roman Number to Decimal Number
  4.  */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8.  
  9. int digit(char);
  10.  
  11. int main(){
  12.  
  13.     char roman_Number[1000];
  14.     int i=0;
  15.     long int number =0;
  16.  
  17.     printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M):  \n");
  18.     scanf("%s",roman_Number);
  19.  
  20.     while(roman_Number[i]){
  21.  
  22.          if(digit(roman_Number[i]) < 0){
  23.              printf("Invalid roman digit : %c",roman_Number[i]);
  24.              return 0;
  25.          }
  26.  
  27.          if((strlen(roman_Number) -i) > 2){
  28.              if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
  29.                  printf("Invalid roman number");
  30.                  return 0;
  31.              }
  32.          }
  33.  
  34.          if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))
  35.              number = number + digit(roman_Number[i]);
  36.          else{
  37.              number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
  38.              i++;
  39.          }
  40.          i++;
  41.     }
  42.  
  43.     printf("Its decimal value is : %ld",number);
  44.  
  45.     return 0;
  46.  
  47. }
  48.  
  49. int digit(char c){
  50.  
  51.     int value=0;
  52.  
  53.     switch(c){
  54.          case 'I': value = 1; break;
  55.          case 'V': value = 5; break;
  56.          case 'X': value = 10; break;
  57.          case 'L': value = 50; break;
  58.          case 'C': value = 100; break;
  59.          case 'D': value = 500; break;
  60.          case 'M': value = 1000; break;
  61.          case '\0': value = 0; break;
  62.          default: value = -1; 
  63.     }
  64.  
  65.     return value;
  66. }
Program Explanation

1. Take a roman number as input and store it in the array roman_Number.
2. In the function digit(), define the value of each digit of the roman number inside the switch statement and return the same.
3. Using while statement access each digit of the input number.
4. Firstly check if the current roman digit’s value is less than zero. If it is, then print the output as “Invalid roman digit”.
5. If not, then check if the value of current roman digit is greater or equal to its next digit’s value. If it is, then increment the variable number with the value of current roman digit.
6. Otherwise, subtract the value of current roman digit from the value of its next roman digit and increment the variable number with the obtained value.
7. Print the variable number as output.

Runtime Test Cases
Output:
Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XVII
Its decimal value is: 17

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C program to Calculate the Value of nPr
» Next Page - C program to Calculate the value of nCr
« C program to Calculate the Value of nPr
C program to Calculate the value of nCr »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples on Threads
  2. C# Basic Programming Examples
  3. Python Programming Examples
  4. C# Programming Examples on Functions
  5. C Questions and Answers
  6. C Programming Examples on Strings
  7. C Programming Examples using Recursion
  8. C# Programming Examples on Strings
  9. C# Programming Examples on Conversions
  10. Simple C Programs
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry