C++ Examples

C++ Program to Convert Given integer to a Roman numeral

The Program in C++ Program to Convert Given integer to a roman numeral is given below:

#include <iostream>
#include <string>

using namespace std;

string intToRoman(int num) {
  string symbol[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  int value[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  string roman;
  for (int i = 0; num != 0; i++) {
    while (num >= value[i]) {
      num -= value[i];
      roman += symbol[i];
    }
  }
  return roman;
}

int main() {
  int num;
  cout << "Hello Codeuari Family,enter an integer here to find the Roman Numeral: ";
  cin >> num;
  cout << "Okay, the Roman numeral is: " << intToRoman(num) << endl;
  return 0;
}

Output:

Hello Codeuari Family,enter an integer here to find the Roman Numeral: 2023
Okay, the Roman numeral is: MMXXIII

Pro-Tips💡

In this program, the intToRoman function converts an integer to a Roman numeral by using arrays of symbols and values for the symbols and looping through the values,

subtracting the value from the integer and appending the corresponding symbol to a string, until the integer is zero.

The main function takes the user input for the integer and outputs its Roman numeral by calling the intToRoman function.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X