Open Source Java Project

Java Program to Convert Decimal Number to Roman Number

Objectives :

  • Convert Decimal number to Roman number
  • Convert Decimal numeral to Roman numeral
  • How to convert a decimal number to roman number
  • Write a program that converts a decimal number to Roman number.
  • Write a program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.
  • Write a program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.

 

Program : 

Java program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.

[sourcecode lang=”java”]

 

import java.util.Scanner;

public class DecimalToRoman {

private static String toRoman(int num) {
String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" };
int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 };
String result = "";

for (int i = 0; i < romanValues.length; i++) {
int numberInPlace = num / romanValues[i];
if (numberInPlace == 0) continue;
result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i – 1]:
new String(new char[numberInPlace]).replace("\0",romanCharacters[i]);
num = num % romanValues[i];
}
return result;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number : ");
int decimal = scanner.nextInt();
System.out.println(toRoman(decimal));
}

}

[/sourcecode]

Output :

Enter a number : 1234

MCCXXXIV

Java program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.

[sourcecode lang=”java”]

public class DecimalToRoman {

private static String toRoman(int num) {
String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" };
int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 };
String result = "";

for (int i = 0; i < romanValues.length; i++) {
int numberInPlace = num / romanValues[i];
if (numberInPlace == 0) continue;
result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i – 1]:
new String(new char[numberInPlace]).replace("\0",romanCharacters[i]);
num = num % romanValues[i];
}
return result;
}

public static void main(String[] args) {
if(args.length<1 || args.length>1){
System.out.println("Wrong input");
}else{
String number = args[0];
int decimal = Integer.parseInt(number);
System.out.println(toRoman(decimal));
}
}

}

[/sourcecode]

Get MAC Address using Java

Free Source Code

Free Source Code

Objective :

* How to get the MAC Address of a computer?
* How to get the MAC Address of a computer using Java?
* How to get the MAC Address of a computer via programming?

[sourcecode language=”java”]
import java.net.*;

class GetMac
{
public static void main(String arg[])
{
try
{
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
[/sourcecode]

Note : This code will return only the first net address; if you need other detils too, get a list.

Posted from WordPress for Android

Check your Operating System & its version

Free Source Code

Free Source Code

Objective :

  • How to check your operating system by programming?
  • How to check your operating system by Java programming?
  • Write Java program to check operating system & its version

Hi folks,

Hey guys, if you want to know which operating system you are using & its version, there are 2 ways to do it as follows;

  • Using Command Prompt
  • Using Programming

1. Using Command Prompt

  • Go to Start Menu
  • Go to Run
  • Type cmd & press Enter/return. Command Prompt will open.
  • Type ver & press Enter/return, you will get the Name of your OS & its Version that you are using
Here I have used, ver command one of the DOS commands which returns Name of Operating System and its version.

2. Using Programming

Here we are using a Java Program to find the operating system you are using & its version :
[sourcecode language=”java”]
package com.si.java.utility.os;

/**
*
* Language : Java
* File : MyOS.java
* Date : Tuesday, October 5, 2010
* @author Atul Palandurkar
* Email : atul.palandurkar@gmail.com
* Website : www.shardainfotech.com
* Blog : http://atulpalandurkar.wordpress.com
*
*/

public class MyOS
{
public static void main(String[] arg)
{
//To find the name of operating system.
String myOS = System.getProperty("os.name");
System.out.println("Operating System : " + myOS);

//To find the version of operating system.
String myOSVersion = System.getProperty("os.version");
System.out.println("Version : " + myOSVersion);
}
}
[/sourcecode]

So, what are you waiting for?
Use it to create various system utilities & have fun.