Week 2 Code Examples
#include <stdio.h>
int main(void) {
int i = 5;
while (i >= 1) {
int j = 5;
while (j > 5 - i) {
printf("%d ", j);
j--;
}
printf("\n");
i--;
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 10;
if (answer == 12) {
printf("12 is the number\n");
}
else {
printf("12 is not the number\n");
}
if (answer % 2 == 0) {
printf("That's even steven\n");
}
else {
printf("That's pretty odd\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int sum = 0;
int to_add;
while (sum < 1000) {
printf("Enter a number to add: ");
scanf("%d", &to_add);
sum = sum + to_add;
}
printf("Our total sum was %d\n", sum);
return 0;
}
#include <stdio.h>
int main(void) {
int sum = 0;
int adder = 0;
while (sum < 100) {
printf("Please enter a number: ");
scanf("%d", &adder);
sum = sum + adder;
}
return 0;
}
#include <stdio.h>
int main(void) {
int total_seasons_watched = 0;
int seasons_watched;
while (total_seasons_watched < 50) {
printf("You've currently watched %d seasons of Survivor, not good enough!\n", total_seasons_watched);
printf("How many seasons of Survivor have you watched since last lecture? ");
scanf("%d", &seasons_watched);
if (seasons_watched >= 1) {
total_seasons_watched = total_seasons_watched + seasons_watched;
}
else if (seasons_watched == 0) {
printf("C'mon, you can do better!\n");
}
else {
printf("You can't un-watch Survivor, don't be silly!\n");
}
}
return 0;
}
#include <stdio.h>
int main(void) {
int sheep_count = 0;
while (sheep_count < 10) {
if (sheep_count < 5) {
printf("I'm wide awake, I've only counted %d sheep!\n", sheep_count);
}
else {
printf("I'm getting so very eepy, I've counted %d sheep!\n", sheep_count);
}
sheep_count++;
}
printf("zzzzzzzzzzzz\n");
return 0;
}
#include <stdio.h>
int main(void) {
int count = 0;
while (count < 10) {
printf("Count is currently %d\n", count);
count = count + 5;
}
return 0;
}
#include <stdio.h>
int main(void) {
int counter = 1;
while (counter < 5) {
printf("Printed %d times\n", counter);
counter = counter + 1
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 12;
if (answer == 10) {
printf("The answer is 10!\n");
}
else if (answer == 12) {
printf("The answer is 12!\n");
}
else {
printf("The answer is not 10!\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 12;
if (answer == 10) {
printf("The answer is 10!\n");
}
else {
printf("The answer is not 10!\n");
}
return 0;
}
#include <stdio.h>
enum weekdays {MON, TUE, WED, THURS, FRI, SAT, SUN};
int main(void) {
enum weekdays day;
day = SAT;
printf("The day of the week is %d\n", day);
return 0;
}
#include <stdio.h>
enum pokemon {VICTINI, BULBASAUR, IVYSAUR, VENUSAUR, CHARMANDER, CHARMELEON, CHARIZARD, SQUIRTLE, WARTOTLE, BLASTOISE, CATERPIE, METAPOD, BUTTERFREE,
WEEDLE, KAKUNA, BEEDRILL, PIDGEY, PIDGEOTTO, PIDGEOT, RATTATA, RATICATE, NIDORAN_M, NIDORINO, NIDOKING, NIDORAN_F, NIDORINA,
NIDOQUEEN, PIKACHU, RAICHU, EKANS, ARBOK};
int main(void) {
enum pokemon pokedex_number = PIKACHU;
printf("That Pokemon's Pokedex number is %d\n", pokedex_number);
return 0;
}
#include <stdio.h>
int main(void) {
return 0;
}
#include <stdio.h>
enum survivor_seasons {BORNEO = 1, THE_AUSTRALIAN_OUTBACK, AFRICA, MARQUESAS, THAILAND, AMAZON, PEARL_ISLANDS, ALL_STARS, VANUATU, PALAU,
GUATEMALA, PANAMA, COOK_ISLANDS, FIJI, CHINA, MICRONESIA, GABON, TOCANTINS, SAMOA, HEROES_VS_VILLAINS,
NICARAGUA, REDEMPTION_ISLAND, SOUTH_PACIFIC, ONE_WORLD, PHILIPPINES, CARAMOAN, BLOOD_VS_WATER, CAGAYANA,
SAN_JUAN_DEL_SUR, WORLDS_APART, CAMBODIA, KAOH_RONG, MILLENIALS_VS_GEN_X, GAME_CHANGERS, HEROES_VS_HEALERS_VS_HUSTLERS,
GHOST_ISLAND, DAVID_VS_GOLIATH, EDGE_OF_EXTINCTION, ISLAND_OF_THE_IDOLS, WINNERS_AT_WAR, _41, _42, _43, _44, _45, _46,
_47, _48, _49, _50_IN_THE_HANDS_OF_THE_FANS};
enum australian_survivor_seasons {_2016 = 1, _2017, CHAMPIONS_VS_CONTENDERS, CHAMPIONS_VS_CONTENDERS_2, ALL_STARS_AUS, BRAINS_VS_BRAWN,
BLOOD_VS_WATER_AUS, HEROES_VS_VILLAINS_AUS, TITANS_VS_REBELS, BRAINS_VS_BRAWN_2, VS_THE_WORLD,
REDEMPTIONS};
int main(void) {
enum survivor_seasons season = _41;
enum australian_survivor_seasons aus_season = TITANS_VS_REBELS;
printf("season %d of Australian Survivor is great!\n", aus_season);
if (season > ISLAND_OF_THE_IDOLS) {
printf("Everything around here is kinda mid\n");
}
else {
printf("Season %d is good!\n", season);
}
return 0;
}
#include <stdio.h>
int main(void) {
int grid_size;
printf("Enter a number for the grid: ");
scanf("%d", &grid_size);
int j = 1;
while (j <= grid_size) {
int i = 1;
while (i <= grid_size) {
printf("%d ", i);
i++;
}
printf("\n");
j++;
}
return 0;
}
#include <stdio.h>
int main(void) {
char letter;
printf("Enter a letter: ");
scanf(" %c", &letter);
if (letter < 67 || letter > 67) {
printf("We've avoided the brainrot letter!\n");
}
else {
printf("Darn\n");
}
}
#include <stdio.h>
int main(void) {
char letter;
printf("Enter a letter: ");
scanf(" %c", &letter);
if (letter == 'H' || letter == 'Y' || letter == 'E' || letter == 'N' || letter == 'R') {
printf("Those letters are in HENRY!\n");
}
else if (letter == 'A' || letter == 'L' || letter == 'E' || letter == 'X') {
printf("Those letters are in ALEX!\n");
}
else {
printf("You hate the coolest twins in the world!\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int fav_number;
printf("Enter your favourite number: ");
scanf("%d", &fav_number);
if (fav_number == 67) {
printf("Okay then, moving on...\n");
}
else if (fav_number == 66) {
printf("The Jedi better watch out!\n");
}
else {
printf("Yippee, brainrot has not claimed another victim!\n");
}
printf("Thanks for partaking in our survey!\n");
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 12;
int answer2 = 10;
if (answer == 12) {
if (answer2 == 10) {
printf("Two ifs were true\n");
}
else {
printf("One if was true\n");
}
}
else {
printf("No ifs were true\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 10;
if (answer == 10) {
printf("The answer is 10!\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int fav_season;
printf("Enter your favourite Survivor season: ");
scanf("%d", &fav_season);
if (fav_season == 1) {
printf("That's Survivor Borneo!\n");
}
else if (fav_season == 2) {
printf("That's Survivor The Australian Outback!\n");
}
else if (fav_season == 3) {
printf("That's Survivor Africa!\n");
}
else {
printf("I ran out of energy to write all these if statements\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int flag = 1;
while (flag == 1) {
printf("Learning C is fun!\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 13;
while (answer == 13) {
printf("uh oh\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer;
printf("Enter an answer here: ");
scanf("%d", &answer);
if (answer % 2 == 0 && answer >= 100) {
printf("I have learnt...something...\n");
}
else if (answer % 2 == 1) {
printf("I know that my number is odd, it may be more than 100, we don't know\n");
}
else {
printf("I know my number is < 100, and isn't odd\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int user_answer;
printf("Gimme a number: ");
scanf("%d", &user_answer);
if (user_answer % 2 == 0 && user_answer >= 16) {
printf("You've found the answer!\n");
}
else {
printf("You haven't found the answer yet!\n");
}
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 12;
// When will this loop end? HINT - Never
while (answer == 12) {
printf("This will take a while\n");
}
return 0;
}
#include <stdio.h>
enum survivor {BORNEO = 1, THE_AUSTRALIAN_OUTBACK, AFRICA, MARQUESAS, THAILND, AMAZON, PEARL_ISLANDS, ALL_STARS,
VANUATU, PALAU, GUATEMALA, PANAMA, COOK_ISLANDS, FIJI, CHINA, MICRONESIA, GABON, TOCANTINS, SAMOA,
HEROES_VS_VILLAINS, NICARAGUA, REDEMPTIONS_ISLAND, SOUTH_PACIFIC, ONE_WORLD, PHILIPPINES, CARAMOAN,
BLOOD_VS_WATER, CAGAYAN, SAN_JUAN_DEL_SUR, WORLDS_APART, CAMBODIA, KAOH_RONG, MILLENIALS_VS_GEN_X,
GAME_CHANGERS, HEROES_VS_HEALERS_VS_HUSTLERS, GHOST_ISLAND, DAVID_VS_GOLIATH, EDGE_OF_EXTINCTION,
ISLAND_OF_THE_IDOLS, WINNERS_AT_WAR, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50_IN_THE_HANDS_OF_THE_FANS};
int main(void) {
enum survivor season;
season = GABON;
printf("Survivor Season %d is good!\n", season);
return 0;
}
#include <stdio.h>
int main(void) {
int answer = 12;
if (answer == 12) {
if (answer % 2 == 0) {
if (answer % 3 == 0) {
printf("You are both 12, and even, and divisible by 3\n");
}
}
}
return 0;
}
#include <stdio.h>
int main(void) {
int i = 1;
while (i <= 5) {
int j = 1;
while (j <= i) {
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
#include <stdio.h>
int main(void) {
int num1 = 10;
int num2;
printf("Enter the second number: ");
scanf("%d", &num2);
printf("The result of your number, multiplied by %d, is %d!\n", num1, num1 * num2);
return 0;
}
#include <stdio.h>
int main(void) {
char character;
printf("Enter a character: ");
scanf("%c", &character);
printf("The character you entered, shifted by 1, is %c!\n", character + 1);
return 0;
}
#include <stdio.h>
int main(void) {
double user_input;
printf("Enter a double: ");
scanf("%lf", &user_input);
printf("The double rounded to 2.dp is %.2lf\n", user_input);
return 0;
}
#include <stdio.h>
int main(void) {
int odd_entered = 0;
int user_input;
while (odd_entered == 0) {
printf("Enter a number from 1 to 10: ");
scanf("%d", &user_input);
if (user_input < 1 || user_input > 10) {
printf("Make sure your number is between 1 and 10\n");
}
else if (user_input % 2 == 1) {
printf("Odd number detected\n");
odd_entered = 1;
}
else {
printf("Try again, new number time!\n");
}
}
return 0;
}
#include <stdio.h>
int main(void) {
int user_input = 12;
while (!(user_input % 2 == 1)) {
printf("Enter a number: ");
scanf("%d", &user_input);
}
printf("Congratulations, you escaped!\n");
return 0;
}
#include <stdio.h>
int main(void) {
int exit_condition = 0;
int coconuts_harvested = 0;
int total_coconuts = 0;
while (exit_condition == 0) {
printf("How many coconuts will you collect? ");
scanf("%d", &coconuts_harvested);
total_coconuts = total_coconuts + coconuts_harvested;
if (total_coconuts > 40) {
printf("We have enough food to survive! Yay!!\n");
exit_condition = 1;
}
else {
printf("Let's keep harvesting!\n");
}
}
return 0;
}
#include <stdio.h>
struct pokemon_stats {
int hp;
int attack;
int defense;
int sp_attack;
int sp_defense;
int speed;
};
struct person {
char first_initial;
char last_innitial;
int height;
};
int main(void) {
struct pokemon_stats glalie;
struct person my_brother;
glalie.hp = 80;
glalie.attack = 80;
glalie.defense = 80;
glalie.sp_attack = 80;
glalie.sp_defense = 80;
glalie.speed = 80;
my_brother.first_initial = 'A';
my_brother.last_innitial = 'H';
my_brother.height = 195;
if (glalie.hp > 90) {
printf("That's a good HP stat\n");
} else {
printf("This Pokemon seems pretty mid\n");
}
if (my_brother.height > 180) {
printf("That's pretty tall!\n");
}
return 0;
}
#include <stdio.h>
struct coordinate {
int x_coord;
int y_coord;
};
int main(void) {
struct coordinate one_piece;
one_piece.x_coord = 15;
one_piece.y_coord = 100;
one_piece.x_coord = one_piece.x_coord * 10;
one_piece.y_coord = one_piece.x_coord + 100;
printf("The One Piece is located at X = %d, and Y = %d!\n", one_piece.x_coord, one_piece.y_coord);
return 0;
}
#include <stdio.h>
#define COCONUTS_LEFT 151
int main(void) {
int guess;
printf("How many coconuts do you think there are? ");
scanf("%d", &guess);
if (guess == COCONUTS_LEFT) {
printf("Congratulations! You win immunity!\n");
}
else if ((guess >= COCONUTS_LEFT - 5 && guess < COCONUTS_LEFT) || (guess <= COCONUTS_LEFT + 5 && guess > COCONUTS_LEFT)) {
printf("So close, yet so far!\n");
}
else if (guess < COCONUTS_LEFT) {
printf("Not even close, way more than that.\n");
}
else if (guess > COCONUTS_LEFT) {
printf("Not even close, way less than that.\n");
}
return 0;
}
#include <stdio.h>
#define COCONUTS_LEFT 151
int main(void) {
int guess;
int total_guess = 0;
while (total_guess < 5) {
printf("How many coconuts do you think there are? ");
scanf("%d", &guess);
if (guess == COCONUTS_LEFT) {
printf("Congratulations! You win immunity!\n");
}
else if ((guess >= COCONUTS_LEFT - 5 && guess < COCONUTS_LEFT) || (guess <= COCONUTS_LEFT + 5 && guess > COCONUTS_LEFT)) {
printf("So close, yet so far!\n");
}
else if (guess < COCONUTS_LEFT) {
printf("Not even close, way more than that.\n");
}
else if (guess > COCONUTS_LEFT) {
printf("Not even close, way less than that.\n");
}
total_guess++;
}
return 0;
}
#include <stdio.h>
#define COCONUTS_LEFT 151
int main(void) {
int guess;
int not_close_guesses = 0;
while (not_close_guesses < 10) {
printf("How many coconuts do you think there are? ");
scanf("%d", &guess);
if (guess == COCONUTS_LEFT) {
printf("Congratulations! You win immunity!\n");
}
else if ((guess >= COCONUTS_LEFT - 5 && guess < COCONUTS_LEFT) || (guess <= COCONUTS_LEFT + 5 && guess > COCONUTS_LEFT)) {
printf("So close, yet so far!\n");
}
else if (guess < COCONUTS_LEFT) {
printf("Not even close, way more than that.\n");
not_close_guesses++;
}
else if (guess > COCONUTS_LEFT) {
printf("Not even close, way less than that.\n");
not_close_guesses++;
}
}
return 0;
}
#include <stdio.h>
#define COCONUTS_LEFT 151
int main(void) {
int guess;
int is_correct = 0;
while (is_correct == 0) {
printf("How many coconuts do you think there are? ");
scanf("%d", &guess);
if (guess == COCONUTS_LEFT) {
printf("Congratulations! You win immunity!\n");
is_correct = 1;
}
else if ((guess >= COCONUTS_LEFT - 5 && guess < COCONUTS_LEFT) || (guess <= COCONUTS_LEFT + 5 && guess > COCONUTS_LEFT)) {
printf("So close, yet so far!\n");
}
else if (guess < COCONUTS_LEFT) {
printf("Not even close, way more than that.\n");
}
else if (guess > COCONUTS_LEFT) {
printf("Not even close, way less than that.\n");
}
}
return 0;
}