Using the C programming tool(s) of choice, write a C program for each of the fol
ID: 3573018 • Letter: U
Question
Using the C programming tool(s) of choice, write a C program for each of the following problems:
1-A metric ton is 35,273.92 ounces. Write a C program that will read the weight of a package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield 1 metric ton of cereal. .
Your program should allow the user to repeat this calculation as often as the user wishes.
2-Many treadmills output the speed of the treadmill in miles per hour (mph) on the console, but most runners think of speed in terms of a pace. A common pace is the number of minutes and seconds per mile instead of mph.
Write a C program that starts with a quantity in mph and converts the quantity into minutes and seconds per mile. As an example, the proper output for an input of 6.5 mph should be 9 minutes and 13.8 seconds per mile. If you need to convert a double to an int, which will discard any value after the decimal point, then you may use:
intValue = (int)dblVal;
3-An approximate value of pi can be calculated using the series given below:
pi = 4 × [ 1 – 1/3 + 1/5 – 1/7 + 1/9 ... + (+|–1)/(2n + 1) ]
Write a C program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation.
Include a loop that allows the user to repeat this calculation for new values n until the user says she or he wants to end the program.
4-A liter is 0.264179 gallons. Write a C program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered.
Your program should allow the user to repeat this calculation as often as the user wishes.
Define a function to compute the number of miles per gallon.
Your program should use a globally defined constant for the number of liters per gallon.
Explanation / Answer
1)
/**
C program that prompts user to enter number of cereal in ounces
to metric tons and number of boxes needed for one metric ton of cereal
*/
//ounces2metric.c
#include<stdio.h>
#include<conio.h>
//main fuction
int main()
{
const double METRIC_TON = 35273.93;
double weight;
printf("Enter the weight of a package breakfast cereal in ounces:");
scanf("%lf",&weight);
double wt = weight/METRIC_TON;
printf("The weight of ceral in tons is %5.2lf tons ",wt );
int box = METRIC_TON/weight;
printf("Need %d boxes ceral to yield one metric ton of cereal",box);
//pause program output on console
getch();
return 0;
}
Output:
Enter the weight of a package breakfast cereal in ounces:1000
The weight of ceral in tons is 0.03 tons
Need 35 boxes ceral to yield one metric ton of cereal
2)
/**
C program that prompts user to enter mph and
converts to minutes and seconds per mile
*/
//mph.c
#include<stdio.h>
#include<conio.h>
//main method
int main()
{
double mph ;
printf("Please enter miles per hour "mph". ");
//read mph from user
scanf("%lf",&mph);
//calculations
double result = 60.0/mph;
int minute = (int)(result);
double second=60.0*(result-minute);
//print minute and second to console
printf("%d minutes and %2.2lf seconds per mile", minute,second);
getch();
return 0;
}
Output:
Please enter miles per hour "mph".
6.5
9 minutes and 13.85 seconds per mile
3)
/**
C program that prompts user to enter n value
and prints the PI value to console
*/
//pivalue.c
#include<stdio.h>
#include<conio.h>
#include<math.h>
//main function
int main()
{
double h = 0;
double n;
double PI;
double power = 0;
printf("Enter number to find PI vlaue : ");
scanf("%lf",&n);
while (n >= power)
{
h += (pow(-1,power)/ (2*power+1));
power++;
}
PI = 4 * h;
printf("PI : %5.5lf",PI);
getch();
return 0;
}
Output:
Enter number to find PI vlaue : 10
PI : 3.23232
4)
/*
C program that prompts user to enter number of liters of gasoline
and distace travelled and prints the miles per gallon to console
*/
//mpg.c
#include<stdio.h>
#include<conio.h>
const double GALLONS_PER_LITRE = 0.264179;
//main method
int main(void)
{
int litres = 0;
double distance = 0.0;
double mpg = 0.0;
printf("How many liters of gasoline is in your vehicle: ");
//read liters
scanf("%d",&litres);
printf("Input the distance in miles you traveled in your vehicle:");
//read distance
scanf("%lf",&distance);
//calculate mpg
mpg = distance / (litres * GALLONS_PER_LITRE);
//print mpg to console
printf("Your vehicle's MPG is: %5.2lf",mpg);
getch();
return 0;
}
Output:
How many liters of gasoline is in your vehicle: 100
Input the distance in miles you traveled in your vehicle:50
Your vehicle's MPG is: 1.89
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.