Tuesday 12 March 2019

Find the absolute value of a double value using JAVA

Problem:

Find the absolute value of a double value using JAVA

Algorithm:
==========
1. Read the double value from the console.
2. verify the number is negative or not.
3. if it is negative value the apply again negative symbol.
4. if not return the given value as absolute value.

Solution:
============
import java.util.*;
public class AbsoluteDoubleValueFinder{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a double value");
double i = scanner.nextDouble();
if(i<0.0){
System.out.println("Absolute value for the given number is: " + -(i));
}
else {
System.out.println("Absolute value for the given number is: " + i);
}
}

}

Output:
=========

Enter a double value
-2.5
Absolute value for the given number is: 2.5

Enter a double value
2.3
Absolute value for the given number is: 2.3


Enter a double value
0.9

Absolute value for the given number is: 0.9

No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...