Program to calculate the profit sharing ratio
Given an array of amounts and time_period that represents the amount of money N persons invest and the time period for which they had invested. The task is to calculate the Profit ratio at the End.
Examples:
Input: n = 2,
Amount1 = 7000, Time1 = 12 months
Amount2 = 6000, Time2 = 6 months
Output: 7 : 3Input: n = 3,
Amount1 = 5000, Time1 = 6 months
Amount2 = 6000, Time2 = 6 months
Amount3 = 1000, Time3 = 12 months
Output: 5 : 6: 2
Formula:
1st person share: (Amount of money invested by 1st) * (Time Period of 1st)
2nd Person share: (Amount of money invested by 2nd) * (Time Period of 2nd)
3rd Person share: (Amount of money invested by 3rd) * (Time Period of 3rd) and so on…
Ratio: 1st person share: 2nd person share : 3rd Person Share
Below is the required implementation:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Calculating GCD of an array. int find_Gcd(int crr[], int n) { int i; int result = crr[0]; for (i = 1; i < n; i++) result = __gcd(crr[i], result); return result; } // Function to calculate the Share void profitRatio(int amountArr[], int timeTrr[], int n) { int i, crr[n]; for (i = 0; i < n; i++) crr[i] = amountArr[i] * timeTrr[i]; int Share = find_Gcd(crr, n); for (i = 0; i < n - 1; i++) cout << crr[i] / Share << " : "; cout << crr[i] / Share; } // Driver Code int main() { int amountArr[] = { 5000, 6000, 1000 }; int timeTrr[] = { 6, 6, 12 }; int n = sizeof(amountArr) / sizeof(amountArr[0]); profitRatio(amountArr, timeTrr, n); return 0; } |
Java
// Java implementation of // above approach import java.io.*; class GFG { // Recursive function to // return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Calculating GCD of an array. static int find_Gcd(int crr[], int n) { int i; int result = crr[0]; for (i = 1; i < n; i++) result = __gcd(crr[i], result); return result; } // Function to calculate the Share static void profitRatio(int amountArr[], int timeTrr[], int n) { int i; int crr[] = new int[n] ; for (i = 0; i < n; i++) crr[i] = amountArr[i] * timeTrr[i]; int Share = find_Gcd(crr, n); for (i = 0; i < n - 1; i++) System.out.print(crr[i] / Share + " : "); System.out.print(crr[i] / Share); } // Driver Code public static void main (String[] args) { int amountArr[] = {5000, 6000, 1000}; int timeTrr[] = {6, 6, 12}; int n = amountArr.length; profitRatio(amountArr, timeTrr, n); } } // This code is contributed // by inder_verma. |
Python3
# Python3 implementation of above approach
# Recursive function to
# return gcd of a and b
def __gcd(a, b):
# Everything divides 0
if(a == 0 or b == 0):
return 0;
# base case
if(a == b):
return a;
# a is greater
if(a > b):
return __gcd(a – b, b);
return __gcd(a, b – a);
# Calculating GCD of an array.
def find_Gcd(crr, n):
result = crr[0];
for i in range(1, n):
result = __gcd(crr[i], result);
return result;
# Function to calculate the Share
def profitRatio(amountArr, timeTrr, n):
i = 0;
crr = [0] * n;
for i in range(n):
crr[i] = amountArr[i] * timeTrr[i];
Share = find_Gcd(crr, n);
for i in range(n – 1):
print(int(crr[i] / Share),
end = ” : “);
print(int(crr[i + 1] / Share));
# Driver Code
amountArr = [5000, 6000, 1000];
timeTrr = [6, 6, 12];
n = len(amountArr);
profitRatio(amountArr, timeTrr, n);
# This code is contributed
# by mits
C#
// C# implementation of // above approach using System; class GFG { // Recursive function to // return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Calculating GCD of an array. static int find_Gcd(int []crr, int n) { int i; int result = crr[0]; for (i = 1; i < n; i++) result = __gcd(crr[i], result); return result; } // Function to calculate the Share static void profitRatio(int []amountArr, int []timeTrr, int n) { int i; int []crr = new int[n] ; for (i = 0; i < n; i++) crr[i] = amountArr[i] * timeTrr[i]; int Share = find_Gcd(crr, n); for (i = 0; i < n - 1; i++) Console.Write(crr[i] / Share + " : "); Console.Write(crr[i] / Share); } // Driver Code public static void Main () { int []amountArr = {5000, 6000, 1000}; int []timeTrr = {6, 6, 12}; int n = amountArr.Length; profitRatio(amountArr, timeTrr, n); } } // This code is contributed // by inder_verma. |
PHP
<?php // PHP implementation of // above approach // Recursive function to // return gcd of a and b function __gcd($a, $b) { // Everything divides 0 if ($a == 0 or $b == 0) return 0; // base case if ($a == $b) return $a; // a is greater if ($a > $b) return __gcd($a - $b, $b); return __gcd($a, $b - $a); } // Calculating GCD of an array. function find_Gcd($crr, $n) { $i; $result = $crr[0]; for ($i = 1; $i < $n; $i++) $result = __gcd($crr[$i], $result); return $result; } // Function to calculate the Share function profitRatio($amountArr, $timeTrr, $n) { $i; $crr = array(); for ($i = 0; $i < $n; $i++) $crr[$i] = $amountArr[$i] * $timeTrr[$i]; $Share = find_Gcd($crr, $n); for ($i = 0; $i < $n - 1; $i++) echo $crr[$i] / $Share , " : "; echo $crr[$i] / $Share; } // Driver Code $amountArr = array(5000, 6000, 1000); $timeTrr = array(6, 6, 12); $n = count($amountArr); profitRatio($amountArr, $timeTrr, $n); // This code is contributed // by inder_verma ?> |
5 : 6 : 2
Recommended Posts:
- Program to calculate Profit Or Loss
- Find the number which when added to the given ratio a : b, the ratio changes to c : d
- Program to find the common ratio of three numbers
- Program to find the count of coins of each type from the given ratio
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Program to find the profit or loss when CP of N items is equal to SP of M items
- C program to calculate the value of nPr
- Program to calculate value of nCr
- Program to calculate age
- Program to calculate the value of sin(x) and cos(x)
- Program to calculate the value of nCr Efficiently
- Program to Calculate e^x by Recursion
- Efficient program to calculate e^x
- Write a program to calculate pow(x,n)
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.



