📜  程序查找两个数字之间的差异百分比

📅  最后修改于: 2021-04-29 10:51:59             🧑  作者: Mango

给定两个数字a和b,其中“ b”按“ a”的某个百分比递增或递减。任务是找出那个百分比。

例子:

Input: a = 20, b = 25
Output: 25% 
Difference between 20 and 25 is 5, 
which is 25 % of 20.
(+ve sign indicate increment)

Input: a = 25, b = 20
Output: -20% 
( -ve sign indicate decrement)

计算百分比的公式:

C++
// C++ program to calculate the percentage
#include 
using namespace std;
 
// Function to calculate the percentage
int percent(int a, int b)
{
    float result = 0;
    result = ((b - a) * 100) / a;
 
    return result;
}
 
// Driver Code.
int main()
{
    int a = 20, b = 25;
 
    cout << percent(a, b) << "%";
 
    return 0;
}


Java
// Java program to calculate
// the percentage
class GFG
{
     
// Function to calculate the percentage
static int percent(int a, int b)
{
    float result = 0;
    result = ((b - a) * 100) / a;
 
    return (int)result;
}
 
// Driver Code
public static void main(String[] args)
{
    int a = 20, b = 25;
 
    System.out.println(percent(a, b) + "%");
}
}
 
// This code is contributed by mits


Python3
# Python 3 program to calculate
# the percentage
 
# Function to calculate the percentage
def percent(a, b) :
 
    result = int(((b - a) * 100) / a)
 
    return result
 
# Driver code
if __name__ == "__main__" :
 
    a, b = 20, 25
 
    # Function calling
    print(percent(a, b), "%")
             
# This code is contributed by ANKITRAI1


C#
// C# program to calculate
// the percentage
class GFG
{
     
// Function to calculate the percentage
static int percent(int a, int b)
{
    float result = 0;
    result = ((b - a) * 100) / a;
 
    return (int)result;
}
 
// Driver Code
static void Main()
{
    int a = 20, b = 25;
 
    System.Console.WriteLine(percent(a, b) + "%");
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
25%