Advertisement
BladeMechanics

Functions Practise

Oct 5th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<Windows.h>
  5.  
  6.  
  7. double areaRightTriangle(float base, float height);
  8. int isValid(int number);
  9. float converttoCelsius(float Farenheit);
  10. int getSeconds(int hours, int minutes, int seconds);
  11. int isMultiple(int num1, int num2);
  12. void countdownTimer(int seconds);
  13.  
  14. void main()
  15. {
  16.     int op_int1, op_int2, op_int3=1;
  17.     float op_f1, op_f2;
  18.  
  19.     while (op_int3)
  20.     {
  21.         system("cls");
  22.         op_int3 = 1;
  23.         printf("\nChoose a program:\n\n1.\tCalculate Area(Right Triangle)\n2.\tIs my number part of the set 1-100?\n3.\tConvert Farenheit to Celsius\n4.\tConvert hours, minutes, seconds to seconds\n5.\tIs my 2nd number a multiple of the 1st?\n0.\tExit Program\n");
  24.         scanf_s("%d", &op_int3);
  25.         system("cls");
  26.         if (!(op_int3)) break;
  27.         else if (op_int3 == 1)
  28.         {
  29.             printf("\nEnter base of the right triangle: ");
  30.             scanf_s("%f", &op_f1);
  31.             printf("\nEnter altitude/height of the right triangle: ");
  32.             scanf_s("%f", &op_f2);
  33.             printf("The area of the right triangle is %.2lf\nReturning to main menu in \n", areaRightTriangle(op_f1, op_f2));
  34.             countdownTimer(10);
  35.  
  36.         }
  37.         else if (op_int3 == 2)
  38.         {
  39.             printf("\nEnter your number: ");
  40.             scanf_s("%d", &op_int1);
  41.             if (isValid(op_int1))
  42.             {
  43.                 printf("\n\nYour number %d is within 1-100\nReturning to main menu in \n", op_int1);
  44.                 countdownTimer(10);
  45.             }
  46.             else
  47.             {
  48.                 printf("\n\nYour number %d is not within 1-100\nReturning to main menu in \n", op_int1);
  49.                 countdownTimer(10);
  50.             }
  51.         }
  52.         else if (op_int3 == 3)
  53.         {
  54.             printf("\n\nPlease enter the temperature in Farenheit: ");
  55.             scanf_s("%f", &op_f1);
  56.             printf("\n\nThe temperature %.2f in Celsius is %.2f\nReturning to main menu in \n",op_f1,converttoCelsius(op_f1));
  57.             countdownTimer(10);
  58.         }
  59.         else if (op_int3 == 4)
  60.         {
  61.             printf("\nEnter Hours: ");
  62.             scanf_s("%d", &op_int1);
  63.             printf("\nEnter Minutes: ");
  64.             scanf_s("%d", &op_int2);
  65.             printf("\nEnter Seconds: ");
  66.             scanf_s("%d", &op_int3);
  67.             printf("\n\nThe total of %d hours, %d minutes, and %d seconds is %d. \nReturning to main menu in \n", op_int1, op_int2, op_int3, getSeconds(op_int1, op_int2, op_int3));
  68.             countdownTimer(10);
  69.         }
  70.         else if (op_int3 == 5)
  71.         {
  72.             printf("\nEnter first number: ");
  73.             scanf_s("%d", &op_int1);
  74.             printf("\nEnter second number: ");
  75.             scanf_s("%d", &op_int2);
  76.             if (isMultiple(op_int1, op_int2))
  77.             {
  78.                 printf("\n\nThe number %d is a multiple of %d\nReturning to main menu in \n", op_int2, op_int1);
  79.                 countdownTimer(10);
  80.             }
  81.             else
  82.             {
  83.                 printf("\n\nThe number %d is not a multiple of %d\nReturning to main menu in \n", op_int2, op_int1);
  84.                 countdownTimer(10);
  85.             }
  86.         }
  87.         else
  88.         {
  89.             printf("\n\nInvalid entry. Please enter a number from 1-5\nReturning to main menu in \n");
  90.             countdownTimer(10);
  91.         }
  92.     }
  93. }
  94.  
  95. double areaRightTriangle(float base, float height)
  96. {
  97.     return (0.5*base*height);
  98. }
  99.  
  100. int isValid(int number)//1-100==1, else 0;
  101. {
  102.     if ((number <= 100) && (number >= 1)) return 1;
  103.     else return 0;
  104. }
  105.  
  106. float converttoCelsius(float farenheit)
  107. {
  108.     return ((float) 5 / 9 * (farenheit - 32));
  109. }
  110.  
  111. int getSeconds(int hours, int minutes, int seconds)
  112. {
  113.     return(60 * 60 * hours + 60 * minutes + seconds);
  114. }
  115.  
  116. int isMultiple(int num1, int num2)
  117. {
  118.     if (!(num2%num1))
  119.         return 1;
  120.     else return 0;
  121. }
  122.  
  123. void countdownTimer(int seconds)
  124. {
  125.     for (seconds; seconds; seconds--)
  126.     {
  127.         printf("%2d second/s\r", seconds);
  128.         Sleep(1000);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement