Program to Print Equivalent Roman Numbers for Decimal Numbers from 1 to N in C++
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long n, i, j;
cout<<"Enter Number upto which Roman Numbers to be printed: ";
cin>>n;
cout<<"===========================================\n";
cout<<setw(10)<<"Decimal"<<"\tRoman No.\n";
cout<<"===========================================\n";
for(i=1;i<=n;i++)
{
cout<<setw(10)<<i<<"\t";
j=i;
while(j>0)
{
if(j>=1000)
{
j=j-1000;
cout<<"M";
}
else if(j>=500)
{
j=j-500;
cout<<"D";
}
else if(j>=100)
{
j=j-100;
cout<<"C";
}
else if(j>=10)
{
j=j-10;
cout<<"X";
}
else if(j==9)
{
j=j-9;
cout<<"IX";
}
else if(j>=5)
{
j=j-5;
cout<<"V";
}
else if(j==4)
{
j=j-4;
cout<<"IV";
}
else if(j>0 && j<=3)
{
j=j-1;
cout<<"I";
}
}
cout<<endl;
}
cout<<"===========================================\n";
}
#include<iomanip>
using namespace std;
int main()
{
long n, i, j;
cout<<"Enter Number upto which Roman Numbers to be printed: ";
cin>>n;
cout<<"===========================================\n";
cout<<setw(10)<<"Decimal"<<"\tRoman No.\n";
cout<<"===========================================\n";
for(i=1;i<=n;i++)
{
cout<<setw(10)<<i<<"\t";
j=i;
while(j>0)
{
if(j>=1000)
{
j=j-1000;
cout<<"M";
}
else if(j>=500)
{
j=j-500;
cout<<"D";
}
else if(j>=100)
{
j=j-100;
cout<<"C";
}
else if(j>=10)
{
j=j-10;
cout<<"X";
}
else if(j==9)
{
j=j-9;
cout<<"IX";
}
else if(j>=5)
{
j=j-5;
cout<<"V";
}
else if(j==4)
{
j=j-4;
cout<<"IV";
}
else if(j>0 && j<=3)
{
j=j-1;
cout<<"I";
}
}
cout<<endl;
}
cout<<"===========================================\n";
}
No comments
Post your comments