Write a Java program to convert a float value to absolute value

Write a Java program to convert a float value to absolute value


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  1. import java.util.*;
  2. public class Javamathexcercise {
  3.  
  4. public static void main(String[] args)
  5. {
  6. Scanner in = new Scanner(System.in);
  7. System.out.print("Input a float number: ");
  8. float x = in.nextFloat();
  9. System.out.printf("The absolute value of %.2f is: %.2f",x, convert(x));
  10. System.out.printf("\n");
  11. }
  12.  
  13. public static float convert(float n)
  14. {
  15. float absvalue = (n >= 0) ? n : -n;
  16. return absvalue;
  17. }
  18. }

Result

java techstudy.org

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.