Program to find the rate percentage from compound interest of consecutive years
Last Updated :
31 May, 2022
Given two integers N1 and N2 which is the Compound Interest of two consecutive years. The task is to calculate the rate percentage.
Examples:
Input: N1 = 660, N2 = 720
Output: 9.09091 %
Input: N1 = 100, N2 = 120
Output: 20 %
Approach: The rate percentage can be calculated with the formula ((N2 – N1) * 100) / N1 where N1 is the compound interest of some year and N2 is the compound interest for the next year.
Let us consider the 1st Example:
The difference between the Compound interest in the two consecutive years is because of the interest received on the previous year interest. Therefore,
–> N2 – N1 = N1 * (Rate / 100)
–> 720 – 660 = 660 * (Rate / 100)
–> (60 / 660) * 100 = Rate
–> Rate = (100 / 11) = 9.09% (Approx)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float Rate( int N1, int N2)
{
float rate = (N2 - N1) * 100 / float (N1);
return rate;
}
int main()
{
int N1 = 100, N2 = 120;
cout << Rate(N1, N2) << " %" ;
return 0;
}
|
Java
class GFG
{
static int Rate( int N1, int N2)
{
float rate = (N2 - N1) * 100 / N1;
return ( int )rate;
}
public static void main(String[] args)
{
int N1 = 100 , N2 = 120 ;
System.out.println(Rate(N1, N2) + " %" );
}
}
|
Python 3
def Rate( N1, N2):
rate = (N2 - N1) * 100 / / (N1);
return rate
if __name__ = = "__main__" :
N1 = 100
N2 = 120
print (Rate(N1, N2) , " %" )
|
C#
using System;
class GFG
{
static int Rate( int N1, int N2)
{
float rate = (N2 - N1) * 100 / N1;
return ( int )rate;
}
static public void Main ()
{
int N1 = 100, N2 = 120;
Console.WriteLine(Rate(N1, N2) + " %" );
}
}
|
PHP
<?php
function Rate( $N1 , $N2 )
{
$rate = ( $N2 - $N1 ) * 100 / $N1 ;
return $rate ;
}
$N1 = 100;
$N2 = 120;
echo Rate( $N1 , $N2 ), "%" ;
?>
|
Javascript
<script>
function Rate(N1 , N2) {
var rate = (N2 - N1) * 100 / N1;
return parseInt( rate);
}
var N1 = 100, N2 = 120;
document.write(Rate(N1, N2) + " %" );
</script>
|
Time Complexity: O(1), as there is only basic arithmetic operation.
Auxiliary Space: O(1), as no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...