Week 2 Code Examples
// A program to revise the following concepts
// Tricky expressions using
// int, double, char
//
// Written by: Angela Finlayson
// Date: 16/09/2026
#include <stdio.h>
int main(void) {
int x = 11;
int y = x / 2;
int remainder = x % 3;
char letter = 'B' - 'A' + 'a';
printf("%d modulus 3 = %d\n", x, remainder);
printf("%d / 2 = %d\n", x, y);
printf("'B' - 'A' + 'a' = %c\n", letter);
return 0;
}
// A program to revise the following concepts
// Declaring, initialising, modifying and printing variables of types
// int, double, char
// Using simple mathematical operators
//
// Written by: Angela Finlayson
// Date: 16/09/2026
//
// This program prints out details of the player's initials
// gold and health
// The gold and health get updated.
#include <stdio.h>
int main(void) {
int gold = 10;
double health = 85;
char initial = 'X';
printf("Player: %c\n", initial);
printf("Gold: %d Health: %.2lf\n", gold, health);
gold = gold + 1; //10 + 1 = 11
health = health / 2; //85 / 2 = 42.5
printf("Gold: %d Health: %.1lf\n", gold, health);
// How could we print to 1 decimal place?
return 0;
}
// A program to revise the following concepts
// Using scanf to read in data from the user of types
// int and double
//
// Written by: Angela Finlayson
// Date: 16/09/2026
//
// This program reads in integer coordinates and a real value for a length
#include <stdio.h>
// In class
// Show error input - see how to handle this later today!!
int main(void) {
int x;
int y;
printf("Enter coordinates: ");
scanf("%d %d", &x, &y);
printf("You entered (%d, %d)\n", x, y);
double length;
printf("Enter length: ");
scanf("%lf", &length);
printf("You entered %lf\n", length);
return 0;
}
// This is an example of using constants
// to give meaning to values used in our code
#include <stdio.h>
#define STARTING_GOLD 100
#define SWORD_COST 40
#define SHIELD_COST 30
#define POT_OF_GOLD 50
int main (void) {
int gold = STARTING_GOLD;
gold = gold - SWORD_COST;
gold = gold + POT_OF_GOLD;
gold = gold - SHIELD_COST;
printf("Gold: %d\n", gold);
return 0;
}
// This is an example of use magic numbers
// A magic number is an unnamed hardcoded value whose meaning is unclear.
// Compare this code to the code in constants.c that
// replaces magic numbers with #define constants
#include <stdio.h>
int main (void) {
int gold = 100;
gold = gold - 40;
gold = gold + 50;
gold = gold - 30;
printf("Gold: %d\n", gold);
return 0;
}
// Write a program to allow a user to enter a character
// Print out whether the character is an uppercase letter,
// a lowercase letter, or not a letter.
#include <stdio.h>
int main(void){
// Do we want to use " %c" or "%c"
char input;
printf("Enter a character: ");
scanf("%c", &input);
if (input >= 'a' && input <= 'z') {
printf("Lowercase\n");
} else if (input >= 'A' && input <= 'Z') {
printf("Uppercase\n");
} else {
printf("Not a letter\n");
}
return 0;
}
// Demonstration of nested if statements
// Written by: Angela Finlayson
// Date: 20/09/2026
// $ ./game_level
// Did you complete the level? (y/n): n
// Keep playing!
// $ ./game_level
// Did you complete the level? (y/n): y
// How many coins did you collect? 10
// Bonus: 3 stars!
// $ ./game_level
// Did you complete the level? (y/n): y
// How many coins did you collect? 9
// Bonus: 1 star!
#include <stdio.h>
#define MAX_COINS 10
int main(void) {
char input;
printf("Did you complete the level? (y/n) ");
scanf(" %c", &input);
if (input == 'y') {
printf("How many coins did you collect? ");
int gold;
scanf("%d", &gold);
if (gold >= MAX_COINS) {
printf("Bonus: 3 stars!\n");
} else {
printf("Bonus: 1 star!\n");
}
} else if (input == 'n'){
printf("Keep Playing!\n");
}
return 0;
}
// Write a program guessing_game.c that
// prompts the user enter an integer
// prints out higher, lower, correct if it is equal to the secret number (42)
// $ ./guessing_game
// Please enter an integer: 8
// Higher!
// $ ./guessing_game
// Please enter an integer: 42
// Correct!
#include <stdio.h>
#define SECRET_NUMBER 42
int main(void){
int guess;
printf("Please enter an integer: ");
int num_read = scanf("%d", &guess);
if (num_read != 1){
printf("Incorrect Usage: enter an integer\n");
} else if (guess == SECRET_NUMBER) {
printf("Correct!\n");
} else if (guess > SECRET_NUMBER) {
printf("Lower!\n");
} else {
printf("Higher\n");
}
printf("Game over\n");
return 0;
}
// Demonstration of simple if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
// Note the indentation
if (1) {
printf("Hooray\n");
}
if (0) {
printf("Yay!\n");
}
if (4 == 4) {
printf("I love C!\n");
}
if ('Z' == 'z') {
printf("I am cool!\n");
}
int x = 5;
int y = 10;
if (x < 0) {
printf("x is negative!\n");
}
if (y >= x) {
printf("y is greater or equal\n");
}
if (x != y) {
printf("x and y not equal!\n");
}
printf("I love if statements!\n");
return 0;
}
// Demonstration of nested if statements
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
int x;
printf("Enter an integer: ");
scanf("%d", &x);
if (x > 0) {
if (x > 100) {
printf("big");
} else {
printf("small");
}
printf(" positive");
} else {
printf("Not a positive");
}
printf(" number\n");
return 0;
}
// Write a program odd_even.c that
// prompts the user enter an integer
// prints out whether the number is even or odd
//
// $ ./odd_even
// Please enter an integer: 8
// Even!
// ./odd_even
// Please enter an integer: 3
// Odd!
#include <stdio.h>
int main(void){
int value;
printf("Please enter an integer: ");
scanf("%d", &value);
if (value % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
// Demonstration of error checking using the return value from scanf
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void){
int x;
int y;
double z;
printf("Enter 2 integers and real number: ");
int inputs_read = scanf("%d %d %lf", &x, &y, &z);
printf("%d inputs were read in\n", inputs_read);
if (inputs_read == 3) {
printf("Inputs were: %d %d %lf\n", x, y, z);
} else {
printf("Incorrect usage: Enter 2 integers and real number\n");
}
return 0;
}
// Demonstration of chained else if statements
// with simple conditions
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
#define COLD 10
#define HOT 25
// What test cases should we use?
int main(void) {
int temperature;
printf("Please enter the temperature: ");
scanf("%d", &temperature);
if (temperature <= COLD) {
printf("I am cold!\n");
} else if (temperature < HOT) {
printf("Just right!\n");
} else {
printf("I am hot!\n");
}
printf("Have a nice day\n");
return 0;
}
// Demonstration of conditional while loops
// We do not know exactly how many times the loop will repeat
// There is no specific input value that stops it
// There is just a condition
// Written by: Angela Finlayson
// Date: 19/09/2026
#include <stdio.h>
#define MAX_KOMBUCHA 2000
int main(void) {
// 1. Initialise the loop control variable
int total_kombucha_ml = 0;
int kombucha_ml;
while (total_kombucha_ml < 2000) { // 2. Test the loop condition
printf("Please enter the ml of kombucha: ");
scanf("%d", &kombucha_ml);
// 3. Update loop control variable
total_kombucha_ml = total_kombucha_ml + kombucha_ml;
}
printf("Kombucha target reached %d!!\n", total_kombucha_ml);
return 0;
}
// Demonstration of conditional while loops
// We do not know exactly how many times the loop will repeat
// There is no specific input value that stops it
// There is just a condition
// In this example we use a flag variable to control the loop
// Written by: Angela Finlayson
// Date: 19/09/2026
#include <stdio.h>
int main(void) {
int pin;
int confirmation;
int pins_match = 0;
while (pins_match == 0) {
printf("Enter a PIN: ");
scanf("%d", &pin);
printf("Enter the PIN again: ");
scanf("%d", &confirmation);
if (pin == confirmation) {
pins_match = 1;
}
}
printf("PIN saved!\n");
return 0;
}
// Demonstration of counting while loops
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
#define MAX 3
//Trace with 3
//Try going backwards
int main(void) {
// 1. Initialise loop counter before the loop
int counter = 0;
while (counter < MAX) { // 2. check loop counter condition
printf("%d Here we go loop de loop!\n", counter);
counter = counter + 1; // 3. update loop counter
}
// What will this print?
printf("%d Yay the loop ended!!!\n", counter);
return 0;
}
// Demonstration of infinite while loops
// Typically you do not want an infinite loop!
// Type Ctrl+C to end program.
//
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
// Change to a while loop
while (1) {
printf("I love my COMP1511/1911 lectures!\n");
}
// Infinite loop
// How could we stop it?
// int push_ups = 0;
// while (push_ups < 10) {
// printf("You have done %d push-ups!\n", push_ups);
// }
// printf("Congratulations you have done %d push-ups!\n", push_ups);
return 0;
}
// Modify this code to calculate the sum of all integers entered
// by the user
// The loop should end when the user enters non-integer data
//
// Written by: Angela Finlayson
// Date: 20/09/2026
#include <stdio.h>
int main(void) {
return 0;
}
// Demonstration of sentinel while loops
// A sentinel value as a specific input value that terminates the loop
// Written by: Angela Finlayson
// Date: 18/09/2026
#include <stdio.h>
int main(void) {
char grade;
printf("Enter a grade (q to quit): ");
scanf(" %c", &grade);
// Stop when we reach the sentinel value 'q'
while (grade != 'q') {
printf("You entered %c\n", grade);
printf("Enter a grade (q to quit): ");
scanf(" %c", &grade);
}
return 0;
}