Do You Have Enough Money?


TCEA State Programming Contest

Problem 2.1 Do You Have Enough Money?

General Statement:
Read the amount of money you have and the prices of the items you intend to buy. Determine whether you have enough money to buy everything you selected or whether you are short of money. If you do not have enough money, indicate the amount of the shortfall. Be sure to include 8% tax when figuring the amount you need.

Input:
The first line in the data set is an integer that represents the number of data collections that follow. There are an unknown number of money amounts in each data set. The value –1 is used to indicate the end of the collection of prices.

Output:
All letters are to be upper case. Include the amount of shortfall if you do not have enough money. This money amount is to have a dollar sign ($) in front of the amount and it is to be rounded to 2 decimal places. The output is to be formatted exactly like that for the sample output given below.

Assumptions:
The –1 used to indicate the end of a data collection is not part of the data for the problem.

Sample Input:
3
10.50 7.60 1.26 3.49 -1
15.75 6.00 3.98 -1
21.00 5.25 5.75 4.76 3.98 1.50 -1

Sample Output:
$2.84 SHORT
ENOUGH MONEY
$1.94 SHORT

Solution:


#include <stdio.h>

int main ()
{
	int test_case;
	scanf ("%d", &test_case);

	while (test_case--) {
		double total_amount = 0;
		scanf ("%lf", &total_amount);

		double total_expense = 0;

		double value;
		scanf ("%lf", &value);

		while (value != -1) {
			total_expense += value;
			scanf ("%lf", &value);
		}

		total_expense = total_expense + (total_expense * 0.08);

		if (total_amount >= total_expense)
			printf("ENOUGH MONEY\n");

		else
			printf("$%.2lf SHORT\n", total_expense - total_amount);
	}

	return 0;
}

Critical Cases:

Critical Input:
5
0 5 -1
10 -1
10 0 -1
5 5 -1
0 0 -1

Critical Output:
$5.40 SHORT
ENOUGH MONEY
ENOUGH MONEY
$0.40 SHORT
ENOUGH MONEY

2 thoughts on “Do You Have Enough Money?

  1. I have modified the problem a bit too. I brought it close to a real life problem. so a program to help a shopper plan their trip, by telling them whether or not they have enough money.

    #include
    int main(){
    float sum;
    int a,i;
    float b,c,d=0;
    printf(“please enter your sum: $”);
    scanf(“%f”,&sum);
    printf(“\n”);
    printf(“please enter your number of items: “);
    scanf(“%d”,&a);
    printf(“\n”);
    for(i=1;i=d){printf(“YOU HAVE ENOUGH MONEY.\n”);}
    else{
    printf(“YOU ARE $%f SHORT.\n”,d-sum);
    }
    printf(“\n”);
    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s