# Aatul Palandurkar

## Archive for the ‘Free Source Code’ Category

### Java Program to convert Decimal values to Hexadecimal values

Posted on: April 20, 2016

Objectives :

• How to convert Decimal to Hexadecimal in Java?

### Java Program to convert Decimal values to Hexadecimal values :

```
import java.util.Scanner;

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter decimal number you like: ");
int deci = input.nextInt();

System.out.println("The hexadecimal number for decimal "
+ deci + " is " + convert(deci));
}

public static String convert(int decimal) {
String hex = "";

while (decimal != 0) {
int hexValue = decimal % 16;
decimal = decimal / 16;
}

return hex;
}

public static char toHexadecimal(int hexValue) {
if (hexValue <= 9 && hexValue >= 0) {
return (char) (hexValue + '0');
} else {
return (char) (hexValue - 10 + 'A');
}
}
}

```

Output :
Enter decimal number you like: 1234
The hexadecimal number for decimal 1234 is 4D2

### Java Program to Convert Decimal Number to Roman Number

Posted on: March 28, 2016

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.

```
&nbsp;

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));
}

}

```

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.

```
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));
}
}

}

```

### Passing data between activities in Android

Posted on: March 19, 2016

Objective :

• Passing data between activities in Android
• Passing multiple data between activities in Android
• Passing array between activities in Android
• Passing ArrayList between activities in Android
• Passing ArrayList to another activity in Android
• Sending data via Intent in Android
• Sending multiple data via Intent in Android
• Sending array between activities in Android
• Sending ArrayList between activities in Android
• Sending ArrayList to another activity in Android
• Sending data via Intent and Bundle in Android
• Sending multiple data via Intent and Bundle in Android
• Passing array between activities in Android using Intent and Bundle
• How to pass array to another activity in Android?
• How to pass array between activities in Android?
• How to pass data to another activity in Android?
• How to pass data to activity in Android?
• How to pass data to between activities in Android?
• How to pass ArrayList to another activity in Android?
• How to pass ArrayList between activities in Android?

## Code for passing data between activities in Android :

ActivityOne.java

```
String value = "Hello!";
Intent in = new Intent(this,ActivityTwo.class);
in = putExtra("Key", value);
startActivity(in);

```

ActivityTwo.java

```
Bundle bundle = getIntent().getExtras();

```

## Code for passing multiple data or values between activities in Android :

### Method 1 : Using Intent to pass data and Bundle to extract data between activities in Android

ActivityOne.java

```
String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class);
in = putExtra("Key1", value1); startActivity(in);
in = putExtra("Key2", value2); startActivity(in);
```

ActivityTwo.java

```Bundle bundle = getIntent().getExtras();
```

### Method 2 : Using Bundle to pass and to extract data between activities in Android

ActivityOne.java

```
String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putString("Key1", value1);
bundle.putString("Key2", value2);
in = putExtras(bundle);
startActivity(in);
```

ActivityTwo.java

```Bundle bundle = getIntent().getExtras();
```

## Code for passing array between activities in Android :

ActivityOne.java

```String[] array = new String[]{"Item1", "Item2", "item3", "Item4", "item5"};
Intent in = new Intent(this,ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putStringArray("MyArray", array);
in = putExtras(bundle);
startActivity(in);
```

ActivityTwo.java

```Bundle bundle = getIntent().getExtras();
```

## Code for passing ArrayList between activities in Android :

ActivityOne.java

```ArrayList<String> array = new ArrayList<String>();
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("array_list", array);
startActivity(intent);
```

ActivityTwo.java

```Bundle bundle = getIntent().getExtras();
ArrayList<String> array = (ArrayList<String>) bundle.getStringArrayList("array_list");
```

### Java Program to Convert Binary to Decimal

Posted on: March 16, 2016

Objectives :

• Binary to Decimal Conversion
• Converting Binary to Decimal
• Write a program to convert binary to decimal
• Write a program to convert binary number to decimal format
• Write a program to convert binary to decimal using Scanner class
• Write a program to convert binary to decimal, take input using Scanner class

Java Program :

```
import java.util.Scanner;

public class BinaryToDecimal {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(&quot;Enter binary number: &quot;);
String binary = scanner.nextLine();
int decimal = binaryToDecimal(binary);
System.out.println(&quot;Decimal equivalent of &quot;+ binary +&quot; is &quot;+ decimal);
}

private static int binaryToDecimal(String binary) {
final int base = 2;
int decimal = 0;
for (int i = 0; i &lt; binary.length(); i++) {
if (binary.charAt(i) == '0') {
decimal += 0 * Math.pow(base, binary.length() - i - 1);
} else if (binary.charAt(i) == '1') {
decimal += 1 * Math.pow(base, binary.length() - i - 1);
} else {
System.out.println(&quot;Invalid Binary Number&quot;);
System.out.println(&quot;Binary Number Contains only 0's or 1's&quot;);
System.exit(0);
}
}
return decimal;
}
}

```

Output :
Enter binary number: 1110
Decimal equivalent of 1110 is 14

### Program to find Sum of Digits in Java

Posted on: February 23, 2016

Objectives :

• Write a program to compute sum of digits of a given number.
• Write a program to find sum of digits of a given number.
• Write a program to calculate sum of digits of a given number.
• Write a program to compute sum of digits of a given integer number.
• Write a program to compute sum of digits of a number entered via Command Line.
• Write a program to compute sum of digits of a given number. Take input from Command Line.
• Write a program to compute sum of digits of a given number. Take input using Scanner class.

Following is the Java Program to compute Sum of Digits of a given integer number;

Method 1 : Java Program to find Sum of Digits when number is entered from command line

```
class SumOfDigits
{
public static void main(String args[])
{
int n;
int a=0;
int sum=0;

//taking integer number from command line and parsing the same
n=Integer.parseInt(args[0]);

while(n!=0)
{
a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}

```

Steps to run above program via command line :

1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output : Sum of digits: 15

Method 2 : Java Program to find Sum of Digits if input is taken using Scanner class

```
import java.util.Scanner;

public class SumOfDigits {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n;
int a=0;
System.out.print("Enter a positive number: ");
n = in.nextInt();

if (n <= 0)
System.out.println("You have entered a negative number.");
else {
int sum = 0;

while (n != 0) {

a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}
}

```

Steps to run above program via command line :

1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output :

Trial 1 : With positive number

Enter a positive number: 12345

Sum of digits: 15

Trial 2 : With negative number

Enter a positive number: -12345

You have entered a negative number.