# Program to find absolute value of a given number

Given an integer N, the task is to find the absolute value of the given integer.

Examples:

Input: N = -6
Output: 6

Input: N = 12
Output: 12

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ program for Method 1 ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the absolute value ` `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 ` `    ``cout << ``" "` `<< N; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by shivanisinghss2110 `

## C

 `// C program for Method 1 ` `#include ` ` `  `// Function to find the absolute value ` `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 ` `    ``printf``(``"%d "``, N); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program for Method 1 ` `class` `GFG{ ` `     `  `// Function to find the absolute value ` `static` `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 Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -``12``; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 program for Method 1 ` ` `  `# Function to find the absolute value ` `def` `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 code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``# Given integer ` `    ``N ``=` `-``12``; ` ` `  `    ``# Function call ` `    ``findAbsolute(N); ` ` `  `# This is code contributed by amal kumar choubey `

## C#

 `// C# program for Method 1 ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` `     `  `// Function to find the absolute value ` `static` `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 Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```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:

1. Set the mask as right shift of integer by 31 (assuming integers are stored using 32 bits).
```mask = n >> 31
```
2. 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
```
```(mask + n)^mask
```

## C++

 `// C++ program for Method 2 ` `#include ` `using` `namespace` `std; ` `#define CHAR_BIT 8 ` ` `  `// Function to find the absolute ` `// value ` `void` `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 Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by Code_Mech `

## C

 `// C program for Method 2 ` `#include ` `#define CHAR_BIT 8 ` ` `  `// Function to find the absolute ` `// value ` `void` `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 Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program for Method 2 ` `class` `GFG{ ` `     `  `static` `final` `int` `CHAR_BIT = ``8``; ` ` `  `// Function to find the absolute value ` `static` `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 Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -``12``; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 program for Method 2 ` `import` `sys ` ` `  `CHAR_BIT ``=` `8``; ` ` `  `# Function to find the absolute value ` `def` `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 Code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# Given integer ` `    ``N ``=` `-``12``; ` ` `  `    ``# Function call ` `    ``findAbsolute(N); ` ` `  `# This code is contributed by 29AjayKumar `

## C#

 `// C# program for Method 2 ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `static` `readonly` `int` `CHAR_BIT = 8; ` ` `  `// Function to find the absolute value ` `static` `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 Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```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++ program for Method 3 ` `# include ` `using` `namespace` `std; ` ` `  `// Function to find the absolute ` `// value ` `void` `findAbsolute(``int` `N) ` `{ ` ` `  `    ``// Find absolute ` `    ``int` `X = ``abs``(N); ` ` `  `    ``// Print the absolute value ` `    ``cout << X; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by Nidhi_biet `

## C

 `// C program for Method 3 ` ` `  `#include ` `#include ` ` `  `// Function to find the absolute ` `// value ` `void` `findAbsolute(``int` `N) ` `{ ` ` `  `    ``// Find absolute ` `    ``int` `X = ``abs``(N); ` ` `  `    ``// Print the absolute value ` `    ``printf``(``"%d "``, X); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program for Method 3 ` `class` `GFG{ ` `     `  `// Function to find the absolute ` `// value ` `static` `void` `findAbsolute(``int` `N) ` `{ ` ` `  `    ``// Find absolute ` `    ``int` `X = Math.abs(N); ` ` `  `    ``// Print the absolute value ` `    ``System.out.printf(``"%d"``, X); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -``12``; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

## Python3

 `# Python3 program for Method 3 ` `import` `math ` ` `  `# Function to find the absolute ` `# value ` `def` `findAbsolute(N): ` ` `  `    ``# Find absolute ` `    ``X ``=` `abs``(N); ` ` `  `    ``# Print the absolute value ` `    ``print``(X); ` ` `  `# Driver Code ` ` `  `# Given integer ` `N ``=` `-``12``; ` ` `  `# Function call ` `findAbsolute(N); ` ` `  `# This code is contributed by Nidhi_biet `

## C#

 `// C# program for Method 3 ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to find the absolute ` `// value ` `static` `void` `findAbsolute(``int` `N) ` `{ ` ` `  `    ``// Find absolute ` `    ``int` `X = Math.Abs(N); ` ` `  `    ``// Print the absolute value ` `    ``Console.Write(``"{0}"``, X); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` `  `    ``// Given integer ` `    ``int` `N = -12; ` ` `  `    ``// Function call ` `    ``findAbsolute(N); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```12
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.