Conversion from Decimal to roman number

Here given code implementation process.

//C Program
//Conversion from Decimal to roman number
#include <stdio.h>

//Display roman value of n
void show(int n)
{
  switch(n)
  {
    //Test Cases
    case 1:
    printf("I"); break;

    case 4:
    printf("IV"); break;

    case 5:
    printf("V"); break;

    case 9:
    printf("IX"); break;

    case 10:
    printf("X"); break;

    case 40:
    printf("XL"); break;

    case 50:
    printf("L"); break;

    case 90:
    printf("XC"); break;

    case 100:
    printf("C"); break; 

    case 400:
    printf("CD"); break;

    case 500:
    printf("D"); break;

    case 900:
    printf("DM"); break; 

    case 1000:
    printf("M"); break;    
  }
}
long long select(long long number,int collection[],int size)
{

  int n = 1,i=0;

  for (i = 0; i < size; ++i)
  {
    if(number >= collection[i])
    {
      n = collection[i];
    }
    else
    {
      break;
    }

  }

  show(n); 
  //Reduce the value of number
  return number-n;
}
void roman(long long number)
{
  if(number<=0)
  {
    return;
  }
  //Base case collection
  int collection[]={1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
  
  //Get the size of collection
  int size = sizeof(collection)/sizeof(collection[0]);
  printf("%lld : ",number );
  while(number>0)
  {
    number=select(number,collection,size);
  }
  printf("\n");
}
int main()
{
 
  //Test Case
  roman(10);
  roman(18);
  roman(189);
  roman(604);
  roman(982);
  roman(3000);
  return 0;
}

Output

10 : X
18 : XVIII
189 : CLXXXIX
604 : DCIV
982 : DMLXXXII
3000 : MMM
#include<iostream>

using namespace std;

/*
  C++ Program
  Conversion from Decimal to roman number
*/
class MyNumber {
  public:

    //Display roman value of n
    void show(int n) {
      switch (n) {
        case 1:
          cout << "I";
          break;
        case 4:
          cout << "IV";
          break;
        case 5:
          cout << "V";
          break;
        case 9:
          cout << "IX";
          break;
        case 10:
          cout << "X";
          break;
        case 40:
          cout << "XL";
          break;
        case 50:
          cout << "L";
          break;
        case 90:
          cout << "XC";
          break;
        case 100:
          cout << "C";
          break;
        case 400:
          cout << "CD";
          break;
        case 500:
          cout << "D";
          break;
        case 900:
          cout << "DM";
          break;
        case 1000:
          cout << "M";
          break;
      }
    }
  long select(long number, int collection[], int size) {
    int n = 1, i = 0;
    for (i = 0; i < size; ++i) {
      if (number >= collection[i]) {
        n = collection[i];
      } else {
        break;
      }
    }
    this->show(n);
    return number - n;
  }
  void roman(long number) {
    if (number <= 0) {
      return;
    }
    int collection[] = {
      1,
      4,
      5,
      9,
      10,
      40,
      50,
      90,
      100,
      400,
      500,
      900,
      1000
    };
    //Get the size of collection
    int size = sizeof(collection) / sizeof(collection[0]);
    cout << " " << number << " : ";
    while (number > 0) {
      number = this->select(number, collection, size);
    }
    cout << "\n";
  }
};
int main() {
  MyNumber obj ;
  //Test Case
  obj.roman(10);
  obj.roman(18);
  obj.roman(189);
  obj.roman(604);
  obj.roman(982);
  obj.roman(3000);
  return 0;
}

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM
using System;

/*
  C# Program
  Conversion from Decimal to roman number
*/

public class MyNumber {
  //Display roman value of n
  public void show(int n) {
    switch (n) {
      case 1:
        Console.Write("I");
        break;;
      case 4:
        Console.Write("IV");
        break;;
      case 5:
        Console.Write("V");
        break;;
      case 9:
        Console.Write("IX");
        break;;
      case 10:
        Console.Write("X");
        break;;
      case 40:
        Console.Write("XL");
        break;;
      case 50:
        Console.Write("L");
        break;;
      case 90:
        Console.Write("XC");
        break;;
      case 100:
        Console.Write("C");
        break;;
      case 400:
        Console.Write("CD");
        break;;
      case 500:
        Console.Write("D");
        break;;
      case 900:
        Console.Write("DM");
        break;;
      case 1000:
        Console.Write("M");
        break;;
    }
  }
  public long select(long number, int[] collection, int size) {
    int n = 1, i = 0;
    for (i = 0; i < size; ++i) {
      if (number >= collection[i]) {
        n = collection[i];
      } else {
        break;;
      }
    }
    show(n);
    return number - n;
  }
  public void roman(long number) {
    if (number <= 0) {
      return;
    }
    int[]
    //Base case collection
    collection = {
      1,
      4,
      5,
      9,
      10,
      40,
      50,
      90,
      100,
      400,
      500,
      900,
      1000
    };
    //Get the size of collection
    int size = collection.Length;
    Console.Write(" " + number + " : ");
    while (number > 0) {
      number = select(number, collection, size);
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyNumber obj = new MyNumber();
    obj.roman(10);
    obj.roman(18);
    obj.roman(189);
    obj.roman(604);
    obj.roman(982);
    obj.roman(3000);
  }
}

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM
<?php
/*
  Php Program
  Conversion from Decimal to roman number
*/
class MyNumber {
  //Display roman value of n

  public  function show($n) {
    switch ($n) {
      case 1:
        echo("I");
        break;
      case 4:
        echo("IV");
        break;
      case 5:
        echo("V");
        break;
      case 9:
        echo("IX");
        break;
      case 10:
        echo("X");
        break;
      case 40:
        echo("XL");
        break;
      case 50:
        echo("L");
        break;
      case 90:
        echo("XC");
        break;
      case 100:
        echo("C");
        break;
      case 400:
        echo("CD");
        break;
      case 500:
        echo("D");
        break;
      case 900:
        echo("DM");
        break;
      case 1000:
        echo("M");
        break;
    }
  }
  public  function select($number, $collection, $size) {
    $n = 1;
    $i = 0;
    for ($i = 0; $i < $size; ++$i) {
      if ($number >= $collection[$i]) {
        $n = $collection[$i];
      } else {
        break;
      }
    }
    $this->show($n);
    return $number - $n;
  }
  public  function roman($number) {
    if ($number <= 0) {
      return;
    }
    //Base case collection
    $collection = array(1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);
    //Get the size of collection
    $size = count($collection);
    echo(" ". $number ." : ");
    while ($number > 0) {
      $number = $this->select($number, $collection, $size);
    }
    echo("\n");
  }
};

function main() {
  $obj = new MyNumber();
  //Test Case

  $obj->roman(10);
  $obj->roman(18);
  $obj->roman(189);
  $obj->roman(604);
  $obj->roman(982);
  $obj->roman(3000);

}
main();

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM
/*
  Node Js Program
  Conversion from Decimal to roman number
*/
class MyNumber {
  //Display roman value of n
  show(n) {
    switch (n) {
      case 1:
        process.stdout.write("I");
        break;
      case 4:
        process.stdout.write("IV");
        break;
      case 5:
        process.stdout.write("V");
        break;
      case 9:
        process.stdout.write("IX");
        break;
      case 10:
        process.stdout.write("X");
        break;
      case 40:
        process.stdout.write("XL");
        break;
      case 50:
        process.stdout.write("L");
        break;
      case 90:
        process.stdout.write("XC");
        break;
      case 100:
        process.stdout.write("C");
        break;
      case 400:
        process.stdout.write("CD");
        break;
      case 500:
        process.stdout.write("D");
        break;
      case 900:
        process.stdout.write("DM");
        break;
      case 1000:
        process.stdout.write("M");
        break;
    }
  }
  select(number, collection, size) {
    var n = 1;
    var i = 0;
    for (i = 0; i < size; ++i) {
      if (number >= collection[i]) {
        n = collection[i];
      } else {
        break;
      }
    }
    this.show(n);
    return number - n;
  }
  roman(number) {
    if (number <= 0) {
      return;
    }

    //Base case collection
    var collection = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
    //Get the size of collection
    var size = collection.length;
    process.stdout.write(" " + number + " : ");
    while (number > 0) {
      number = this.select(number, collection, size);
    }

    process.stdout.write("\n");
  }
}

function main(args) {
  var obj = new MyNumber();
  //Test Case
  obj.roman(10);
  obj.roman(18);
  obj.roman(189);
  obj.roman(604);
  obj.roman(982);
  obj.roman(3000);
}

main();

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM
#   Python 3 Program
#   Conversion from Decimal to roman number
class MyNumber :
  # Display roman value of n
  def show(self, n) :
    switcher = {
      1: "I",
      4: "IV",
      5: "V",
      9: "IX",
      10: "X",
      40: "XL",
      50: "L",
      90: "XC",
      100: "C",
      400: "CD",
      500: "D",
      900: "DM",
      1000: "M",
    }
    print(switcher.get(n, ""),end="")
  
  def select(self, number, collection, size) :
    n = 1
    i = 0
    i = 0
    while (i < size) :
      if (number >= collection[i]) :
        n = collection[i]
      else:
        break
      i += 1
    
    self.show(n)
    return number - n
  
  def roman(self, number) :
    if (number <= 0) :
      return
    
    collection = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
    size = len(collection)
    print(" ", number ," : ", end = "")
    while (number > 0) :
      number = self.select(number, collection, size)
    
    print("\n", end = "")
  

def main() :
  obj = MyNumber()
  obj.roman(10)
  obj.roman(18)
  obj.roman(189)
  obj.roman(604)
  obj.roman(982)
  obj.roman(3000)


if __name__ == "__main__":
  main()

Output

  10  : X
  18  : XVIII
  189  : CLXXXIX
  604  : DCIV
  982  : DMLXXXII
  3000  : MMM
#   Ruby Program
#   Conversion from Decimal to roman number
class MyNumber 
  # Display roman value of n
  def show(n) 
    if (n == 1) 

      print("I")
    
    elsif (n == 4) 

      print("IV")
    
    elsif (n == 5) 

      print("V")
    
    elsif (n == 9) 

      print("IX")
    
    elsif (n == 10) 

      print("X")
    
    elsif (n == 40) 

      print("XL")
    
    elsif (n == 50) 

      print("L")
    
    elsif (n == 90) 

      print("XC")
    
    elsif (n == 100) 

      print("C")
    
    elsif (n == 400) 

      print("CD")
    
    elsif (n == 500) 

      print("D")
    
    elsif (n == 900) 

      print("DM")
    
    elsif (n == 1000) 

      print("M")
    end
  end
  def select(number, collection, size) 
    n = 1
    i = 0
    i = 0
    while (i < size) 
      if (number >= collection[i]) 
        n = collection[i]
      else 
        break
      end
      i += 1
    end
    self.show(n)
    return number - n
  end
  def roman(number) 
    if (number <= 0) 
      return
    end
    collection = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
    size = collection.length
    print(" ", number ,"  :")
    while (number > 0) 
      number = self.select(number, collection, size)
    end
    print("\n")
  end
end
def main() 
  obj = MyNumber.new()
  obj.roman(10)
  obj.roman(18)
  obj.roman(189)
  obj.roman(604)
  obj.roman(982)
  obj.roman(3000)
end
main()

Output

 10  :X
 18  :XVIII
 189  :CLXXXIX
 604  :DCIV
 982  :DMLXXXII
 3000  :MMM
/*
  Scala Program
  Conversion from Decimal to roman number
*/
class MyNumber {
  //Display roman value of n
  def show(n: Int): Unit = {
    if (n == 1) {
      print("I");

    };
    else if (n == 4) {
      print("IV");

    };
    else if (n == 5) {
      print("V");

    };
    else if (n == 9) {
      print("IX");

    };
    else if (n == 10) {
      print("X");

    };
    else if (n == 40) {
      print("XL");

    };
    else if (n == 50) {
      print("L");

    };
    else if (n == 90) {
      print("XC");

    };
    else if (n == 100) {
      print("C");

    };
    else if (n == 400) {
      print("CD");

    };
    else if (n == 500) {
      print("D");

    };
    else if (n == 900) {
      print("DM");

    };
    else if (n == 1000) {
      print("M");

    };
  }
  def select(number: Long, collection: Array[Int], size: Int): Long = {
    var n: Int = 1;
    var i: Int = 0;
    i = 0;
    while (i < size) {
      if (number >= collection(i)) {
        n = collection(i);
      } else {
                //Break condition
        i+=size;
      }
      i += 1;
    }
    this.show(n);

    return number - n;
  }
  def roman(n : Long): Unit = {
        var number : Long = n;
    if (number <= 0) {
      return;
    }
    val collection: Array[Int] = Array(1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);
    val size: Int = collection.length;
    print(" " + number + " : ");
    while (number > 0) {
      number = this.select(number, collection, size);
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val obj: MyNumber = new MyNumber();
    obj.roman(10);
    obj.roman(18);
    obj.roman(189);
    obj.roman(604);
    obj.roman(982);
    obj.roman(3000);
  }
}

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM
/*
  Swift Program
  Conversion from Decimal to roman number
*/
class MyNumber {
  //Display roman value of n
  func show(_ n: Int) {
    if (n == 1) {
      print("I", terminator: "");
    }
    else if (n == 4) {
      print("IV", terminator: "");
    }
    else if (n == 5) {
      print("V", terminator: "");
    }
    else if (n == 9) {
      print("IX", terminator: "");
    }
    else if (n == 10) {
      print("X", terminator: "");
    }
    else if (n == 40) {
      print("XL", terminator: "");
    }
    else if (n == 50) {
      print("L", terminator: "");
    }
    else if (n == 90) {
      print("XC", terminator: "");
    }
    else if (n == 100) {
      print("C", terminator: "");
    }
    else if (n == 400) {
      print("CD", terminator: "");
    }
    else if (n == 500) {
      print("D", terminator: "");
    }
    else if (n == 900) {
      print("DM", terminator: "");
    }
    else if (n == 1000) {
      print("M", terminator: "");
    }
  }
  func select(_ number: Int, _ collection: [Int], _ size: Int) -> Int {
    var n: Int = 1;
    var i: Int = 0;
    i = 0;
    while (i < size) {
      if (number >= collection[i]) {
        n = collection[i];
      } else {
        break;
      }
      i += 1;
    }
    self.show(n);
    return number - n;
  }
  func roman(_ n: Int) {
        var number: Int = n;
    if (number <= 0) {
      return;
    }
    let collection: [Int] = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
    let size: Int = collection.count;
    print(" ", number ," : ", terminator: "");
    while (number > 0) {
      number = self.select(number, collection, size);
    }
    print("\n", terminator: "");
  }
}
func main() {
  let obj: MyNumber = MyNumber();
  obj.roman(10);
  obj.roman(18);
  obj.roman(189);
  obj.roman(604);
  obj.roman(982);
  obj.roman(3000);
}
main();

Output

  10  : X
  18  : XVIII
  189  : CLXXXIX
  604  : DCIV
  982  : DMLXXXII
  3000  : MMM
/*
  Java Program
  Conversion from Decimal to roman number
*/
public class MyNumber {

  //Display roman value of n
  public void show(int n)
  {
    switch(n)
    {
      //Test Cases
      case 1:
      System.out.print("I"); break;

      case 4:
      System.out.print("IV"); break;

      case 5:
      System.out.print("V"); break;

      case 9:
      System.out.print("IX"); break;

      case 10:
      System.out.print("X"); break;

      case 40:
      System.out.print("XL"); break;

      case 50:
      System.out.print("L"); break;

      case 90:
      System.out.print("XC"); break;

      case 100:
      System.out.print("C"); break; 

      case 400:
      System.out.print("CD"); break;

      case 500:
      System.out.print("D"); break;

      case 900:
      System.out.print("DM"); break; 

      case 1000:
      System.out.print("M"); break;    
    }
  }
  public long  select(long  number,int []collection,int size)
  {

    int n = 1,i=0;

    for (i = 0; i < size; ++i)
    {
      if(number >= collection[i])
      {
        n = collection[i];
      }
      else
      {
        break;
      }

    }

    show(n); 
    //Reduce the value of number
    return number-n;
  }
  public void roman(long  number)
  {
    if(number<=0)
    {
      return;
    }
    //Base case collection
    int []collection={1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
    
    //Get the size of collection
    int size = collection.length;
    System.out.print(" "+number+" : " );
    while(number>0)
    {
      number=select(number,collection,size);
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {
  

    MyNumber obj = new MyNumber();
   
    //Test Case
    obj.roman(10);
    obj.roman(18);
    obj.roman(189);
    obj.roman(604);
    obj.roman(982);
    obj.roman(3000);
  }
}

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved