I am supposed to make a pseudocode for a fat gram calculator and I want to know if my code is correct. This is the instructions:
Design a program that asks for the number of fat grams and calories in a food item. Validate the input as follows:
- Make sure the number of fat grams and calories are not less than 0
- According to nutritional formulas, the number of calories cannot exceed fat grams x 9. Make sure that the number of calories entered is not greater than fat grams x 9.
Once the correct data has been entered the program should calculate and display the percentage of calories that come from fat. Use the following formula:
% of calories from fat = (fat grams x 9) / calories
Some nutritionists classify a food as "low fat" if less than 30& of its calories come from fat. If the results of this formula are less than 0.3, the program should display a message indicating the food is low in fat.
Here is my code:
Declare Real fatGrams, percentCal, calories
Module main()
Display “Enter the number of fat grams.”
Input fatGrams
Display “Enter the number of calories.”
Input calories
Call checkInput()
Set percentCal = calc(fatGrams, calories)
If percentCal < 0.3 then Display “The percentage of calories from fat is”, percentCal, “. This food is low in fat.”
Else
Display “The percentage of calories from fat is”, percentCal
End if
End Module
Module checkInput()
While fatGrams and calories <= 0
Display “You must enter a value greater than 0 for the number of fat grams and calories.”
End while
While calories > fatGrams * 9
Display “The number of calories cannot exceed fat grams times 9.”
End while
End Module
Function calc(Real num1, Real num2)
Declare Real result
Set percentCal = (num1 * 9) / num2
Return result
End Function