Home > Backend Development > C++ > body text

C program to find change

PHPz
Release: 2023-08-29 08:37:06
forward
876 people have browsed it

C program to find change

In this problem, we are given a value n, we want to give change n rupees, and we have n coins, each coin has a face value ranging from 1 to m . We need to return the total number of ways that this sum can be formed. The Chinese translation of

Example

Input : N = 6 ; coins = {1,2,4}.
Output : 6
Explanation : The total combination that make the sum of 6
is :
{1,1,1,1,1,1} ; {1,1,1,1,2}; {1,1,2,2}; {1,1,4}; {2,2,2} ; {2,4}.
Copy after login

Example

is:

Example

#include <stdio.h>
int coins( int S[], int m, int n ) {
   int i, j, x, y;
   int table[n+1][m];
   for (i=0; i<m; i++)
      table[0][i] = 1;
   for (i = 1; i < n+1; i++) {
      for (j = 0; j < m; j++) {
         x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
         y = (j >= 1)? table[i][j-1]: 0;
         table[i][j] = x + y;
      }
   }
   return table[n][m-1];
}
int main() {
   int arr[] = {1, 2, 3};
   int m = sizeof(arr)/sizeof(arr[0]);
   int n = 4;
   printf("The total number of combinations of coins that sum up to %d",n);
   printf(" is %d ", coins(arr, m, n));
   return 0;
}
Copy after login

Output

The total number of combinations of coins that sum up to 4 is 4
Copy after login

The above is the detailed content of C program to find change. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!