Write a function number_of_pennies() that returns the total number of pennies given a number of dollars and (optionally) a number of pennies. Sample output with inputs: 5 6 4 506 400

Respuesta :

Answer:

#include <iostream>  

using namespace std;  

// Declaring Function  

void number_of_pennies(int dollars, int pennies)  

{  

   int convdollarinpennies = 0;  

convdollarinpennies = (dollars*100)+pennies; // Since One Dollar = 100 Pennies, Multiply 100 into number of Dollars  

   cout << "Total Pennies Are " <<convdollarinpennies;  

   

}  

int main()  

{  

   

int dollars=0;  

int pennies=0;  

   cout << "Enter The Amount in Dollars "; //Taking Input For Dollars  

   cin >> dollars;  

   

   cout << "Enter The Amount in Pennies "; //Taking Input For Pennies  

   cin >> pennies;  

number_of_pennies(dollars,pennies); //Calling Function  

   

   return 0;  

}

Answer:

#in python:

def number_of_pennies(dollars = 0,pennies=0):

   number_of_pennies= (dollars * 100) + pennies

   return number_of_pennies

     

print(number_of_pennies(int(input()), int(input()))) # Both dollars and pennies

print(number_of_pennies(int(input())))               # Dollars only

Explanation:

def number_of_pennies(dollars = 0,pennies=0): #def func

   number_of_pennies= (dollars * 100) + pennies #do work

   return number_of_pennies #return func

     

print(number_of_pennies(int(input()), int(input()))) # Both dollars and pennies

print(number_of_pennies(int(input())))               # Dollars only