In: Computer Science
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
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:

