by Dinesh Thakur Category: Control Structures

Consider that we have to add the given numbers until the desired sum is obtained (i. e., as long as the sum is less than a specified value).

 

This is required in many situations. For example, to pay a shopkeeper for the goods purchased, we can continue to give one currency note at a time as long as the amount being given is less than the desired amount. Such a sum can be calculated using a while loop as shown below.

printf("Enter payment to be made: ");

        scanf("%d", &payment);

        printf("Enter currency note values:\n");

        sum = 0;

        while (sum < payment)

            {

                   scanf ("%d", &value);

                            sum += value;

            }

            printf("\n Amount payable: %d Amount paid: %d\n", payment, sum);

Initially, the value of the amount to be paid is accepted from the keyboard in variable payment (of type int).Then variable sum is initialized to zero and a while loop is setup to execute its body as long as the value of sum is less than payment. The statements within the body of the loop accept a number (value of next currency note) from the keyboard and add it to sum. The program containing this code segment is executed once and the output is given below.

Enter payment to be made: 125

Enter currency note values:

100

20

10

Amount payable: 125 Amount paid: 130

Observe that we have pressed the Enter key after each value of currency note. As a result, the program accepts only the required data values and displays the sum of entered numbers when sum exceeds payment. If we enter the currency note values separated by spaces we might sometimes get an output that looks wrong, similar to one shown below.

Enter payment to be made: 125

Enter currency note values:

100 20 10 10 10

Amount payable: 125 Amount paid: 130

However, this output is correct. Actually, the last two numbers were not processed by the program as the desired sum was obtained after the third number. This can be easily verified by printing the numbers read from the keyboard within the while loop as shown below (only the while loop is shown to save space):

while (sum < payment) {

scanf ("%d", &value);

printf("%d ", value);

sum += value;

The output of the modified program is given below. Observe that after we press the Enter key, the program first prints the three values processed by it followed by the result.

Enter payment to be made: 125

Enter currency note values:

JOO 20 10 10 10

100 20 10

Amount payable: 125 Amount paid: 130

Note that the code to calculate the sum can be written concisely using the for loop as shown below.

printf("Enter currency note values:\n");

for (sum = 0; sum < payment; sum += value)

scanf("%d", &value);

 

Example: Money payment: add numbers until desired sum is obtained 

#include <stdio.h>
void main()
{
int payment,sum,value;
clrscr();
printf("Enter payment to be made: ");
scanf("%d", &payment);
printf("Enter currency note values:\n");
sum = 0;
while (sum < payment)
{
scanf ("%d", &value);
sum += value;
}

 

printf("\nAmount payable: %d Amount paid: %d\n", payment, sum);
getch();
}

           Money payment: add numbers until desired sum is obtained



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles