© Fort Street High School Robotics
Use printf
to display text on the screen.
printf("I like robotics!\n");
printf("%d pizzas cost %d.\n", pizzas, cost);
Store numbers in variables. Start them with int
(and maybe float
).
int chickens = 17;
int people;
people = 10;
Use the various symbols to do mathematical operations.
Action | Symbol | Example | Comment |
---|---|---|---|
Addition | + |
pizzas = 5 + 3; |
|
Subtraction | - |
coolnessFactor = coolnessFactor - 1; |
Can refer to itself - e.g. it will count down by 1 |
Multiplication | * |
money = money * money; |
This is equivalent to squaring the original |
Division | / |
printf("%d", 5 / 3) |
|
Modulus | % |
printf("%d", 5 % 3) |
Use scanf
to allow input from the user. Remember the &
!
int year;
printf("Enter a year: ");
scanf("%d", &year);
Use if
and a condition to make decisions.
int x = 1;
if (x != 3) {
...
} else if (x <= 10) {
...
} else {
...
}
Operator | Comment | Example |
---|---|---|
== |
Equals. Very different from = . |
5 == 10 is false. |
!= |
Not equals | 5 != 10 is true. |
< |
Less than, checks if left is less than right. | 5 < 10 is true. |
> |
Greater than, checks if left is greater than right. | 5 > 10 is false. |
<= |
Less than or equal to. | 5 <= 10 is true. |
>= |
Greater than or equal to. | 5 >= 10 is false. |
Operator | Comment | Example |
---|---|---|
&& |
AND | If two conditions are true. |
|| |
OR | If one of two conditions are true. |
! |
NOT | Inverts the condition. |
To do the exercises, use Repl.it.
Write a program calcs.c
that calculates 39*623+10
.
Your program output the following:
24307
Write a program pizzacalcs.c
that allows the user to enter the number of pizzas. The program should print out the total slices (each pizza is divided into 8 slices) and the total slices per person if there are 5 people.
Examples:
How many pizzas: 5
There are 40 slices of pizza.
Each person gets 8.
How many pizzas: 1
There are 8 slices of pizza.
Each person gets 1.
Using an if
statement (with else if
and else
), write a program soccerrobots.c
that allows the user to enter a number of robots. It should check if the number of robots is 2:
You have the correct number of robots!
You don't have enough robots.
You have too many robots.
Examples:
Number of robots: 5
You have too many robots.
Number of robots: 2
You have the correct number of robots!
Georgio has $10 dollars and he wants to buy his friend some icecream. Write a program so that he can input the number of scoops and how much each scoop costs, and it will tell him if he has enough money.
Your program should perform as follows:
How many scoops? 5
How much per scoop? 1
You can buy the icecream!
How many scoops? 5
How much per scoop? 3
Rip, you don't have enough money :(
At Petersham's station, the ticket office wants a simple program that can immediately tell which ticket someone should buy.
Write a program that allows the user to enter an age, and it should print out which ticket they should receive.
Child ticket
Concession ticket
Concession ticket
Adult ticket
Your program should perform as follows:
Enter your age: 12
Child ticket
Enter your age: 13
Concession ticket
Enter your age: 40
Adult ticket
Uh oh! We gave Mr Semaan the task to write some C code. Unfortunately, it's a bit wrong. It's meant to ask you to put in his favourite number, and print out if it is right or not.
Your task is to fix up his program so that it is indented properly, compiles with no errors and it returns the correct results.
#include stdio.h>
int main() {
printf("Guess my favourite number: ");
scanf("%d", n);
int faveNumber;
if (faveNumber = 17) {
printf("You're right!\n");
}
else {
printf("You're wrong.\n");
}
return 0;
}
Write a C program leapyear.c
that reads a year and then prints whether that year is a leap year.
Note that leap years aren't just divisble by 4. Use this pseudocode from Wikipedia:
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
Examples:
Enter year: 2017
2017 is not a leap year.
Enter year: 2016
2016 is a leap year.
Enter year: 2000
2000 is a leap year.
Enter year: 3000
3000 is not a leap year.
Write a program tictactoe.c
that takes in a tic tac toe board and determines who wins in that game. Player 1 is represented by '1', Player 2 is represented by '2', and empty cells are represented by '0's.
scanf("%d %d %d", &cell1, &cell2, &cell3);
Your program should behave as follows:
Enter the board:
1 2 1
1 1 2
2 2 1
Player 1 has won!
Enter the board:
0 0 1
1 1 0
2 2 2
Player 2 has won!
Enter the board:
0 0 1
0 0 0
0 0 2
No players have won.
Solutions will be posted here.