Problem:
==============
Find the absolute value of an int value using JAVA
What is absolute value??
The absolute value of an integer is the numerical value without regard to whether the sign is negative or positive.
Algorithm:
==========
1. Read the integer 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 AbsoluteValueFinder{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int i = scanner.nextInt();
if(i<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 number
12
Absolute value for the given number is: 12
Enter a number
-12
Absolute value for the given number is: 12
Enter a number
-89
Absolute value for the given number is: 89
==============
Find the absolute value of an int value using JAVA
What is absolute value??
The absolute value of an integer is the numerical value without regard to whether the sign is negative or positive.
Algorithm:
==========
1. Read the integer 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 AbsoluteValueFinder{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int i = scanner.nextInt();
if(i<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 number
12
Absolute value for the given number is: 12
Enter a number
-12
Absolute value for the given number is: 12
Enter a number
-89
Absolute value for the given number is: 89
No comments:
Post a Comment