Dasturlash I 3-hafta mavzu: С++da sonlarning turlari va ular ustida amallar. Dastur yaratish jarayoni
Download 6.28 Mb.
|
ma\'ruza-3
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: Foydalanuvchini 11.56 kabi o`nli son ko`rinishidagi qiymat kiritishga undash; Pul birligi qiymati (11.56)ni tangalarga o`tkazish (1156); So`mlar sonini aniqlash uchun tangalar 100 ga bo`linadi va qoldiqlarni tanga sifatida olinadi; Chorak qiymatni olish uchun qoldiq tangalarni 25 ga bo`lib, chiqqan qoldiq olinadi; Qoldiq tangalar 10 ga bo`linib, uning qoldig`idan kichik tanga qiymati olinadi; Qoldiq tangalar 5 ga bo`linib, uning qoldig`idan eng kichik tanga qiymati olinadi; Qoldiq tangalar – kichik qoldiq tangalardir; 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 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 }
Download 6.28 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling