11.1k views
0 votes
The manager of Fitness Health Club wants a program that allows her to enter the number of calories and grams of fat contained in a specific food. The program should calculate and display two values: the food's fat calories and its fat percentage. The fat calories are the number of calories attributed to fat. The fat percentage is the ratio of the food's fat calories to its total calories. You can calculate a food's fat calories by multiplying its fat grams by the number 9, because each gram of fat contains 9 calories. To calculate the fat percentage, you divide the food's fat calories by its total calories and then multiply the result by 100. The fat percentage should be displayed with zero decimal places. The fat calories are the number of calories are integers. You must use static_cast and setprecision.

User Quinetta
by
4.2k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

Below is the code used to carry out this problem, i hope you try it out and apply it to other similar problems.

Code to Copy:

Public Class myFatCalculator

'calculating fat

Private Sub btnCalculateFat_Click(sender As Object, e As EventArgs) Handles btnCalculateFat.Click

Dim totCalories As Integer

Dim grams As Decimal

Dim fatCalories As Decimal

Dim fatPercentage As Decimal

Integer.TryParse(txtTotCalories.Text,totCalories)

Decimal.TryParse(txtGrams.Text, grams);

fatCalories=grams*9

fatPercentage=fatCalories/totCalories*100;

lblFatCalories.Text=fatCalories.ToString()

lblFatPercentage.Text=fatPercentage.ToString("N0")

End Sub

'Clear all text boxes & labels

Private Sub btnClearForm_Click(sender As Object, e As EventArgs)Handles btnClearFrom_Click

txtTotCalories.Clear()

txtTotCalories.Clear()

lblFatCalories.Clear()

lblFatPercentage.Clear() ‘clear labels

End Sub

'Exit from the Application

Private Sub btnExitApp_Click(sender As Object, e As EventArgs)Handles btnExitApp_Click

Application.Exit()

End Sub

'class ends

End Class

Coding:

Public Class myFatCalculator

'calculating fat

Private Sub btnCalculateFat_Click(sender As Object, e As EventArgs) Handles btnCalculateFat.Click

Dim totCalories As Integer

Dim grams As Decimal

Dim fatCalories As Decimal

Dim fatPercentage As Decimal

Integer.TryParse(txtTotCalories.Text,totCalories)

Decimal.TryParse(txtGrams.Text, grams);

fatCalories=grams*9

fatPercentage=fatCalories/totCalories*100;

lblFatCalories.Text=fatCalories.ToString()

lblFatPercentage.Text=fatPercentage.ToString("N0")

End Sub

'Clear all text boxes & labels

Private Sub btnClearForm_Click(sender As Object, e As EventArgs)Handles btnClearFrom_Click

txtTotCalories.Clear()

txtTotCalories.Clear()

lblFatCalories.Clear()

lblFatPercentage.Clear() ‘clear labels

End Sub

'Exit from the Application

Private Sub btnExitApp_Click(sender As Object, e As EventArgs)Handles btnExitApp_Click

Application.Exit()

End Sub

'class ends

End Class

cheers i hope this helped !!!!

User Bozzmob
by
4.3k points