Chapter - 6: Functions And Pointers

Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: 1 - I, 5 - V, 10 - X, 50 - L, 100 - C, 500 - D, 1000 - M.


D
Sections
3
Exercises

Example:

Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv


#include<stdio.h>
#include<conio.h>

void rom(int);
void print(int, char);

int main()
{
	int yer;

	printf("Enter the year : ");
	scanf("%d", &yer);
	printf("\nRoman equivalent of %d is : ", yer);
	rom(yer);

	_getch();
	return 0;
}

void rom(int yer)
{
	int v, x, l, c, d, m;

	m = yer / 1000;
	print(m, 'm');
	yer %= 1000;

	d = yer / 500;
	print(d, 'd');
	yer %= 500;

	c = yer / 100;
	print(c, 'c');
	yer %= 100;

	l = yer / 50;
	print(l, 'l');
	yer %= 50;

	x = yer / 10;
	print(x, 'x');
	yer %= 10;

	v = yer / 5;
	print(v, 'v');
	yer %= 5;

	print(yer, 'i');
}

void print(int a, char c)
{
	int i;
	for (i = 0; i < a; i++)
		printf("%c", c);
}

© 2021 Garbage Valuegarbage value logo