Skip to content
Related Articles

Related Articles

Improve Article

Program to find the percentage of difference between two numbers

  • Difficulty Level : Medium
  • Last Updated : 13 Apr, 2021

Given two numbers a and b, where ‘b’ is incremented or decremented by some percentage of ‘a’. The task is to find out that percentage.

Examples: 

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)

Formula to calculate percentage: 

(Percentage2 – Percentage1) * 100 / (Percentage1) 
  

C++




// C++ program to calculate the percentage
#include <iostream>
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




<?php
// PHP program to calculate
// the percentage
 
// Function to calculate
// the percentage
function percent($a, $b)
{
    $result = 0;
    $result = (($b - $a) * 100) / $a;
 
    return $result;
}
 
// Driver Code
$a = 20;
$b = 25;
 
echo percent($a, $b) . "%";
 
// This code is contributed by mits
?>

Javascript




<script>
 
// Javascript program to calculate the percentage
 
// Function to calculate the percentage
function percent(a, b)
{
    var result = 0;
    result = ((b - a) * 100) / a;
 
    return result;
}
 
// Driver Code.
var a = 20, b = 25;
 
document.write( percent(a, b) + "%");
 
// This code is contributed by itsok
 
</script>
Output: 
25%

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :