move exercises and lectures into subfolders
This commit is contained in:
24
Exercises/C9/do_while_loop.c
Normal file
24
Exercises/C9/do_while_loop.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int age; // Declare variable, no need to initialise this time
|
||||
// as we read into it before it is tested against
|
||||
|
||||
// The loop is always entered as the test is at the end
|
||||
do
|
||||
{
|
||||
// Code must be executed at least once
|
||||
printf ("\nPlease enter your age");
|
||||
scanf("%d", &age);
|
||||
printf ("You are %d years old\n", age);
|
||||
|
||||
// Test is now made and the code
|
||||
// repeats if the test equates as non-zerp
|
||||
// (i.e. is age is not zero)
|
||||
}
|
||||
while ( age != 0);
|
||||
|
||||
return 0;
|
||||
}
|
23
Exercises/C9/ex2.c
Normal file
23
Exercises/C9/ex2.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int age = 1; // Declare variable and initialise to 1
|
||||
|
||||
while ( age != 0) // Loop as long as age is not zero
|
||||
{
|
||||
// Code in {} executed if condition is true (non-zero)
|
||||
|
||||
printf ("\nPlease enter your age: ");
|
||||
scanf("%d", &age);
|
||||
printf ("You are %d years old\n", age);
|
||||
|
||||
if (age == 18 || age == 21) {
|
||||
printf("You have come of age\n");
|
||||
}
|
||||
|
||||
// Code now goes back and repeats the test with the value of age just entered
|
||||
}
|
||||
return 0; // Exit code
|
||||
}
|
14
Exercises/C9/ex3.c
Normal file
14
Exercises/C9/ex3.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_NAME_LEN 200
|
||||
|
||||
int main() {
|
||||
char name[MAX_NAME_LEN] = { 0 };
|
||||
|
||||
printf("Please enter your name: ");
|
||||
scanf("%s", name);
|
||||
|
||||
while (1) {
|
||||
printf("%s\n", name);
|
||||
}
|
||||
}
|
21
Exercises/C9/ex4.c
Normal file
21
Exercises/C9/ex4.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int age = 1; // Declare variable and initialise to 1
|
||||
|
||||
do {
|
||||
|
||||
printf ("\nPlease enter your age: ");
|
||||
scanf("%d", &age);
|
||||
printf ("You are %d years old\n", age);
|
||||
|
||||
if (age == 18 || age == 21) {
|
||||
printf("You have come of age\n");
|
||||
}
|
||||
|
||||
// Code now goes back and repeats the test with the value of age just entered
|
||||
} while ( age != 0); // Loop as long as age is not zero
|
||||
return 0; // Exit code
|
||||
}
|
9
Exercises/C9/ex5.c
Normal file
9
Exercises/C9/ex5.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
for (int i = 1; i <= 15; i++) {
|
||||
printf("%d\n", i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
29
Exercises/C9/ex6.c
Normal file
29
Exercises/C9/ex6.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int rc, start, end;
|
||||
|
||||
printf("Start: ");
|
||||
rc = scanf("%d", &start);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
printf("End: ");
|
||||
rc = scanf("%d", &end);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (start > end) {
|
||||
printf("Please ensure that start value is smaller than end value\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
printf("%d\n", i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
29
Exercises/C9/ex7.c
Normal file
29
Exercises/C9/ex7.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int rc, start, end;
|
||||
|
||||
printf("Start: ");
|
||||
rc = scanf("%d", &start);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
printf("End: ");
|
||||
rc = scanf("%d", &end);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (start > end) {
|
||||
printf("Please ensure that start value is smaller than end value\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
printf("%d %d\n", i, i*i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
32
Exercises/C9/ex8.c
Normal file
32
Exercises/C9/ex8.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int rc, start, end, sum = 0;
|
||||
|
||||
printf("Start: ");
|
||||
rc = scanf("%d", &start);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
printf("End: ");
|
||||
rc = scanf("%d", &end);
|
||||
if (rc != 1) {
|
||||
printf("Please enter a real integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (start > end) {
|
||||
printf("Please ensure that start value is smaller than end value\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
printf("%d\n", i);
|
||||
sum += i;
|
||||
}
|
||||
|
||||
printf("Sum: %d\n", sum);
|
||||
|
||||
return 0;
|
||||
}
|
31
Exercises/C9/for_loop_examples.c
Normal file
31
Exercises/C9/for_loop_examples.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Ddeclare variable and initialise to 1
|
||||
int i;
|
||||
|
||||
// Count up from 1 to 100 in steps of 1
|
||||
// Note the test could also be: i <= 100
|
||||
for ( i = 0 ; i < 101 ; i++ )
|
||||
{
|
||||
printf ("The value of i is %d\n",i);
|
||||
}
|
||||
|
||||
// Count down form 10 to zero
|
||||
// Note: we could also use the test: i != 0
|
||||
for ( i = 10 ; i >= 0 ; i-- )
|
||||
{
|
||||
printf ("The value of i is %d\n",i);
|
||||
}
|
||||
|
||||
// Count up from 1 to 100 in steps of 3
|
||||
// Note the test could also be: i <= 100
|
||||
// Increement could also be written as i+=3
|
||||
for ( i = 0 ; i < 101 ; i=i+1 )
|
||||
{
|
||||
printf ("The value of i is %d\n",i);
|
||||
}
|
||||
return 0;
|
||||
}
|
23
Exercises/C9/infinite_while_loop.c
Normal file
23
Exercises/C9/infinite_while_loop.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int age ; // Declare variable, no need to initialise this time
|
||||
// as the test condition is not based on its value
|
||||
|
||||
// This is always non-zero, the loop will never break
|
||||
while ( 1 )
|
||||
{
|
||||
// Code in {} executed if condition is true (non-zero)
|
||||
|
||||
printf ("\nPlease enter your age");
|
||||
scanf("%d", &age);
|
||||
printf ("You are %d years old\n", age);
|
||||
|
||||
// Code now goes back and repeats
|
||||
// as the conditions is always non-zero (true)
|
||||
}
|
||||
|
||||
return 0; // Exit code
|
||||
}
|
19
Exercises/C9/while_loop.c
Normal file
19
Exercises/C9/while_loop.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int age = 1; // Declare variable and initialise to 1
|
||||
|
||||
while ( age != 0) // Loop as long as age is not zero
|
||||
{
|
||||
// Code in {} executed if condition is true (non-zero)
|
||||
|
||||
printf ("\nPlease enter your age");
|
||||
scanf("%d", &age);
|
||||
printf ("You are %d years old\n", age);
|
||||
|
||||
// Code now goes back and repeats the test with the value of age just entered
|
||||
}
|
||||
return 0; // Exit code
|
||||
}
|
Reference in New Issue
Block a user