Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>// Standard Library for Input Output
- #include <math.h> //Math library
- #define LITER_GAL 3.78544
- //Liters per Gallon
- double computation(double num1, double num2,double num3){//Function and function definition.
- double comp;//computation of the function.
- for(;;){//infinite for loop
- printf("Canadian Price per liter: ");
- scanf("%lg",&num2);
- if(num2<0){
- printf("Re-enter; Must be positive.\n");
- printf("-----------------------\n");}
- else if (num2>0){
- break;}}//breaks from the infinite loop if the num2 is bigger than 0. Continues to the bottom parts.
- comp= num1*num2*num3;//Liter per Gallon* $US per $Can*$Can per liter.
- printf("$Can= $%lg, $USD= $%lg\n",num2,comp);
- printf("\n--------------------------------------------------\n");
- return comp;
- }
- void banner (void){// banner function to print out my name and class.
- printf("--------------------------------------------------");
- printf("\nMalachi Sor\nCSCI 1109 Intro to C Programming Class\n");
- printf("--------------------------------------------------\n");
- printf("--------------------------------------------------");
- }
- int main(void) {
- long double $US_PER_CAN;// defining the variables as long doubles inorder to get more bits for the numbers
- long double $CAN_PER_LITER;
- long double USD_PER_GALLON;
- int number;
- banner();// Function call for banner
- for(;;){//infinite loop.
- printf("\nEnter the exchange rate, USD per CAN.D: ");
- scanf("%Lg",&$US_PER_CAN);
- printf("--------------------------------------------------\n");
- printf("--------------------------------------------------\n");
- 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.
- printf("\n-------------");}
- 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.
- 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.
- USD_PER_GALLON=computation($US_PER_CAN, $CAN_PER_LITER,LITER_GAL); //Function call of the computation.
- for(;;){//infinite loop
- printf("Enter '1' to continue or '0' to stop: ");
- scanf("%i",&number);
- printf("--------------------------------------------------\n");
- if(number==1){USD_PER_GALLON=computation($US_PER_CAN, $CAN_PER_LITER,LITER_GAL);}if (number==0){return 0;}}}
- //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.
- // the return0 will cause the program to stop looping.
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment