C++ 程序计算金额中的总钞票数

用示例编写一个 C++ 程序来计算金额中的总钞票数。首先,我们声明一个可用钞票的数组,然后将用户输入的金额赋给临时变量。接下来,我们使用 for 循环遍历该钞票数组,找出给定现金中的总钞票数。

#include<iostream>
using namespace std;

int main()
{
	int a[9] = {1000, 500, 100, 50, 20, 10, 5, 2, 1};
	int amount, i, temp;
	
	cout << "\nPlease Enter the Amount of Cash =  ";
	cin >> amount;
	
	temp = amount;
	
	for(i = 0; i < 9; i++)
  	{
		cout << a[i] <<" Notes is = " << temp / a[i] << endl;
		temp = temp % a[i];
  	}
		
 	return 0;
}
C++ Program to Count Total Notes in an Amount

使用函数计算金额中总钞票数的 C++ 程序

#include<iostream>
using namespace std;

void totalNotes(int amount)
{
	int a[9] = {1000, 500, 100, 50, 20, 10, 5, 2, 1};
	int i, temp;
	
	temp = amount;
	
	for(i = 0; i < 9; i++)
  	{
		cout << a[i] <<" Notes is =  " << temp / a[i] << endl;
		temp = temp % a[i];
  	}
}
int main()
{
	int amount;
	
	cout << "\nPlease Enter the Amount of Cash =  ";
	cin >> amount;
	
	totalNotes(amount);
		
 	return 0;
}
Please Enter the Amount of Cash =  876592
1000 Notes is =  876
500 Notes is =  1
100 Notes is =  0
50 Notes is =  1
20 Notes is =  2
10 Notes is =  0
5 Notes is =  0
2 Notes is =  1
1 Notes is =  0

使用多个 If 语句计算给定金额中总钞票数的 C++ 程序

#include<iostream>
using namespace std;

int main()
{
    int amount;
    
	int note500, note100, note50, note20, note10, note5, note2, coin1;
	note500 = note100 = note50 = note20 = note10 = note5 = note2 = coin1 = 0; 	
	
	cout << "\nPlease Enter the Amount of Cash =  ";
	
	cin >> amount;
  	if (amount > 500)
	{
  		note500 = amount / 500;
  		amount = amount - (note500 * 500);	
  	} 
	if (amount >= 100)  	
	{
  		note100 = amount / 100;
  		amount = amount - (note100 * 100);	
  	}
	if (amount >= 50)  	
	{
  		note50 = amount / 50;
  		amount = amount - (note50 * 50);	
  	} 
	if (amount >= 20)  	
	{
  		note20 = amount / 20;
  		amount = amount - (note20 * 20); 	
  	} 
	if (amount >= 10)  	
	{
  		note10 = amount / 10;
  		amount = amount - (note10 * 10); 	
  	} 
	if (amount >= 5)  	
	{
  		note5 = amount / 5;
  		amount = amount - (note5 * 5); 	
  	} 
	if (amount >= 2)  	
	{
  		note2 = amount / 2;
  		amount = amount - (note2 * 2); 	
  	} 
	if (amount >= 1)  	
	{
	   	coin1 = amount;
	}
	cout << "\nTotal Number of Notes present in the Cash that you entered are \n";
	cout << "\n500 Notes  =  " << note500; 
	cout << "\n100 Notes  =  " << note100; 
	cout << "\n50 Notes   =  " << note50; 
	cout << "\n20 Notes   =  " << note20; 
	cout << "\n10 Notes   =  " << note10; 
	cout << "\n5 Notes    =  " << note5; 
	cout << "\n2 Notes    =  " << note2; 
	cout << "\n1 Coin     =  " << coin1; 
			
 	return 0;
}
Please Enter the Amount of Cash =  2156781

Total Number of Notes present in the Cash that you entered are 

500 Notes  =  4313
100 Notes  =  2
50 Notes   =  1
20 Notes   =  1
10 Notes   =  1
5 Notes    =  0
2 Notes    =  0
1 Coin     =  1