In: Computer Science
Must be written in C:
Lab 3: Logical Expressions and if Statements
Project Goals
The goals of this project are to:
1. Get students familiar with evaluating logical expressions
2. Get students familiar with
if - else
statements
Important Notes:
1.
Formatting:
Make sure that you follow the precise recommendations for the
output content and formatting: for example, do not change the text of the problem from
“
Enter the number of loaves of bread and price per loaf:
” to
“
Enter
bread and price:
”
. Your assignment will be auto-graded and any change in
formatting will result in a loss in the grade.
2.
Comments:
Header comments are required on all files and recommended for the
rest of the program. Points will be deducted if no header comments are included.
Problem 1
Save your program as
party.c
Perry is throwing a tea party and needs to buy supplies. Write a program that asks the user to
enter information about bread and butter. Each item has a number of units and a price per unit.
The program will ask how many of each item Perry wants and the price per item. Then the
program will ask for Perry’s budget. All values entered will be integers. The program will then
evaluate a set of logical expressions and print out specific messages based on the truth value of
those expressions. The expressions are as follows:
(1)
Perry has money
.
If the condition is true the program will print:
(1) Perry has some money to buy party supplies.
If the condition is false, the program should print:
(1) Perry does not have money to buy anything.
(2)
Perry can get at least one of his supplies.
If Perry can buy at least one of the supplies, the program should print:
(2) Perry can buy at least one of the supplies.
If Perry can’t buy any supplies, the program should print:
(2) Perry is going to have a very sad party.
(3)
Perry can buy all of his supplies
.
If the condition is true, the program should print:
(3) Perry can buy all of the supplies.
If the condition is false, the program should print:
(3) Perry can’t buy all of his supplies.
The program should function as follows (items underlined are to be entered by the user):
Enter the number of sticks of butter and price per stick: 2 20
Enter the number of loaves of bread and price per loaf: 3 30
Enter Perry's budget: 100
(1) Perry has some money to buy party supplies.
(2) Perry can buy at least one of the supplies.
(3) Perry can’t buy all of his supplies.
Run your program again, testing it with different values.
Notes:
●
Your program should print out in the same order as the project description.
●
Your program should only ever print out one statement for every condition
//main program
#include<stdio.h>
int main()
{
printf("Enter the number of sticks of butter and price per stick:
");
int butter,butter_price; //to store butter and it's price;
scanf("%d%d",&butter,&butter_price);
printf("Enter the number of loaves of bread and price per loaf:
");
int loaf,loaf_price; //to store loaf and it's price
scanf("%d%d",&loaf,&loaf_price);
printf("Enter Perry's budget: ");
int budget; //to store budget
scanf("%d",&budget);
if(budget>=butter_price || budget>=loaf_price) //if budget is
greater than any of the price, he can atleast buy single
stuff
printf("(1) Perry has some money to buy party supplies.\n");
else
{
printf("(1) Perry does not have money to buy anything.\n");
return 0;
}
if(budget>=(butter_price*butter) ||
budget>=(loaf_price*loaf)) //if he can buy atleat one stuff
completly
printf("(2) Perry can buy at least one of the supplies.\n");
else
{
printf("(2) Perry is going to have a very sad party.\n");
return 0;
}
if(budget>=(butter_price*butter)+(loaf_price*loaf)) //if budget
is greter than total cost of supplies
printf("(3) Perry can buy all of the supplies.\n");
else
{
printf("(3) Perry can’t buy all of his supplies.\n");
return 0;
}
return 0;
}