Absolute Value

PHOTO EMBED

Sat Feb 05 2022 12:16:24 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #absolutevalue #mathematics #gfg #geeksforgeeks

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt(); // number of testcases
        while (T > 0) {
            int I = sc.nextInt();
            Solution obj = new Solution();
            System.out.println(obj.absolute(I));

            T--;
        }
    }
}


class Solution {
    public int absolute(int I) {
       int absolute=Math.abs(I);
       return absolute;
       
       /*
       //used a simple logic 

       if(I<0){
           I=-I;
       }
       else if(I==0){
          I=0;
       }
       else{
           I=I;
       }
       return I;
       */
    }
}
content_copyCOPY

1. Absolute Value You are given an integer I, find the absolute value of the integer I. Example 1: Input: I = -32 Output: 32 Explanation: The absolute value of -32 is 32. Example 2: Input: I = 45 Output: 45 Explanation: The absolute value of 45 is 45 itself. Your Task: You don't need to read input or print anything. Your task is to complete the function absolute() which takes an integer I as an input parameter and return the absolute value of I. Expected Time Complexity: O(1) Expected Auxiliary Space: O(1) Constraints: -10^6 <= I <= 10^6

https://practice.geeksforgeeks.org/problems/absolute-value/1/?track=DSASP-Mathematics&batchId=190