Print Equivalent Decimal & Roman Numbers Series 1 to N in C++


#include<iostream>
#include<iomanip>
#include<math.h>

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++)
{
j=pow(2,i-1);
cout<<setw(10)<<j<<"\t";
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

Powered by Blogger.