Computer Science, asked by ziadalsaydia, 2 months ago

Write a java program that deal with a number entered by the user and print the absolute value of the entered number?
Example:
Entered value = 1 output value = 1
Entered value = -1 output value = 1

Answers

Answered by ayush07092004
1

// to display absolute value of number

public class Number

{

public static void main ( String args [] )

{

int n1 , n2 ;

double p = 0.0 , q = 0.0;

p = Math . abs ( n1 ) ;

q = Math . abs ( n2 ) ;

System.out.println ( " number 1 = " + p ) ;

System.out.println ( " number 2 = " + q ) ;

}

}

Answered by anindyaadhikari13
1

Answer:

The given program is written in Java.

import java.util.*;

public class Main {

 public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    System.out.print("Enter an integer: ");

    int a=sc.nextInt();

    System.out.println("Absolute value: "+Math.abs(a));

    sc.close();

 }

}

Explanation:

  • The Math.abs() function returns the absolute value of a given number. Using this function, the problem is solved.

See the attachment for output.

•••♪

Attachments:
Similar questions