📜  程序查找给定数字的绝对值

📅  最后修改于: 2021-05-28 03:30:29             🧑  作者: Mango

给定一个整数N ,任务是找到给定整数的绝对值。
例子:

方法1 –天真的方法:由于任何数字的绝对值始终为正。对于任何正数,绝对值是数字本身,对于任何负数,绝对值是(-1)乘以负数。

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


Javascript


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


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


Javascript


输出:
12

方法2 –使用位掩码:由于负数以2s补码形式存储,为了获取绝对值,我们必须切换数字的位并将结果加1。步骤如下:

  1. 将掩码设置为整数右移31(假设整数使用32位存储)。
mask = n >> 31
  1. 对于负数,上述步骤将掩码设置为1 1 1 1 1 1 1 1 1,对于正数,将掩码设置为0 0 0 0 0 0 0 0。将掩码添加到给定的数字。
mask + n
  1. 掩码+ n与掩码的XOR给出绝对值。
(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
输出:
12

方法3 -使用内置ABS()函数:内置函数abs()stdlib.h中文库发现的任何数目的绝对值。
下面是上述方法的实现:

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

Java脚本


输出:
12