Write a parameterized function that calculates and returns the cost per square inch of a rectangular pizza, given its width, height and price as parameters. The formula for area of a rectangle is width * height.
//Solution in C++
#include<iostream>
using namespace std;
double calculateCostperSquare(double width,double height,double price){
double area=width*height;
double costperSq=area/price;
return costperSq;
}
int main(){
double width,height,price;
cout<<"Enter Width and Height in inches:";
cin>>width>>height;
cout<<"Enter Price:";
cin>>price;
double costperSq=calculateCostperSquare(width,height,price);
cout<<"Cost per Square Inch "<<costperSq<<endl;
}
/*
Output
Enter Width and Height in inches:5 6
Enter Price:12
Cost per Square Inch 2.5
--------------------------------
Process exited after 6.477 seconds with return value 0
Press any key to continue . . .
*/
//Solution in C
#include<stdio.h>
double calculateCostperSquare(double width,double height,double price){
double area=width*height;
double costperSq=area/price;
return costperSq;
}
int main(){
double width,height,price;
printf("Enter Width and Height in inches:");
scanf("%lf%lf",&width,&height);
printf("Enter Price:");
scanf("%lf",&price);
double costperSq=calculateCostperSquare(width,height,price);
printf("Cost per Square Inch %lf ",costperSq);
}
/*
Output
Enter Width and Height in inches:5 6
Enter Price:12
Cost per Square Inch 2.500000
--------------------------------
Process exited after 2.734 seconds with return value 0
Press any key to continue . . .
*/
//Solution in Java
import java.util.Scanner;//import Scanner class
public class CalculateRate {
static double calculateCostperSquare(double width,double height,double price){
double area=width*height;
double costperSq=area/price;
return costperSq;
}
public static void main(String[] args) {
double width,height,price;
Scanner input=new Scanner(System.in);//input is object of Scanner class which takes System.in as argument.
System.out.print("Enter Width and Height in inches:");
width=input.nextDouble();
height=input.nextDouble();
System.out.print("Enter Price:");
price=input.nextDouble();
double costperSq=calculateCostperSquare(width,height,price);
System.out.println("Cost per Square Inch "+costperSq);
}
}
/*
Output
Enter Width and Height in inches:5 6
Enter Price:12
Cost per Square Inch 2.500000
--------------------------------
Process exited after 2.734 seconds with return value 0
Press any key to continue . . .
*/
#Solution in Python
def calculateCostperSquare(width,height,price):
area=width*height
costperSq=area/price
return costperSq
print("Enter Width and Height in inches:")
width=(float(input("")))
height=(float(input("")))
print("Enter Price:");
price=(float(input("")))
costperSq=calculateCostperSquare(width,height,price);
print("Cost per Square Inch {} ".format(costperSq))
'''
output
Enter Width and Height in inches:
5
6
Enter Price:
12
Cost per Square Inch 2.5
'''
Get Answers For Free
Most questions answered within 1 hours.