1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/** * File: parity.c * Author: Risto Heinsar * Created: 02.09.2014 * Modified 29.12.2022 * * Description: The program reads a number from the user and checks * the parity of the number (odd or even) by using modulo division */ #include <stdio.h> // include an input-output library int main(void) // start of the main function { int number; // Integer type variable to hold the number // Prompts for and reads an integer printf("Enter an integer to verify\n"); scanf("%d", &number); // Checks if it is odd by checking the remainder if (number % 2 == 0) { printf("The number %d is even\n", number); } else // if the condition evaluated to false, this will run { printf("The number %d is odd\n", number); } return 0; // Program ended successfully } |
- The indentation for both the main function and the if/else statements
- The placement of dashes all around the code (between operators and numbers, after commas etc)
- Where to put the semicolon! Function calls need to have a semicolon in the end, however if condition should typically not be followed by a semicolon!
This is available in more detail in the coding style document
Also look for what has changed from the starter code. See what different and figure out why we changed that.
The things to look for in the algorithm
- How the condition is formed – all of the exit conditions are named and the condition itself lies in a comment connected to the decision node
- All of the arrow heads showing the direction of flow in the algorithm
- No language specific commands are listed
- End node includes a comment (this is because programs may have multiple end scenarios, some of which may for example come from errors.