Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a C# program that allows the user to enter the number of calories and fat grams in a food. The application should display the
Create a C# program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than of the total calories of the food, it should also display a message indicating the food is low in fat.
One gram of fat has calories, so: Calories from fat fat grams
The percentage of calories from fat can be calculated as:
Percentage of calories from fat Calories from fat total calories
Input validation: Make sure the number of calories are not less than Also, the number of calories from fat cannot be greater than the total number of calories. If that happens, display an error message indicating that either the calories or fat grams were incorrectly entered.
Use the following test data to determine if the application is calculating properly:
Calories and Fat Percentage Fat
calories, fat grams Percentage of calories from fat: calories fat grams Percentage of calories from fat: a lowfat food
lories, fat grams Percentage of calories from fat:
I know my math is off the test didn't show it gave me an insane number instead I also need a checkbox tp put in whether the food item is a high or low food item
public partial class Form : Form
public Form
InitializeComponent;
End initialization
private void calcButtonClickobject sender EventArgs e
try
Calories from fat fat grams
number range to
Percentage of calories from fat calories from fat total calories
Number range to
Get variables
int calories;
int fatGrams;
Declare user input
calories int.ParsecaloriesBoxText;
fatGrams int.ParsefatGramsBoxText;
Check calories range and fat grams range
if
calories && calories && fatGrams && fatGrams
Calculate the calories from fat
int caloriesFromFat fatGrams ; gram of fat calories
Validate that calories from fat is not greater than total calories
if caloriesFromFat calories
Calculate the percentage of calories from fat
double percentageOfCaloriesFromFat doublecaloriesFromFat calories ;
Display the result
messageLabel.Text caloriesFromFat.ToString percentageOfCaloriesFromFat.ToStringn;
else
MessageBox.ShowInvalid values. Calories from fat cannot be greater than total calories.";
else
MessageBox.ShowInvalid values. Calories should be between and Fat grams should be between and ;
catch Exception ex
Display error message
MessageBox.ShowexMessage;
private void clearButtonClickobject sender EventArgs e
clear textboxes and reset focus
caloriesBox.Text ;
fatGramsBox.Text ;
caloriesBox.Focus;
private void exitMenuClickobject sender EventArgs e
Close application
this.Close;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started