How to convert a Decimal number to Roman number in C language
#include<stdio.h>
void fun2(char c1,char c2);
void fun1(char c,int n);
char roman[1000];
int i=0;
void main(){
int j;
long int number;
printf("Enter the number: ");
scanf("%d",&number);
while(number != 0){
if(number >= 1000){
fun1('M',number/1000);
number = number - (number/1000) * 1000;
}
else if(number >=500){
if(number < (500 + 4 * 100)){
fun1('D',number/500);
number = number - (number/500) * 500;
}
else{
fun2('C','M');
number = number - (1000-100);
}
}
else if(number >=100){
if(number < (100 + 3 * 100)){
fun1('C',number/100);
number = number - (number/100) * 100;
}
else{
fun2('L','D');
number = number - (500-100);
}
}
else if(number >=50){
if(number < (50 + 4 * 10)){
fun1('L',number/50);
number = number - (number/50) * 50;
}
else{
fun2('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10)){
fun1('X',number/10);
number = number - (number/10) * 10;
}
else{
fun1('X','L');
number = number - (50-10);
}
}
else if(number >=5){
if(number < (5 + 4 * 1)){
fun1('V',number/5);
number = number - (number/5) * 5;
}
else{
fun1('I','X');
number = number - (10-1);
}
}
else if(number >=1){
if(number < 4){
fun1('I',number/1);
number = number - (number/1) * 1;
}
else{
fun2('I','V');
number = number - (5-1);
}
}
}
printf("Roman number will be: ");
for(j=0;j<i;j++)
printf("%c",roman[j]);
return 0;
}
void fun2(char c1,char c2){
roman[i++] = c1;
roman[i++] = c2;
}
void fun1(char c,int n){
int j;
for(j=0;j<n;j++)
roman[i++] = c;
}
0 comments:
Post a Comment
You are most welcome for making comments here. I will consider your view and will reply you soon.