C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
/* The student got 88s on the first and third test,
so a multiple assignment statement works. */
grade1 = grade3 = 88;
grade2 = 79;
// The user needs to enter the fourth grade
printf("What did you get on the fourth test");
printf(" (An integer between 0 and 100)?");
scanf(" %d", &grade4);
averageGrade = (grade1+grade2+grade3+grade4)/4;
printf("Your average is %.1f.\n", averageGrade);
gradeDelta = 95 - averageGrade;
percentDiff = 100 * ((95-averageGrade) / 95);
printf("Your grade is %.1f points lower than the ", gradeDelta);
printf("top grade in the class (95)\n");
printf("You are %.1f percent behind ", percentDiff);
printf("that grade!\n\n\n");
return 0;
}

This program helps reinforce the use of the assignment operators, as well as the operators for
addition, subtraction, multiplication, and division. You also use parentheses to set your own order of
operations, including a double parentheses when calculating the percent difference between the user’s
grade and the top grade in the class. Keep practicing these C programs, and you will have the top
grade in your programming class!


The Absolute Minimum
C provides several math operators that do calculations for you. You just need to
understand the order of operators to ensure that you input your numbers correctly for
your desired calculations. Key concepts from this chapter include:


  • Use +, -, *, and / for addition, subtraction, multiplication, and division,
    respectively.

  • Use the modulus operator (%) if you want the remainder of an integer division.

  • Remember the order of operators, and use parentheses if you need to change the
    order.

  • Don’t put two minus signs together if you are subtracting a negative number, or C
    will think you are using a different operator. Place a space between the two minus
    signs.

  • Use multiple assignment operators if you have several variables to initialize.

Free download pdf