Jun 14

How to add two BigDecimal values in java?

This program demonstrates how to add two big decimal numbers in java.

Method : public BigDecimal add(BigDecimal augend):

Number of arguments : 1

Return value : BigDecimal

[code lang=”java”]import java.math.BigDecimal;

public class BigDecimalAddDemo {

public static void main(String[] args) {

BigDecimal bigDecimal3;

BigDecimal bigDecimal1=new BigDecimal(123.3256);

BigDecimal bigDecimal2=new BigDecimal(345.768);

bigDecimal3=bigDecimal1.add(bigDecimal2);

System.out.println(bigDecimal3);

}

}
[/code]Output:

469.0935999999999665988070773892104625701904296875

Jun 04

Write a java program to accept 3 numbers and print the max value

Write a program to accept 3 numbers and print the maximum value

This program demonstrates to find greatest of 3 numbers .

[code lang=”java”]public class Greatest
{
public static void main(String[] args)
{
greatestNumber(3, 100, 1);

greatestNumber(111, 12, 5);
}

private static void greatestNumber(int num1, int num2, int num3) {

if(num1>num2)
{
if(num1>num3)
{
System.out.println("num1 is greater "+num1);
}
else
{
System.out.println("num3 is greater "+num3);
}
}
else
{
if(num2>num3)
{
System.out.println("num2 is greater "+num2);
}
else
{
System.out.println("num3 is greater "+num3);
}
}
}
}[/code]

Output

num2 is greater 100
num1 is greater 111