Я сгенерировал код, который подсчитывает минимальное количество 20-х, 10-х, 5-х, 2-х и 1-х, которое будет складываться до определенной пользователем суммы денег.Пользователь может вводить только целые числа, т.е. без десятичных значений.У меня два вопроса.
- Если деноминация не нужна, программа выводит случайное число вместо 0. Как это исправить?
- Можно ли создать функциючто может заменить все операторы if и, возможно, операторы
printf
?Я новичок в функциях, поэтому немного потерян с ними.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int pounds;
int one, two, five, ten, twenty;
printf("Enter a pounds amount with no decimals, max E999999: \n");
scanf("%d", £s);
printf("%d\n", pounds);
if(pounds >= 20)
{
twenty = (pounds / 20);
pounds = (pounds-(twenty * 20));
printf("%d\n", pounds);
}
if(pounds >= 10)
{
ten = (pounds / 10);
pounds = (pounds-(ten * 10));
printf("%d\n", pounds);
}
if(pounds >= 5)
{
five = (pounds / 5);
pounds = (pounds-(five * 5));
printf("%d\n", pounds);
}
if(pounds >= 2)
{
two = (pounds / 2);
pounds = (pounds-(two * 2));
printf("%d\n", pounds);
}
if(pounds >= 1)
{
one = (pounds / 1);
pounds = (pounds-(one * 1));
printf("%d\n", pounds);
}
printf("The smallest amount of denominations you need are: \n");
printf("20 x %d\n", twenty);
printf("10 x %d\n", ten);
printf("5 x %d\n", five);
printf("2 x %d\n", two);
printf("1 x %d\n", one);
return 0;
}