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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

//

//  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.

 

Expert Solution
Program

//
//  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);
}

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education