144k views
3 votes
Write a function numberofpennies() that returns the total number of pennies given a number of dollars and (optionally) a number of pennies. ex: 5 dollars and 6 pennies returns 506.

User Dzinx
by
7.5k points

2 Answers

0 votes

Final answer:

The numberofpennies() function calculates the total number of pennies by converting dollars to pennies and adding any additional pennies. It multiplies the number of dollars by 100 and adds the extra pennies to find the total.

Step-by-step explanation:

Function to Calculate Total Number of Pennies

The function numberofpennies() is used to calculate the total number of pennies for a given amount of dollars and optionally, additional pennies. To do this, it converts the dollars into pennies and then adds the additional pennies if they are provided.

Example Function in Python

Here is an example of how one might write the numberofpennies() function in Python:

def numberofpennies(dollars, extra_pennies=0):
return (dollars * 100) + extra_pennies

# Example usage:
total_pennies = numberofpennies(5, 6)
print(total_pennies) # This would print 506

In this example, the function takes two arguments: dollars, which is a mandatory argument representing the number of dollars, and extra_pennies, which is an optional argument representing additional pennies. The function then returns the total number of pennies by multiplying the number of dollars by 100 and then adding the extra pennies if any.

User Ravenous
by
7.5k points
6 votes
What language are you writing this in? C? Javascript? Java? Are you allow to have parameters?
The general idea should be the same. Since 1 dollar = 100 pennies, we should write something like...

Java:
public static double numberofpennies(double dollars, double penny) {
double sum = 0;

// The amount of pennies that dollar represents
double converted = dollars * 100.0;

sum = converted + penny;

return sum;
}
Note: You should probably place this question under the category computer and technology instead of math. Also, this is just an example of what you could possibly write. What parameters you are allowed to use, what type (double? int? etc?) of pennies are you allowed to return, etc. depends on how you write it.



User Gaurav K
by
7.1k points