diff --git a/C9/ex2.c b/C9/ex2.c new file mode 100644 index 0000000..906166b --- /dev/null +++ b/C9/ex2.c @@ -0,0 +1,23 @@ +#include +#include + +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 +} diff --git a/C9/ex3.c b/C9/ex3.c new file mode 100644 index 0000000..7b8e564 --- /dev/null +++ b/C9/ex3.c @@ -0,0 +1,14 @@ +#include + +#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); + } +} diff --git a/C9/ex4.c b/C9/ex4.c new file mode 100644 index 0000000..f3a7ef3 --- /dev/null +++ b/C9/ex4.c @@ -0,0 +1,21 @@ +#include +#include + +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 +} diff --git a/C9/ex5.c b/C9/ex5.c new file mode 100644 index 0000000..0508542 --- /dev/null +++ b/C9/ex5.c @@ -0,0 +1,9 @@ +#include + +int main() { + for (int i = 1; i <= 15; i++) { + printf("%d\n", i); + } + + return 0; +} diff --git a/C9/ex6.c b/C9/ex6.c new file mode 100644 index 0000000..d72d131 --- /dev/null +++ b/C9/ex6.c @@ -0,0 +1,29 @@ +#include + +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; +} diff --git a/C9/ex7.c b/C9/ex7.c new file mode 100644 index 0000000..26db66a --- /dev/null +++ b/C9/ex7.c @@ -0,0 +1,29 @@ +#include + +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; +} diff --git a/C9/ex8.c b/C9/ex8.c new file mode 100644 index 0000000..a977093 --- /dev/null +++ b/C9/ex8.c @@ -0,0 +1,32 @@ +#include + +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; +}