Rejack

Excercise 1 and 2

Jan 14th, 2024
1,243
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 1 0
  1. #include < stdio.h >
  2. /*
  3. *
  4. * Excercise #1
  5. void main()
  6. {
  7.     printf("1. Hello Yan\n");
  8.  
  9.     double PI = 3.141592;
  10.  
  11.     printf("2. %.2lf\n", PI);
  12.  
  13.     printf("3.\n  *\n ***\n*****\n");
  14.  
  15.     printf("4.\nEnter 2 integers:\n");
  16.  
  17.     int a, b, c;
  18.  
  19.     scanf_s("%d%d", &a, &b);
  20.  
  21.     printf("Sum of (%d+%d): %d\n",a,b, (a + b));
  22.     printf("Division of (%d/%d): %.2lf\n", a,b, (double) a / b);
  23.     printf("Modulo of (%d mod %d): %d\n", a, b, a % b);
  24.  
  25.     printf("5.\nA = %d || B = %d\n", a, b);
  26.  
  27.     printf("#1: a = %d || b = %d\n", b, a);
  28.  
  29.     c = a;
  30.     a = b;
  31.     b = c;
  32.  
  33.     printf("#2: a = %d || b = %d\n", a, b );
  34. }
  35. */
  36.  
  37. void main()
  38. {
  39.     /*
  40.     int a;
  41.  
  42.     printf("Enter a 3 digit number i.e 184\n");
  43.  
  44.     scanf_s("%d", &a);
  45.  
  46.     while (a <= 99 || a > 999)
  47.     {
  48.         printf("Enter a 3 digit number i.e 184\n");
  49.         scanf_s("%d", &a);
  50.     }
  51.  
  52.     int pos[4];
  53.  
  54.     pos[0] = (a / 100);
  55.     pos[1] = (a % 100) / 10;
  56.     pos[2] = (a % 100) % 10;
  57.  
  58.     printf("#1.\nThe sum of the number #%d is --> %d\n", a, pos[0] + pos[1] + pos[2]);
  59.     printf("#2.\nThe new number is: %d%d%d\n", pos[2], pos[1], pos[0]);
  60.  
  61.     pos[3] = pos[0] + (pos[1] * 10) + (pos[2] * 100);
  62.     printf("#2.\nThe new number is: %d\n", pos[3]);
  63.  
  64.     pos[0] = a / 365;
  65.     pos[1] = (a % 365) / 30;
  66.     pos[2] = (a % 365) % 30;
  67.     printf("#3.\n%d days are %d years and %d months with %d extra days.\n", a, pos[0],pos[1],pos[2]);
  68.     */
  69.  
  70.     /*
  71.     int gTemp[4];
  72.     int attempt;
  73.  
  74.     for (attempt = 0; attempt < 4; attempt++)
  75.     {
  76.         if (attempt < 3)
  77.             printf("Please enter the grade you recieved for Task #%d\n", attempt + 1);
  78.  
  79.         else
  80.             printf("Please enter the grade you recieved for the Test.\n");
  81.  
  82.             scanf_s("%d", &gTemp[attempt]);
  83.  
  84.             while (gTemp[attempt] < 0 || gTemp[attempt] > 100)
  85.             {
  86.                 printf("Invalid value for the 'grade' please try again.\n");
  87.                 scanf_s("%d", &gTemp[attempt]);
  88.             }
  89.     }
  90.  
  91.     printf("* Test Results:\n");
  92.     printf("[TASK 1]     %d\n", gTemp[0] );
  93.     printf("[TASK 2]     %d\n", gTemp[1] );
  94.     printf("[TASK 3]     %d\n", gTemp[2]);
  95.     printf("* Avg. Task Results: %d\n", (gTemp[0]+gTemp[1]+gTemp[2]) / 3);
  96.     printf("[TEST]       %d\n", gTemp[3]);
  97.     printf("* Test Results: %.0lf\n", (((gTemp[0] + gTemp[1] + gTemp[2]) / 3) * 0.2) + (gTemp[3] * 0.8));
  98.     */
  99.  
  100.     /*
  101.     int i;
  102.     double x, tempx;
  103.  
  104.     printf("Enter a value for X:\n");
  105.  
  106.     scanf_s("%lf", &x);
  107.  
  108.     tempx = x;
  109.  
  110.     for (i = 2; i <= 8;)
  111.     {
  112.         tempx *= x;
  113.  
  114.         printf("[X^%d]:    %.0lf\n", i, tempx);
  115.  
  116.         tempx *= x;
  117.        
  118.         i += 2;
  119.     }
  120.     */
  121.  
  122.     int Distance, CarSpeed, FlySpeed;
  123.  
  124.     printf("First enter the distance in KM from City A to City B:\n");
  125.  
  126.     scanf_s("%d", &Distance);
  127.  
  128.     while (Distance < 1)
  129.     {
  130.         printf("Invalid value for distance, please enter a new value:\n");
  131.  
  132.         scanf_s("%d", &Distance);
  133.     }
  134.  
  135.     printf("Now enter the speed of the cars in Km/h:\n");
  136.  
  137.     scanf_s("%d", &CarSpeed);
  138.    
  139.     while (CarSpeed < 1)
  140.     {
  141.         printf("Invalid value for speed, please enter a new value:\n");
  142.  
  143.         scanf_s("%d", &CarSpeed);
  144.     }
  145.  
  146.     printf("Lastly enter the bonus speed for the fly over the cars in Km/h:\n");
  147.  
  148.     scanf_s("%d", &FlySpeed);
  149.  
  150.     while (FlySpeed < 1)
  151.     {
  152.         printf("Invalid value for bonus, please enter a new value:\n");
  153.  
  154.         scanf_s("%d", &FlySpeed);
  155.     }
  156.  
  157.     printf("The distance between the cities in kilometers is: %d km\n", Distance);
  158.     printf("The speed of the cars in km/h is: %d km/h\n", CarSpeed);
  159.     printf("The speed of the fly in km/h is: %d km/h\n", CarSpeed + FlySpeed);
  160.  
  161.     printf("The cars will crash after: %.2lf hour(s)\n", (double) Distance / CarSpeed );
  162.     printf("Over this time the fly will travel a distance of: %.2lf km\n", (FlySpeed + CarSpeed) * ((double)Distance / CarSpeed));
  163. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment