1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Cloud [144]
3 years ago
15

help users calculate their car miles per gallon. write a program to allow a user to enter the number of miles driven and the num

ber of gallons of gas used. the output should be the miles per gallon. use a Do... while (post-test) loop to allow users to enter as many sets of data as desired.
Computers and Technology
1 answer:
Tpy6a [65]3 years ago
8 0

Answer:

The c++ program for the scenario is given below.

#include <iostream>

using namespace std;

int main() {    

   float miles, gallon, speed;

   char reply;    

   cout<<"This program calculates speed of your car in miles per gallon." <<endl;    

   do

   {

       do

       {

           cout<<endl<<"Enter the miles driven by your car" <<endl;

           cin>>miles;

           if(miles<=0)

           {

               cout<<"Invalid input. Enter the miles driven by your car."<<endl;

               cin>>miles;

           }            

       }while(miles<=0);        

       do

       {

           cout<<"Enter the gallons of gas used by your car" <<endl;

           cin>>gallon;

           if(gallon<=0)

           {

               cout<<"Invalid input. Enter the gallons of gas used by your car."<<endl;

               cin>>gallon;

           }

       }while(gallon<=0);    

       speed = miles/gallon;

   

       cout<<"Your car drives at "<<speed<<" miles per gallon."<<endl;        

       cout<<"Do you with to continue (y/n)?"<<endl;

       cin>>reply;        

   }while(reply != 'n');    

   return 0;

}  

This program calculates speed of your car in miles per gallon.

Enter the miles driven by your car

0

Invalid input. Enter the miles driven by your car.

-9  

Enter the miles driven by your car

12.34

Enter the gallons of gas used by your car

0

Invalid input. Enter the gallons of gas used by your car.

-2

Enter the gallons of gas used by your car

3.4

Your car drives at 3.62941 miles per gallon.

Do you with to continue (y/n)?

n

Explanation:

The do while loop is also known as post-test loop. This loop executes once mandatorily. After first execution, the condition is tested and based on the condition, the loop either executes again or discontinues.

In this program, the loop continues till the user wants to try out different sets of data for calculation. Once the user enters input to quit, the loop discontinues.

Do

{  

cout<<"Do you with to continue (y/n)?"<<endl;

        cin>>reply;

}while(reply != 'n');

Moreover, the input by the user is tested for validity. The input cannot be negative or zero. This is implemented by if statement.

if(miles<0)

       {

           cout<<"Invalid input. Miles cannot be negative."<<endl;

           cin>>miles;

       }

The same validity is implemented for gallons of gas also.

You might be interested in
Given that n refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and asso
dybincka [34]

Answer:

The program in Python is as follows:

n = int(input("n:"))

total = 0

for k in range(1,n+1):

   total+=k**3

print(total)

Explanation:

This gets input for n

n = int(input("n:"))

This initializes total to 0

total = 0

This iterates from 1 to n

for k in range(1,n+1):

This adds up the cube of each digit

   total+=k**3

This prints the calculated total

print(total)

7 0
3 years ago
Exercise : Randomizer In this exercise, we are going to create a static class Randomizer that will allow users to get random int
Nutka1998 [239]

Answer:

Here the code is by using java.

Explanation:

//Randomizer.java

public class Randomizer {

public static int nextInt() {

//get random number from 1-10

int randInteger = (int) (Math.random() * (11) + 1);

//if number is greater than 10 or less than 1

while (randInteger > 10 || randInteger < 1) {

randInteger = (int) (Math.random() * (11) + 1);

}

return randInteger;

}

public static int nextInt(int min, int max) {

//formula to get random number from min-max

int randInteger = (int) (Math.random() * (max + 1) + min);

while (randInteger > max || randInteger < min) {

randInteger = (int) (Math.random() * (max + 1) + min);

}

return randInteger;

}

}

//RandomizerTester.java

public class RandomizerTester {

public static void main(String[] args) {

System.out.println("Results of Randommizer.nextInt()");

for (int i = 0; i < 10; i++) {

System.out.println(Randomizer.nextInt());

}

int min = 5;

int max = 10;

System.out.println("\n Results of Randomizer.nextInt(5,10)");

for (int i = 0; i < 10; i++) {

System.out.println(Randomizer.nextInt(min, max));

}

}

}

OUTPUT:

Results of Randommizer.nextInt()

9

2

3

8

5

9

4

1

9

2

Results of Randomizer.nextInt(5,10)

9

8

9

7

5

10

5

10

7

7

4 0
2 years ago
What do you hope that people see in your digital footprint 5 years from now? ​
nikdorinn [45]

Answer:

I hope they see me playing with my dog (who died a few months ago) Malikeye

Explanation:

none :)

5 0
2 years ago
What is the output of the following code?
Lynna [10]

Answer:

The output is 24

Explanation:

Given

The above code segment

Required

The output

We have (on the first line):

x = 6

y = 3

z=24

result=0

On the second line:

result = 2*((z/(x-y))\%y+10)

Substitute the value of each variable

result = 2*((24/(6-3))\%3+10)

Solve the inner brackets

result = 2*((24/3)\%3+10)

result = 2*(8\%3+10)

8%3 implies that, the remainder when 8 is divided by 3.

The remainder is 2

So:

result = 2*(2+10)

result = 2*12

result = 24

<em>Hence, the output is 24</em>

8 0
2 years ago
Which programming languages are the best choices for desktop applications? Select 3 options.
slega [8]
The three are Python c and html
5 0
3 years ago
Read 2 more answers
Other questions:
  • What is a Photojournalist
    5·1 answer
  • How will using ascending order on a field that contains dates sort a list?
    12·2 answers
  • To share a document in my online electronic journal, I should select the option to _____.
    14·1 answer
  • List the steps in setting up an online banking account
    12·2 answers
  • Question 4: What will be the output of the code? Show a complete analysis.
    6·1 answer
  • Which of the following “invisible” marks represents an inserted tab?
    11·2 answers
  • Android OS "AFTER" Alpha.<br><br> A. Bubblegum<br><br> B. Beta<br><br> C. Berry<br><br> D. Buzz Cola
    10·1 answer
  • Which are benefits of group discussions? Check all that apply. Learning becomes more interesting. Students engage in learning. L
    12·2 answers
  • Kai notices his laptop is running sluggishly, so he wants to check his system resources for any issues. Which utility can he use
    11·2 answers
  • Explain how mobile phone production could be more sustainable​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!