2.15. Pul birliklarini hisoblash Key Point. Bu qismda pul birligining katta hajmini kichik birliklarda hisoblash masalasini dasturlash qaraladi. Faraz qilaylik biz katta hajmda berilgan pul birligini kichik hajmda gi birlikda hisoblashni amalga oshirmoqchimiz. Foydalanuvchi dasturga double turidagi, katta hajmli pul birligi qiymatini birlikning butun qiymati va tangalarda kiritadi. Dasturning bajarilishi natijasida chiqarilgan ma’lumot uning tangalardagi minimum hiajmdagi ekvivalent qiymatni chiqaradi.
Bu yerda dasturni ishlab chiqish bosqichlari keltirilgan:
1. Foydalanuvchini 11.56 kabi o`nli son ko`rinishidagi qiymat kiritishga undash;
2. Pul birligi qiymati (11.56)ni tangalarga o`tkazish (1156);
3. So`mlar sonini aniqlash uchun tangalar 100 ga bo`linadi va qoldiqlarni tanga sifatida olinadi;
4. Chorak qiymatni olish uchun qoldiq tangalarni 25 ga bo`lib, chiqqan qoldiq olinadi;
5. Qoldiq tangalar 10 ga bo`linib, uning qoldig`idan kichik tanga qiymati olinadi;
6. Qoldiq tangalar 5 ga bo`linib, uning qoldig`idan eng kichik tanga qiymati olinadi;
7. Qoldiq tangalar – kichik qoldiq tangalardir;
8. Natijani chiqarish.
2.12-ro`yxat. 1 #include 2 using namespacestd;
3
4 intmain()
5 {
6 // Receive the amount
7 cout << "Enter an amount in double, for example 11.56: ";
8 doubleamount;
9 cin >> amount;
10
11 intremainingAmount = static_cast(amount * 100);
incremental code and test
✓Point ✓Check
Key
Point
minimum number of coins
60 Chapter 2 Elementary Programming
12
13 // Find the number of one dollars
14 intnumberOfOneDollars = remainingAmount / 100;
15 remainingAmount = remainingAmount % 100;
16
17 // Find the number of quarters in the remaining amount
18 intnumberOfQuarters = remainingAmount / 25;
19 remainingAmount = remainingAmount % 25;
20
21 // Find the number of dimes in the remaining amount
22 intnumberOfDimes = remainingAmount / 10;
23 remainingAmount = remainingAmount % 10;
24
25 // Find the number of nickels in the remaining amount
26 intnumberOfNickels = remainingAmount / 5;
27 remainingAmount = remainingAmount % 5;
28
29 // Find the number of pennies in the remaining amount
30 intnumberOfPennies = remainingAmount;
31
32 // Display results
33 cout << "Your amount "<< amount << " consists of "<< endl <<
34 " "<< numberOfOneDollars << " dollars"<< endl <<
35 " "<< numberOfQuarters << " quarters"<< endl <<
36 " "<< numberOfDimes << " dimes"<< endl <<
37 " "<< numberOfNickels << " nickels"<< endl <<
38 " "<< numberOfPennies << " pennies"<< endl;
39
40 return 0;
41 }
Enter an amount in double, for example 11.56: 11.56
Your amount 11.56 consists of
11 dollars
2 quarters
0 dimes
1 nickels
1 pennies