when i run my code why does it keep add more cents when e.g? 30 cent50 : 2 cent20 : 5 cent10 : 1 cent05 : 1 i only need the 1 cent20 and 1 cent10 especially when i use the }do }while so the user can input as many inputs before he quit.
//
// main.c
// Assignment1
//
// Created by Hassan omer on 15/10/21.
//
#include <stdio.h>
# include <stdlib.h>
int input();
int multiples();
int cions();
void display_change();
int cent50 = 0;
int cent20 = 0;
int cent10 = 0;
int cent05 = 0;
int main() {
char choice = (char)0;
int num;
do {
num = input();
multiples(num);
cions(num);
display_change();
}while(choice != 'q');
return 0;
}
int input(int num)
{
printf("enter 5-95 number\n");
scanf("%d",&num);
return num;
}
int multiples(int num)
{
int sum5 =0;
if (num %5 != 0 ||num<=4|| num >=96) {
printf("invaild input %d\n",num);
exit(0);
}
return sum5;
}
int cions(int sum5){
if (sum5 > 0)
{
while (sum5 >= 50){
sum5 -= 50;
cent50++;
}
while (sum5 >=20){
sum5 -= 20;
cent20++;
}
while (sum5 >= 10){
sum5 -=10;
cent10++;
}
while (sum5 >= 05){
sum5 -= 05;
cent05++;
}
}
return (sum5);
}
void display_change(int sum5)
{
if (cent50)
printf("cent50 : %d\n",cent50);
if(cent20)
printf("cent20 : %d\n",cent20);
if(cent10)
printf("cent10 : %d\n",cent10);
if(cent05)
printf("cent05 : %d\n",cent05);
}
when i run my code why does it keep add more cents when e.g?
30
cent50 : 2
cent20 : 5
cent10 : 1
cent05 : 1
i only need the 1 cent20 and 1 cent10 especially when i use the }do }while so the user can input as many inputs before he quit.
//
// main.c
// Assignment1
//
// Created by Hassan omer on 15/10/21.
//
#include <stdio.h>
//function declaration
int input();
int multiples();
int cions();
void display_change();
//global variables
int cent50 = 0;
int cent20 = 0;
int cent10 = 0;
int cent05 = 0;
//main function of program
int main()
{
char choice = (char)0;
int num;
do {
num = input();
multiples(num);
cions(num);
display_change();
//clear the values obtained for last calculation
cent50 = 0;
cent20 = 0;
cent10 = 0;
cent05 = 0;
}while(choice != 'q');
return 0;
}
//function to read user input
int input(int num)
{
printf("enter 5-95 number\n");
scanf("%d",&num);
return num;
}
//function to validate input
int multiples(int num)
{
int sum5 =0;
if (num %5 != 0 ||num<=4|| num >=96) {
printf("invaild input %d\n",num);
exit(0);
}
return sum5;
}
//coin function to calculates the required coins
int cions(int sum5){
if (sum5 > 0)
{
while (sum5 >= 50){
sum5 -= 50;
cent50++;
}
while (sum5 >=20){
sum5 -= 20;
cent20++;
}
while (sum5 >= 10){
sum5 -=10;
cent10++;
}
while (sum5 >= 05){
sum5 -= 05;
cent05++;
}
}
return (sum5);
}
//function to display change
void display_change(int sum5)
{
if (cent50)
printf("cent50 : %d\n",cent50);
if(cent20)
printf("cent20 : %d\n",cent20);
if(cent10)
printf("cent10 : %d\n",cent10);
if(cent05)
printf("cent05 : %d\n",cent05);
}
Step by step
Solved in 2 steps