Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. // Exercises 2.16
  2. // (Arithmetic) Write a program that asks the user to enter two numbers, obtains
  3. // them from the user and prints their sum, product, difference, quotient, and
  4. // remainder.
  5.  
  6. #include <stdio.h>
  7.  
  8. int main()
  9. {
  10. int x, y, sum, product, difference, quotient, remainder;
  11.  
  12. printf( "Please enter two numbers.\n" );
  13. scanf( "%d%d", &x, &y );
  14.  
  15. sum = x + y;
  16. printf( "The sum of %d and %d is %d.\n", x, y, sum );
  17.  
  18. product = x * y;
  19. printf( "The product is %d.\n", product );
  20.  
  21. difference = x - y;
  22. printf( "The difference of %d and %d is %d.\n", x, y, difference );
  23.  
  24. quotient = x / y;
  25. printf( "The quotient is %d.\n", quotient );
  26.  
  27. remainder = x % y;
  28. printf( "The remainder of %d and %d is %d.\n", x, y, remainder);
  29.  
  30. return 0;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. // Exercises 2.19
  38. // (Arithmetic, Largest value and Smallest Value) Write a program that inputs
  39. // three different integers from the keyboard, then prints the sum, the average,
  40. // the product, the smallest, and the largest of these numbers. Use only the
  41. // single selection form of the if statement you learned in this chapter. The
  42. // screen dialog should appear as follows:
  43. //
  44. // Input three different integers: 13 27 14
  45. // Sum is 54
  46. // Average is 18
  47. // Product is 4914
  48. // Smallest is 13
  49. // Largest is 27
  50.  
  51. #include <stdio.h>
  52.  
  53. int main() {
  54. int x, y, z, sum, average, product;
  55.  
  56. printf( "Input three different integers: ");
  57. scanf( "%d%d%d", &x, &y, &z );
  58.  
  59. sum = x + y + z;
  60. printf("Sum is %d\n", sum );
  61.  
  62. average = (x + y + z ) / 3;
  63. printf("Average is %d\n", average );
  64.  
  65. product = x * y * z;
  66. printf("Product is %d\n", product );
  67.  
  68. if ( x < y ) {
  69. if ( x < z ) {
  70. printf("Smallest is %d\n", x );
  71. }
  72. }
  73.  
  74. if ( y < x ) {
  75. if ( y < z ) {
  76. printf("Smallest is %d\n", y );
  77. }
  78. }
  79.  
  80. if ( z < x ) {
  81. if ( z < y ) {
  82. printf("Smallest is %d\n", z );
  83. }
  84. }
  85.  
  86. if ( x > y ) {
  87. if ( x > z ) {
  88. printf("Largest is %d\n", x );
  89. }
  90. }
  91.  
  92. if ( y > x ) {
  93. if ( y > z ) {
  94. printf("Largest is %d\n", y );
  95. }
  96. }
  97.  
  98. if ( z > x ) {
  99. if ( z > y ) {
  100. printf("Largest is %d\n", z );
  101. }
  102. }
  103.  
  104. return 0;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. // Exercises 2.20
  113. // (Diameter, Circumference and Area of a Circle) Write a program that reads in
  114. // the radius of a circle and prints the circle's diameter, circumference and
  115. // area. Use the constant value 3.14159 for π. Perform each of these calculations
  116. // inside the printf statement(s) and use the conversion specifier %f.
  117. //
  118. // [Note: In this chapter, we have discussed only integer constants and variables
  119. // In Chapter 3 we'll discuss floating-point numbers, i.e., values that can have
  120. // decimal points.]
  121.  
  122. #include <stdio.h>
  123.  
  124. int main() {
  125. int radius, diameter;
  126. double constantValue, circumference, area;
  127.  
  128. printf( "Radius of the circle: \n" );
  129. scanf( "%d", &radius );
  130.  
  131. diameter = radius * 2;
  132. printf( "The diameter is %d\n", diameter );
  133.  
  134. constantValue = 3.14159;
  135. circumference = constantValue * diameter;
  136. printf( "The circumference is %f\n", circumference );
  137.  
  138. area = constantValue * radius * radius;
  139. printf("The area of the circle is %f\n", area );
  140.  
  141. return 0;
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. // Exercises 2.21
  149. // (Shapes with Asterisks) Write a program that prints the following shapes
  150. // with asterisks.
  151.  
  152. #include <stdio.h>
  153.  
  154. int main() {
  155.  
  156. printf("*********\n* *\n* *\n* *\n* *\n* *\n* *\n* *\n*********\n");
  157. printf(" *** \n * *\n* *\n* *\n* *\n* *\n* *\n * *\n *** \n");
  158. printf(" * \n *** \n*****\n * \n * \n * \n * \n * \n * \n");
  159. printf(" * \n * * \n * * \n * * \n* *\n * * \n * * \n * * \n * \n");
  160.  
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement