Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exercises 2.16
- // (Arithmetic) Write a program that asks the user to enter two numbers, obtains
- // them from the user and prints their sum, product, difference, quotient, and
- // remainder.
- #include <stdio.h>
- int main()
- {
- int x, y, sum, product, difference, quotient, remainder;
- printf( "Please enter two numbers.\n" );
- scanf( "%d%d", &x, &y );
- sum = x + y;
- printf( "The sum of %d and %d is %d.\n", x, y, sum );
- product = x * y;
- printf( "The product is %d.\n", product );
- difference = x - y;
- printf( "The difference of %d and %d is %d.\n", x, y, difference );
- quotient = x / y;
- printf( "The quotient is %d.\n", quotient );
- remainder = x % y;
- printf( "The remainder of %d and %d is %d.\n", x, y, remainder);
- return 0;
- }
- // Exercises 2.19
- // (Arithmetic, Largest value and Smallest Value) Write a program that inputs
- // three different integers from the keyboard, then prints the sum, the average,
- // the product, the smallest, and the largest of these numbers. Use only the
- // single selection form of the if statement you learned in this chapter. The
- // screen dialog should appear as follows:
- //
- // Input three different integers: 13 27 14
- // Sum is 54
- // Average is 18
- // Product is 4914
- // Smallest is 13
- // Largest is 27
- #include <stdio.h>
- int main() {
- int x, y, z, sum, average, product;
- printf( "Input three different integers: ");
- scanf( "%d%d%d", &x, &y, &z );
- sum = x + y + z;
- printf("Sum is %d\n", sum );
- average = (x + y + z ) / 3;
- printf("Average is %d\n", average );
- product = x * y * z;
- printf("Product is %d\n", product );
- if ( x < y ) {
- if ( x < z ) {
- printf("Smallest is %d\n", x );
- }
- }
- if ( y < x ) {
- if ( y < z ) {
- printf("Smallest is %d\n", y );
- }
- }
- if ( z < x ) {
- if ( z < y ) {
- printf("Smallest is %d\n", z );
- }
- }
- if ( x > y ) {
- if ( x > z ) {
- printf("Largest is %d\n", x );
- }
- }
- if ( y > x ) {
- if ( y > z ) {
- printf("Largest is %d\n", y );
- }
- }
- if ( z > x ) {
- if ( z > y ) {
- printf("Largest is %d\n", z );
- }
- }
- return 0;
- }
- // Exercises 2.20
- // (Diameter, Circumference and Area of a Circle) Write a program that reads in
- // the radius of a circle and prints the circle's diameter, circumference and
- // area. Use the constant value 3.14159 for π. Perform each of these calculations
- // inside the printf statement(s) and use the conversion specifier %f.
- //
- // [Note: In this chapter, we have discussed only integer constants and variables
- // In Chapter 3 we'll discuss floating-point numbers, i.e., values that can have
- // decimal points.]
- #include <stdio.h>
- int main() {
- int radius, diameter;
- double constantValue, circumference, area;
- printf( "Radius of the circle: \n" );
- scanf( "%d", &radius );
- diameter = radius * 2;
- printf( "The diameter is %d\n", diameter );
- constantValue = 3.14159;
- circumference = constantValue * diameter;
- printf( "The circumference is %f\n", circumference );
- area = constantValue * radius * radius;
- printf("The area of the circle is %f\n", area );
- return 0;
- }
- // Exercises 2.21
- // (Shapes with Asterisks) Write a program that prints the following shapes
- // with asterisks.
- #include <stdio.h>
- int main() {
- printf("*********\n* *\n* *\n* *\n* *\n* *\n* *\n* *\n*********\n");
- printf(" *** \n * *\n* *\n* *\n* *\n* *\n* *\n * *\n *** \n");
- printf(" * \n *** \n*****\n * \n * \n * \n * \n * \n * \n");
- printf(" * \n * * \n * * \n * * \n* *\n * * \n * * \n * * \n * \n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement