Question 8 16 points Write a program that converts a distance from miles to

Question 8 16 points write a program that converts a

This preview shows page 5 - 6 out of 6 pages.

Question 8 (16 points) Write a program that converts a distance from miles to kilometers or from kilometers to miles. Your program should give two options to the user. If the user types 1, then the program will ask for a distance in miles and converts it to kilometers. If the user types 2, then the program will ask for a distance in kilometers and converts it to miles. If the user types another number, the program will display an error message. 1 mile=1.609 kms. The conversion factor from mile to kilometer needs to be declared as a constant. Use switch statement . Samples of your program runs are shown below: #include <stdio.h> #define KMS_PER_MILE 1.609 int main() { int choice; double kms, miles; printf("Enter your choice: 1 (miles to kms) or 2(kms to miles) >"); scanf("%d",&choice); switch(choice){ case 1: printf("enter distance in miles >"); scanf("%lf",&miles); kms=miles*KMS_PER_MILE; printf("%f miles = %f kilmeters",miles,kms); break; case 2: printf("Enter distance in kilometers >"); scanf("%lf",&kms); miles=kms/KMS_PER_MILE; printf("%f kilometers=%f miles",kms,miles); break; default: printf("Sorry your choice must be 1 or 2"); } return 0; } 5
Image of page 5
Question 9 (18 points) Write a program that asks the user for a code character and a radius. The code character can be: A or a to compute and display the area of the circle C or c to compute and display the circumference of the circle D or d to compute and display the diameter of the circle r diameter r nce circumfere r area 2 2 2 = = = π π Use if-else-if statement #include <stdio.h> #define PI 3.14159 int main() { double radius,diameter,area,circum; char choice; printf("Enter code and radius >"); scanf("%c%lf",&choice,&radius); if(choice == 'A' || choice == 'a') { area=PI*radius*radius; printf("area=%f",area); } else if(choice == 'C' || choice == 'c') { circum=2*PI*radius; printf("circumference=%f",circum); } else if (choice=='D' || choice == 'd') { diameter=2*radius; printf("diameter=%f",diameter); } else printf("Sorry you entered wrong code"); return 0; } 6
Image of page 6

You've reached the end of your free preview.

Want to read all 6 pages?

  • Spring '10
  • baleh
  • printf, Computer Science Department, King Fahd University of Petroleum and Minerals Information

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes