Program to find absolute value of a given number - GeeksforGeeks

14 Jun, 2021
Given an integer N, the task is to find the absolute value of the given integer.Examples:  
Input: N = -6 Output: 6Input: N = 12 Output: 12  
 
Method 1 – Naive Approach: Since the absolute value of any number is always positive. For any positive number, the absolute value is the number itself and for any negative number, the absolute value is (-1) multiplied by the negative number.  
C++
C
Java
Python3
C#
Javascript
// C++ program for Method 1#include <bits/stdc++.h>using namespace std; // Function to find the absolute valuevoid findAbsolute(int N){     // If the number is less than    // zero, then multiply by (-1)    if (N < 0)    {        N = (-1) * N;    }     // Print the absolute value    cout << " " << N;} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;} // This code is contributed by shivanisinghss2110
// C program for Method 1#include <stdio.h> // Function to find the absolute valuevoid findAbsolute(int N){     // If the number is less than    // zero, then multiply by (-1)    if (N < 0) {        N = (-1) * N;    }     // Print the absolute value    printf("%d ", N);} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;}
// Java program for Method 1class GFG{     // Function to find the absolute valuestatic void findAbsolute(int N){     // If the number is less than    // zero, then multiply by (-1)    if (N < 0)    {        N = (-1) * N;    }     // Print the absolute value    System.out.printf("%d ", N);} // Driver Codepublic static void main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by 29AjayKumar
# Python3 program for Method 1 # Function to find the absolute valuedef findAbsolute(N):     # If the number is less than    # zero, then multiply by (-1)    if (N < 0):        N = (-1) * N;         # Print the absolute value    print(N); # Driver codeif __name__ == '__main__':     # Given integer    N = -12;     # Function call    findAbsolute(N); # This is code contributed by amal kumar choubey
// C# program for Method 1using System;using System.Collections.Generic; class GFG{     // Function to find the absolute valuestatic void findAbsolute(int N){     // If the number is less than    // zero, then multiply by (-1)    if (N < 0)    {        N = (-1) * N;    }     // Print the absolute value    Console.Write("{0} ", N);} // Driver Codepublic static void Main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by sapnasingh4991
<script>    // Javascript program for Method 1         // Function to find the absolute value    function findAbsolute(N)    {         // If the number is less than        // zero, then multiply by (-1)        if (N < 0)         {            N = (-1) * N;        }         // Print the absolute value        document.write(" " + N);    }          // Given integer    let N = -12;       // Function call    findAbsolute(N);         // This code is contributed by suresh07.</script>
12
 
Method 2 – Using Bitmasking: Since negative numbers are stored in 2s complement form, to get the absolute value, we have to toggle bits of the number and add 1 to the result. Below are the steps:  
Set the mask as right shift of integer by 31 (assuming integers are stored using 32 bits).  
Set the mask as right shift of integer by 31 (assuming integers are stored using 32 bits).  
mask = n >> 31
 For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. Add the mask to the given number.  
 
For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. Add the mask to the given number.  
mask + n
 XOR of mask +n and mask gives the absolute value.  
 
XOR of mask +n and mask gives the absolute value.  
(mask + n)^mask 
 
 
 
