Design a program that asks the user 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 * 9. Make sure that the number of calories entered is not greater than fat grams * 9.
Once correct data has been entered, the program should calculate and display the percentage of calories that come from fat. Use the following formula:
Percentage of calories from fat=( Fat grams * 9) / Calories
Some nutritionists classify food as “ low fat” if less than 30percent 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.

how to write this in pseudo code for both Module and function?

------------------------------

method:
-------

Module main ( )

/ / Local variables
double fatGramCalculator(double fatGrams, double calories)

// If fat grams or calories are less than 0, display a message and return -1
if (fatGrams < 0 || calories < 0)

Display ('Number of fat grams and calories must be greater than 0

return -1;

// If calories is greater than fat grams * 90, display a message and return -1
if (calories > (fatGrams * 9)

Display('Calories must not exceed fat grams x 9

return -1;

// Calculate percentage of calories from fat
double caloriesFromFat = (fatGrams * 9) / calories;

// Display calories from fat, multiplying percentage by 100 to remove decimal point
Display('%d percent of calories from fat,
caloriesFromFat * 100);

// If less than 30% of calories come from fat, display a message
if (caloriesFromFat < 0.3)

Display('Food is low in fat

// Return the number of calories from fat
return caloriesFromFat;

End Module

Function Real sum ( Real fatGrams, Real doubleCalories )
Declare Real result
Set result = ( fatGrams*9 ) / calories
Return result
End Function

--------
is this right?

Your pseudo code for both the module and function is almost correct. However, there are a few syntax errors and missing brackets in the code. Here is the corrected pseudo code:

Module main ( )
// Local variables
Declare Real fatGrams, calories

// Prompt the user for fat grams and calories
Display("Enter the number of fat grams:")
Input fatGrams
Display("Enter the number of calories:")
Input calories

// If fat grams or calories are less than 0, display a message and return -1
If fatGrams < 0 OR calories < 0 Then
Display("Number of fat grams and calories must be greater than 0")
Return -1
End If

// If calories is greater than fat grams * 9, display a message and return -1
If calories > (fatGrams * 9) Then
Display("Calories must not exceed fat grams x 9")
Return -1
End If

// Calculate percentage of calories from fat
Declare Real caloriesFromFat
Set caloriesFromFat = (fatGrams * 9) / calories

// Display percentage of calories from fat, multiplying percentage by 100 to remove decimal point
Display("Percentage of calories from fat: " + (caloriesFromFat * 100) + "%")

// If less than 30% of calories come from fat, display a message
If caloriesFromFat < 0.3 Then
Display("Food is low in fat")
End If

// Return the percentage of calories from fat
Return caloriesFromFat
End Module

Function Real fatGramCalculator (Real fatGrams, Real calories)
// Calculate percentage of calories from fat
Declare Real caloriesFromFat
Set caloriesFromFat = (fatGrams * 9) / calories

Return caloriesFromFat
End Function

In the module, I have added the prompts for inputs, corrected the syntax errors, and added missing brackets. In the function, the syntax for the function declaration and return statement is fixed.

Yes, your pseudo code for both the module and function is correct. Here is the revised version for better clarity:

Module main():

Declare double fatGramCalculator(double fatGrams, double calories)

// If fat grams or calories are less than 0, display a message and return -1
if (fatGrams < 0 or calories < 0):
Display "Number of fat grams and calories must be greater than 0"
return -1

// If calories is greater than fat grams * 9, display a message and return -1
if (calories > fatGrams * 9):
Display "Calories must not exceed fat grams * 9"
return -1

// Calculate percentage of calories from fat
double caloriesFromFat = (fatGrams * 9) / calories

// Display percentage of calories from fat
Display caloriesFromFat * 100, "percent of calories from fat"

// If less than 30% of calories come from fat, display a message
if (caloriesFromFat < 0.3):
Display "Food is low in fat"

// Return the percentage of calories from fat
return caloriesFromFat

Function Real fatGramCalculator(Real fatGrams, Real calories):
Declare Real percentageOfCaloriesFromFat
Set percentageOfCaloriesFromFat = (fatGrams * 9) / calories
Return percentageOfCaloriesFromFat
End Function

Yes, the pseudo code you provided for both the module and the function is generally correct. However, there are a few syntax errors and missing semicolons that need to be fixed. Here is the corrected version:

Method:
-------

Module main ()
// Local variables
Function fatGramCalculator(fatGrams, calories)
// If fat grams or calories are less than 0, display a message and return -1
If (fatGrams < 0 OR calories < 0) Then
Display "Number of fat grams and calories must be greater than 0"
Return -1
End If

// If calories is greater than fat grams * 90, display a message and return -1
If (calories > (fatGrams * 9)) Then
Display "Calories must not exceed fat grams x 9"
Return -1
End If

// Calculate percentage of calories from fat
caloriesFromFat = (fatGrams * 9) / calories

// Display calories from fat, multiplying percentage by 100 to remove decimal point
Display caloriesFromFat * 100 & " percent of calories from fat"

// If less than 30% of calories come from fat, display a message
If (caloriesFromFat < 0.3) Then
Display "Food is low in fat"
End If

// Return the number of calories from fat
Return caloriesFromFat
End Function

// Call the fatGramCalculator function
fatGramCalculator(fatGrams, calories)
End Module

Function Real fatGramCalculator(Real fatGrams, Real calories)
// Calculate percentage of calories from fat
caloriesFromFat = (fatGrams * 9) / calories

// Display calories from fat, multiplying percentage by 100 to remove decimal point
Display caloriesFromFat * 100 & " percent of calories from fat"

// If less than 30% of calories come from fat, display a message
If (caloriesFromFat < 0.3) Then
Display "Food is low in fat"
End If

// Return the number of calories from fat
Return caloriesFromFat
End Function