Question

In: Computer Science

Miles to Kilometers ASSIGNMENT: Write a program to convert miles to kilometers. Put the entire program...

Miles to Kilometers

ASSIGNMENT:

Write a program to convert miles to kilometers. Put the entire program in a sentinel-controlled loop that runs until the user enters a negative number. Use both a pre-test sentinel-controlled loop and a post-test sentinel-controlled loop in the program.

There are 1.6 kilometers in 1.0 mile. Store the value of 1.6 in a constant and use the constant in the calculations.

There is 1 blank line after the descriptions, and 2 blanks lines between the pre-test and the post-test parts of the program.

Use singular/plural decisions for both miles and kilometers.

There is no validation.

Example Run #1:
(bold type is what is entered by the user)

*** Using a pre-test (while) loop ***
*** This requires the initial prompt and get before the loop,
*** and the loop itself must end with a re-prompt and re-get,
*** but it doesn't require a decision inside the loop.
*************************************************************

Enter the number of miles (enter a negative number to quit): 1.0
1.0 mile is 1.6 kilometers.
Enter the number of miles (enter a negative number to quit): -1

*** Using a post-test (do) loop ***
*** This requires a decision inside the loop to see
*** if the process and output should be done,
*** but the prompt is only written once.
*************************************************************

Enter the number of miles (enter a negative number to quit): 0.625
0.6 miles is 1.0 kilometer.
Enter the number of miles (enter a negative number to quit): 5.5
5.5 miles is x.x kilometers.
Enter the number of miles (enter a negative number to quit): -1

The example run shows EXACTLY how your program input and output will look.

C Programming NO FLOATS

Solutions

Expert Solution

Code:

#include<stdio.h>
#define MILE 1.6
/*Constant*/
int main()
{
   double m=0;
   printf("*** Using a pre-test (while) loop ***\n");
   printf("*** This requires the initial prompt and get before the loop,\n");
   printf("*** and the loop itself must end with a re-prompt and re-get,\n");
   printf("*** but it doesn't require a decision inside the loop.\n");
   printf("*************************************************************\n");
   /*Printing the introduction using while*/
   printf("Enter the number of miles (enter a negative number to quit):");
   scanf("%lf",&m);
   /*Initially reading the input from the user*/
   while(m>=0)
   {
       /*If non negetive we enter into the loop*/
       printf("%.1lf miles is %.1lf kilometers\n",m,m*MILE);
       /*Converting miles to km*/
       printf("Enter the number of miles (enter a negative number to quit):");
       scanf("%lf",&m);
       /*Agaain reading the input*/
      
   }
   printf("*** Using a post-test (do) loop ***\n");
   printf("*** This requires a decision inside the loop to see\n");
   printf("*** if the process and output should be done,\n");
   printf("*** but the prompt is only written once.\n");
   printf("*************************************************************\n");
   /*printing introduction using do while*/
   do
   {
       printf("Enter the number of miles (enter a negative number to quit):");
       scanf("%lf",&m);
       /*Reading the input miles from the user*/
       if(m>=0)
       {
           /*If non negetive we print the output*/
           printf("%.1lf miles is %.1lf kilometers\n",m,m*MILE);
           /*converting miles to km*/  
       }
      
   }
   while(m>=0);
   /*If non negetive then we again repeat the loop*/
  
}

Output:

Indentation:


Related Solutions

Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
Convert the MileConversions program to an interactive application. Instead of assigning a value to the miles...
Convert the MileConversions program to an interactive application. Instead of assigning a value to the miles variable, accept it from the user as input. class MileConversionsInteractive {    public static void main(String[] args) {       // Modify the code below       final double INCHES_IN_MILE = 63360;       final double FEET_IN_MILE = 5280;       final double YARDS_IN_MILE = 1760;       double miles = 4;       double in, ft, yds;       in = miles * INCHES_IN_MILE;       ft = miles * FEET_IN_MILE;       yds = miles * YARDS_IN_MILE;       System.out.println(miles + " miles...
Write a program to calculate the time to run 5 miles, 10 miles, half marathon, and...
Write a program to calculate the time to run 5 miles, 10 miles, half marathon, and full marathon if you can run at a constant speed. The distance of a half marathon is 13.1 miles and that of a full marathon is 26.2 miles. Report the time in the format of hours and minutes. Your program will prompt for your running speed (mph) as an integer. Write a program that displays the Olympic rings. Color the rings in the Olympic...
Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
write a assembly language program to convert GRAY to BCD code in 8051
write a assembly language program to convert GRAY to BCD code in 8051
Write an entire program (with a main function and necessary header files included) that declares an...
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output: $ ./program.exe enter string: nvidia before: nvidia after: aidivn in C
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT