In: Computer Science
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s.
Test your solution using this samples]
a. Input: 250
Output: 1x200s, 1x50s
b. Input: 1127
Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s
c. Input: 1127
Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s
d. Input: 19
Output: 1x10s, 1x5s, 4x1s
[Hints]
o Use division to determine the number of occurrence of each element (i.e. 200, 100) in the input (e.g. Given 500 if we divide it by the largest number possible; which is 200; we will get 2. Therefore, there are 2x200s.)
o Use subtraction to determine the remaining value of the input. (e.g. In the 500 example, since there are 2x200s, we still have 100 to process. The 100 came from 500 – (2*200) = 100.)
o Use the next largest number possible (i.e. 100) to check the number of occurrence. Continue until the remaining value of the input is zero.
#include <iostream>
using namespace std;
int main() {
int number,n1,n2,n3,n4,n5,n6,n7;
cin>>number;
while(number != 0)
{
n1 = number / 200;
if(n1 != 0)
{
cout<<n1<<"*200s,";
number = number % 200;
}
n2 = number / 100;
if(n2 != 0)
{
cout<<n2<<"*100s,";
number = number % 100;
}
n3 = number / 50;
if(n3 != 0)
{
cout<<n3<<"*50s,";
number = number % 50;
}
n4 = number / 20;
if(n4 != 0)
{
cout<<n4<<"*20s,";
number = number % 20;
}
n5 = number / 10;
if(n5 != 0)
{
cout<<n5<<"*10s,";
number = number % 10;
}
n6 = number / 5;
if(n6 != 0)
{
cout<<n6<<"*5s,";
number = number % 5;
}
n7 = number / 1;
if(n7 != 0)
{
cout<<n7<<"*1s";
number = number % 1;
}
}
return 0;
}
Output:
1127 5*200s,1*100s,1*20s,1*5s,2*1s
Do ask if any doubt. Please upvote.