Kevin321888999

Lab 4 part C

Jul 7th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>// Standard Library for Input Output
  2. #include <math.h> //Math library
  3. #define LITER_GAL 3.78544
  4. //Liters per Gallon
  5.  
  6. double computation(double num1, double num2,double num3){//Function and function definition.
  7. double comp;//computation of the function.
  8. for(;;){//infinite for loop
  9. printf("Canadian Price per liter: ");
  10. scanf("%lg",&num2);
  11. if(num2<0){
  12. printf("Re-enter; Must be positive.\n");
  13. printf("-----------------------\n");}
  14. else if (num2>0){
  15. break;}}//breaks from the infinite loop if the num2 is bigger than 0. Continues to the bottom parts.
  16. comp= num1*num2*num3;//Liter per Gallon* $US per $Can*$Can per liter.
  17. printf("$Can= $%lg, $USD= $%lg\n",num2,comp);
  18. printf("\n--------------------------------------------------\n");
  19. return comp;
  20. }
  21.  
  22. void banner (void){// banner function to print out my name and class.
  23.   printf("--------------------------------------------------");
  24.   printf("\nMalachi Sor\nCSCI 1109 Intro to C Programming Class\n");
  25.   printf("--------------------------------------------------\n");
  26.   printf("--------------------------------------------------");
  27. }
  28.  
  29. int main(void) {
  30. long double $US_PER_CAN;// defining the variables as long doubles inorder to get more bits for the numbers
  31. long double $CAN_PER_LITER;
  32. long double USD_PER_GALLON;
  33. int number;
  34. banner();// Function call for banner
  35. for(;;){//infinite loop.
  36. printf("\nEnter the exchange rate, USD per CAN.D: ");
  37. scanf("%Lg",&$US_PER_CAN);
  38. printf("--------------------------------------------------\n");
  39. printf("--------------------------------------------------\n");
  40. if ($US_PER_CAN<0){printf("Re-enter the US per CAN it must be positive: ");//if the $US_PER_CAN is lower than 0, then the program will print the statement out and return to the beginning of the Infinite Loop.
  41. printf("\n-------------");}
  42. else if ($US_PER_CAN>0){break;}}//If the $US_PER_CAN is bigger than 0, then the program will "break" out of the loop and continue to the while loop.
  43.  
  44. while($US_PER_CAN>=1){// This cannot be equal to 0 because the USD when printed out will always be $0. So we start at 1.
  45. USD_PER_GALLON=computation($US_PER_CAN, $CAN_PER_LITER,LITER_GAL); //Function call of the computation.
  46.  
  47. for(;;){//infinite loop
  48.   printf("Enter '1' to continue or '0' to stop: ");
  49.   scanf("%i",&number);
  50.  
  51.   printf("--------------------------------------------------\n");
  52. if(number==1){USD_PER_GALLON=computation($US_PER_CAN, $CAN_PER_LITER,LITER_GAL);}if (number==0){return 0;}}}
  53. //the if statements are the query loops and will check the user input. If they enter 1 then the program will loop back and continue. If the user enters 0 then the program will stop.
  54.  
  55. // the return0 will cause the program to stop looping.
  56.   return 0;}
Advertisement
Add Comment
Please, Sign In to add comment