C++
C
Java
Python3
C#
Javascript
// C++ program for Method 2#include <bits/stdc++.h>using namespace std;#define CHAR_BIT 8 // Function to find the absolute// valuevoid findAbsolute(int N){     // Find mask    int mask = N >> (sizeof(int) * CHAR_BIT - 1);     // Print the absolute value    // by (mask + N)^mask    cout << ((mask + N) ^ mask);} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;} // This code is contributed by Code_Mech
// C program for Method 2#include <stdio.h>#define CHAR_BIT 8 // Function to find the absolute// valuevoid findAbsolute(int N){     // Find mask    int mask        = N          >> (sizeof(int) * CHAR_BIT - 1);     // Print the absolute value    // by (mask + N)^mask    printf("%d ", (mask + N) ^ mask);} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;}
// Java program for Method 2class GFG{     static final int CHAR_BIT = 8; // Function to find the absolute valuestatic void findAbsolute(int N){     // Find mask    int mask = N >> (Integer.SIZE / 8 *                         CHAR_BIT - 1);     // Print the absolute value    // by (mask + N)^mask    System.out.printf("%d ", (mask + N) ^ mask);} // Driver Codepublic static void main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by 29AjayKumar
# Python3 program for Method 2import sys CHAR_BIT = 8; # Function to find the absolute valuedef findAbsolute(N):         # Find mask    mask = N >> (sys.getsizeof(int()) // 8 *                              CHAR_BIT - 1);     # Print the absolute value    # by (mask + N)^mask    print((mask + N) ^ mask); # Driver Codeif __name__ == '__main__':         # Given integer    N = -12;     # Function call    findAbsolute(N); # This code is contributed by 29AjayKumar
// C# program for Method 2using System; class GFG{     static readonly int CHAR_BIT = 8; // Function to find the absolute valuestatic void findAbsolute(int N){         // Find mask    int mask = N >> (sizeof(int) / 8 *                        CHAR_BIT - 1);     // Print the absolute value    // by (mask + N)^mask    Console.Write((mask + N) ^ mask);} // Driver Codepublic static void Main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by 29AjayKumar
<script>    // Javascript program for Method 2         let CHAR_BIT = 8;      // Function to find the absolute value    function findAbsolute(N)    {         // Find mask        let mask = N >> (4 / 8 * CHAR_BIT - 1);         // Print the absolute value        // by (mask + N)^mask        document.write((mask + N) ^ mask);    }         // Given integer    let N = -12;      // Function call    findAbsolute(N); // This code is contributed by mukesh07.</script>
12
 
Method 3 – Using inbuilt abs() function: The inbuilt function abs() in stdlib.h library finds the absolute value of any number.Below is the implementation of the above approach:  
C++
C
Java
Python3
C#
Javascript
// C++ program for Method 3# include<bits/stdc++.h>using namespace std; // Function to find the absolute// valuevoid findAbsolute(int N){     // Find absolute    int X = abs(N);     // Print the absolute value    cout << X;} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;} // This code is contributed by Nidhi_biet
// C program for Method 3 #include <stdio.h>#include <stdlib.h> // Function to find the absolute// valuevoid findAbsolute(int N){     // Find absolute    int X = abs(N);     // Print the absolute value    printf("%d ", X);} // Driver Codeint main(){     // Given integer    int N = -12;     // Function call    findAbsolute(N);    return 0;}
// Java program for Method 3class GFG{     // Function to find the absolute// valuestatic void findAbsolute(int N){     // Find absolute    int X = Math.abs(N);     // Print the absolute value    System.out.printf("%d", X);} // Driver Codepublic static void main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by sapnasingh4991
# Python3 program for Method 3import math # Function to find the absolute# valuedef findAbsolute(N):     # Find absolute    X = abs(N);     # Print the absolute value    print(X); # Driver Code # Given integerN = -12; # Function callfindAbsolute(N); # This code is contributed by Nidhi_biet
// C# program for Method 3using System; class GFG{     // Function to find the absolute// valuestatic void findAbsolute(int N){     // Find absolute    int X = Math.Abs(N);     // Print the absolute value    Console.Write("{0}", X);} // Driver Codepublic static void Main(String[] args){     // Given integer    int N = -12;     // Function call    findAbsolute(N);}} // This code is contributed by 29AjayKumar
<script>    // Javascript program for Method 3         // Function to find the absolute    // value    function findAbsolute(N)    {         // Find absolute        let X = Math.abs(N);         // Print the absolute value        document.write(X);    }         // Given integer    let N = -12;      // Function call    findAbsolute(N); </script>
12
 
sapnasingh4991
29AjayKumar
Amal Kumar Choubey
shivanisinghss2110
nidhi_biet
Code_Mech
divyeshrabadiya07
suresh07
mukesh07
Numbers
C Programs
C# Programs
C++ Programs
Java Programs
Mathematical
School Programming
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
How to Append a Character to a String in C
Program to print ASCII Value of a character
Convert String to Character Array in C#
Socket Programming in C#
Program to Print a New Line in C#
Getting a Month Name Using Month Number in C#
C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7