├── 00-introduction-to-c.md ├── 01-datatypes.c ├── 02-variable.c ├── 03-format-specifiers.c ├── 04-comment-and-escape-sequence.c ├── 05-operators.c ├── 06-conditional-statements.c ├── 07-loops.c ├── 08-Ternary-Operator.c ├── 09-Nested-Loops.c ├── 10-Break-and-Continue.c ├── 11-Switch-Statement.c ├── 12-Scope-of-variables.c ├── 13-Variable-modifier.c ├── 14-Functions.c ├── 15-Recursion.c ├── 16-Pointers.c ├── 17-Arrays.c ├── README.md ├── TASKS ├── Namepair.c ├── Namepairsample.c ├── Namepairsample.exe ├── README.md ├── Task 001.c ├── Task 002.c ├── Task 003.c ├── Task 004.c ├── Task 005.c ├── Task 006.c ├── Task 007.c ├── Task 008.c ├── Task 009.c ├── Task 010.c ├── Task 011.c ├── Task 012.c ├── Task 013.c ├── Task 014.c ├── Task 015.c ├── Task 016.c ├── Task 017.c ├── Task 018.c ├── Task 019.c ├── Task-20-Functions.md ├── Task-21-Pointers-Quiz.md ├── Task-22.c ├── Task-23.c ├── Task-24.c ├── Task-25.c ├── Task-26.c ├── Task-27.c ├── Task-28.c ├── fruit_bowl.c ├── popsicles.c └── thats_odd_and_even.c ├── playground.c └── playground.exe /00-introduction-to-c.md: -------------------------------------------------------------------------------- 1 | # Introduction To C 2 | What is C? 3 | Who invented c? 4 | What year was c invented? 5 | Why are we learning to program in c? 6 | What's special about c? 7 | What is c used for? 8 | Why did we start with C? 9 | -------------------------------------------------------------------------------- /01-datatypes.c: -------------------------------------------------------------------------------- 1 | // What is datatype? 2 | /* 3 | A data type, in programming, is a classification 4 | that specifies which type of value a variable 5 | has and what type of mathematical, relational or 6 | logical operations can be applied to it without causing an error. 7 | */ 8 | 9 | /* 10 | Types 11 | 1. int 12 | Integers are whole numbers that can have both zero, 13 | positive and negative values but no decimal values. 14 | For example, 0, -5, 10 15 | 16 | We can use int for declaring an integer variable. 17 | You can declare multiple variables at once in C programming. 18 | 19 | 2. Float 20 | Floats are used to hold decimal numbers. For example 12.3456, 14.5. 21 | 22 | 3. Double 23 | Floats are used to hold decimal numbers. For example 12.3, 14.5. 24 | 25 | 4. char 26 | Keyword char is used for declaring character type variables. For example, a, b. 27 | 28 | - Constant 29 | Are used to declare variables, that the values can't be changed in the program. 30 | 31 | Others 32 | short and long 33 | signed and unsigned 34 | */ -------------------------------------------------------------------------------- /02-variable.c: -------------------------------------------------------------------------------- 1 | /* 2 | What is a variable in programming? 3 | In programming, a variable is a container (storage area) to hold data. 4 | 5 | To indicate the storage area, each variable should be given 6 | a unique name (identifier). Variable names are just the 7 | symbolic representation of a memory location. 8 | 9 | Rules for naming a variable 10 | - A variable name can only have letters (both uppercase 11 | and lowercase letters), digits and underscore. 12 | - The first letter of a variable should be either a letter or an underscore. 13 | - There is no rule on how long a variable name (identifier) can be. 14 | However, you may run into problems in some compilers if the 15 | variable name is longer than 31 characters. 16 | 17 | Note: You should always try to give meaningful names to 18 | variables. For example: firstName is a better variable name than fn. 19 | 20 | In C, variable names should be preceded by a datatype, 21 | to tell the compiler what type of data we storing in the variable. 22 | 23 | Syntax - 24 | datatype variableName = value; 25 | 26 | 27 | */ -------------------------------------------------------------------------------- /03-format-specifiers.c: -------------------------------------------------------------------------------- 1 | /* 2 | FORMAT SPECIFIER 3 | What are format specifiers and why do we use them? 4 | 5 | Format specifiers in C are used to take inputs and 6 | print the output of a type. The symbol we use in every 7 | format specifier is %. Format specifiers tell the compiler 8 | about the type of data that must be given as input and 9 | the type of data that must be printed on the screen. 10 | 11 | Types 12 | 1. Int - %d or %i 13 | 2. Float - %f 14 | 3. Double - %lf 15 | 4. Char - %c 16 | */ -------------------------------------------------------------------------------- /04-comment-and-escape-sequence.c: -------------------------------------------------------------------------------- 1 | /* 2 | What is comment in C language? 3 | Comments in C language are used to provide information 4 | about lines of code. It is widely used for documenting code. 5 | There are 2 types of comments in the C language; 6 | Single Line Comments and Multi-Line Comments. 7 | 8 | 9 | Escape Sequence 10 | A sequence of characters starting with backslash ( \ ) 11 | that performs a specified function, which is already 12 | defined by C. 13 | It is named so as it doesn’t represent itself as a 14 | character, when it is used inside a string. 15 | 16 | Some of the C Escape Sequence are listed below. 17 | ESCAPE SEQUENCE USES 18 | \n New Line 19 | \a Alarm or Beep 20 | \b Backspace 21 | \\ Backslash 22 | \r Carriage Return 23 | \” Double Quote 24 | \f Form Feed 25 | \xhh Hexadecimal Number 26 | \0 Null 27 | \nnn Octal Number 28 | \? Question Mark 29 | \’ Single Quote 30 | \t Tab (Horizontal) 31 | \v Vertical Tab 32 | */ -------------------------------------------------------------------------------- /05-operators.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | An operator is a symbol that operates on a value or a variable. 4 | For example: + is an operator to perform addition. 5 | C has a wide range of operators to perform various operations. 6 | 7 | C Arithmetic Operators 8 | An arithmetic operator performs mathematical operations such as 9 | addition, subtraction, multiplication, division etc on numerical 10 | values (constants and variables). 11 | 12 | Operator | Meaning of Operator 13 | + addition or unary plus 14 | - subtraction or unary minus 15 | * multiplication 16 | / division 17 | % remainder after division (modulo division) 18 | 19 | */ 20 | 21 | // Example 1: Arithmetic Operators 22 | // Working of arithmetic operators 23 | int main() 24 | { 25 | int a = 9, b = 4, c; 26 | 27 | c = a+b; 28 | printf("a+b = %d \n",c); 29 | c = a-b; 30 | printf("a-b = %d \n",c); 31 | c = a*b; 32 | printf("a*b = %d \n",c); 33 | c = a/b; 34 | printf("a/b = %d \n",c); 35 | c = a%b; 36 | printf("Remainder when a divided by b = %d \n",c); 37 | 38 | return 0; 39 | } 40 | 41 | /* 42 | C Increment and Decrement Operators 43 | C programming has two operators increment ++ and decrement -- 44 | to change the value of an operand (constant or variable) by 1. 45 | 46 | Increment ++ increases the value by 1 whereas decrement -- 47 | decreases the value by 1. These two operators are unary 48 | operators, meaning they only operate on a single operand. 49 | 50 | */ 51 | /* 52 | // Example 2: Increment and Decrement Operators 53 | // Working of increment and decrement operators 54 | int main() 55 | { 56 | int a = 10, b = 100; 57 | float c = 10.5, d = 100.5; 58 | 59 | printf("++a = %d \n", ++a); 60 | printf("--b = %d \n", --b); 61 | printf("++c = %f \n", ++c); 62 | printf("--d = %f \n", --d); 63 | 64 | return 0; 65 | } 66 | */ 67 | 68 | /* 69 | C Assignment Operators 70 | An assignment operator is used for assigning a value 71 | to a variable. The most common assignment operator is = 72 | 73 | Operator Example Same as 74 | = a = b a = b 75 | += a += b a = a+b 76 | -= a -= b a = a-b 77 | *= a *= b a = a*b 78 | /= a /= b a = a/b 79 | %= a %= b a = a%b 80 | 81 | */ 82 | /* 83 | Example 3: Assignment Operators 84 | // Working of assignment operators 85 | int main() 86 | { 87 | int a = 5, c; 88 | 89 | c = a; // c is 5 90 | printf("c = %d\n", c); 91 | c += a; // c is 10 92 | printf("c = %d\n", c); 93 | c -= a; // c is 5 94 | printf("c = %d\n", c); 95 | c *= a; // c is 25 96 | printf("c = %d\n", c); 97 | c /= a; // c is 5 98 | printf("c = %d\n", c); 99 | c %= a; // c = 0 100 | printf("c = %d\n", c); 101 | 102 | return 0; 103 | } 104 | */ 105 | 106 | /* 107 | C Relational Operators 108 | A relational operator checks the relationship between two operands. 109 | If the relation is true, it returns 1; if the relation is false, 110 | it returns value 0. 111 | 112 | Relational operators are used in decision making and loops. 113 | 114 | Operator Meaning of Operator Example 115 | == Equal to 5 == 3 is evaluated to 0 116 | > Greater than 5 > 3 is evaluated to 1 117 | < Less than 5 < 3 is evaluated to 0 118 | != Not equal to 5 != 3 is evaluated to 1 119 | >= Greater than or equal to 5 >= 3 is evaluated to 1 120 | <= Less than or equal to 5 <= 3 is evaluated to 0 121 | 122 | */ 123 | /* 124 | Example 4: Relational Operators 125 | // Working of relational operators 126 | int main() 127 | { 128 | int a = 5, b = 5, c = 10; 129 | 130 | printf("%d == %d is %d \n", a, b, a == b); 131 | printf("%d == %d is %d \n", a, c, a == c); 132 | printf("%d > %d is %d \n", a, b, a > b); 133 | printf("%d > %d is %d \n", a, c, a > c); 134 | printf("%d < %d is %d \n", a, b, a < b); 135 | printf("%d < %d is %d \n", a, c, a < c); 136 | printf("%d != %d is %d \n", a, b, a != b); 137 | printf("%d != %d is %d \n", a, c, a != c); 138 | printf("%d >= %d is %d \n", a, b, a >= b); 139 | printf("%d >= %d is %d \n", a, c, a >= c); 140 | printf("%d <= %d is %d \n", a, b, a <= b); 141 | printf("%d <= %d is %d \n", a, c, a <= c); 142 | 143 | return 0; 144 | } 145 | */ 146 | 147 | /* 148 | C Logical Operators 149 | An expression containing logical operator returns 150 | either 0 or 1 depending upon whether expression results 151 | true or false. Logical operators are commonly used in 152 | decision making in C programming. 153 | 154 | Operator Meaning 155 | && Logical AND. True only if all operands are true 156 | || Logical OR. True only if either one operand is true 157 | ! Logical NOT. True only if the operand is 0 158 | 159 | Example case; 160 | ALX SE Group - 50 male, 50 female 161 | Group Shell - male && uses EMACS 162 | Group Git - female uses EMAcs || female uses VI 163 | Group Sandbox - ! using VS Code 164 | */ 165 | /* 166 | // Example 5: Logical Operators 167 | // Working of logical operators 168 | 169 | int main() 170 | { 171 | int a = 5, b = 5, c = 10, result; 172 | 173 | result = (a == b) && (c > b); 174 | printf("(a == b) && (c > b) is %d \n", result); 175 | 176 | result = (a == b) && (c < b); 177 | printf("(a == b) && (c < b) is %d \n", result); 178 | 179 | result = (a == b) || (c < b); 180 | printf("(a == b) || (c < b) is %d \n", result); 181 | 182 | result = (a != b) || (c < b); 183 | printf("(a != b) || (c < b) is %d \n", result); 184 | 185 | result = !(a != b); 186 | printf("!(a != b) is %d \n", result); 187 | 188 | result = !(a == b); 189 | printf("!(a == b) is %d \n", result); 190 | 191 | return 0; 192 | } 193 | 194 | */ 195 | 196 | /* 197 | The sizeof operator 198 | The sizeof is a unary operator that returns the size 199 | of data (constants, variables, array, structure, etc). 200 | */ 201 | /* 202 | Example 6: sizeof Operator 203 | #include 204 | int main() 205 | { 206 | int a; 207 | float b; 208 | double c; 209 | char d; 210 | printf("Size of int=%lu bytes\n",sizeof(a)); 211 | printf("Size of float=%lu bytes\n",sizeof(b)); 212 | printf("Size of double=%lu bytes\n",sizeof(c)); 213 | printf("Size of char=%lu byte\n",sizeof(d)); 214 | 215 | return 0; 216 | } 217 | */ 218 | 219 | 220 | /* 221 | C Bitwise Operators 222 | During computation, mathematical operations like: addition, 223 | subtraction, multiplication, division, etc are converted to 224 | bit-level which makes processing faster and saves power. 225 | 226 | Bitwise operators are used in C programming to perform bit-level operations. 227 | 228 | Operators Meaning of operators 229 | & Bitwise AND 230 | | Bitwise OR 231 | ^ Bitwise exclusive OR 232 | ~ Bitwise complement 233 | << Shift left 234 | >> Shift right 235 | 236 | */ 237 | 238 | -------------------------------------------------------------------------------- /06-conditional-statements.c: -------------------------------------------------------------------------------- 1 | /* 2 | C if Statement 3 | The syntax of the if statement in C programming is: 4 | 5 | if (test expression) 6 | { 7 | // code 8 | } 9 | 10 | How if statement works? 11 | The if statement evaluates the test expression inside the parenthesis (). 12 | 13 | If the test expression is evaluated to true, statements inside the body of if are executed. 14 | If the test expression is evaluated to false, statements inside the body of if are not executed. 15 | 16 | Practice Program 17 | Check if a number is negative 18 | */ 19 | 20 | /* 21 | 22 | C if...else Statement 23 | The if statement may have an optional else block. The syntax of the if..else statement is: 24 | 25 | if (test expression) { 26 | // run code if test expression is true 27 | } 28 | else { 29 | // run code if test expression is false 30 | } 31 | 32 | How if...else statement works? 33 | If the test expression is evaluated to true, 34 | statements inside the body of if are executed. 35 | statements inside the body of else are skipped from execution. 36 | 37 | If the test expression is evaluated to false, 38 | statements inside the body of else are executed 39 | statements inside the body of if are skipped from execution. 40 | 41 | Practice Program 42 | Check whether a number is odd or even 43 | */ 44 | 45 | /* 46 | C if...else Ladder 47 | The if...else statement executes two different codes depending 48 | upon whether the test expression is true or false. Sometimes, 49 | a choice has to be made from more than 2 possibilities. 50 | 51 | The if...else ladder allows you to check between multiple 52 | test expressions and execute different statements. 53 | 54 | Syntax of if...else Ladder 55 | if (test expression1) { 56 | // statement(s) 57 | } 58 | else if(test expression2) { 59 | // statement(s) 60 | } 61 | else if (test expression3) { 62 | // statement(s) 63 | } 64 | . 65 | . 66 | else { 67 | // statement(s) 68 | } 69 | 70 | Practice Program 71 | Program to relate two integers using ==, > or < symbol 72 | 73 | */ -------------------------------------------------------------------------------- /07-loops.c: -------------------------------------------------------------------------------- 1 | /* 2 | In programming, a loop is used to repeat a 3 | block of code until the specified condition is met. 4 | 5 | C programming has three types of loops: 6 | 7 | for loop 8 | while loop 9 | do...while loop 10 | */ 11 | 12 | /* 13 | for Loop 14 | The syntax of the for loop is: 15 | 16 | for (initializationStatement; testExpression; updateStatement) 17 | { 18 | // statements inside the body of loop 19 | } 20 | How for loop works? 21 | - The initialization statement is executed only once. 22 | - Then, the test expression is evaluated. If the test 23 | expression is evaluated to false, the for loop is terminated. 24 | - However, if the test expression is evaluated to true, 25 | statements inside the body of the for loop are executed, and 26 | the update expression is updated. 27 | - Again the test expression is evaluated. 28 | This process goes on until the test expression is false. 29 | When the test expression is false, the loop terminates. 30 | 31 | Practice Program 32 | Print Numbers 1 - 10 33 | Calculate the sum 34 | */ 35 | 36 | /* 37 | C while loop 38 | The syntax of the while loop is: 39 | 40 | while (testExpression) { 41 | // the body of the loop 42 | } 43 | How while loop works? 44 | The while loop evaluates the testExpression inside the parentheses (). 45 | If testExpression is true, statements inside the body of while 46 | loop are executed. Then, testExpression is evaluated again. 47 | The process goes on until testExpression is evaluated to false. 48 | If testExpression is false, the loop terminates (ends). 49 | 50 | Practice Program 51 | Print Numbers 1 - 10 52 | */ 53 | 54 | /* 55 | do...while loop 56 | The do..while loop is similar to the while loop with one important 57 | difference. The body of do...while loop is executed at least once. 58 | Only then, the test expression is evaluated. 59 | 60 | The syntax of the do...while loop is: 61 | 62 | do { 63 | // the body of the loop 64 | } 65 | while (testExpression); 66 | 67 | How do...while loop works? 68 | - The body of do...while loop is executed once. Only then, the testExpression is evaluated. 69 | - If testExpression is true, the body of the loop is executed again and testExpression is evaluated once more. 70 | - This process goes on until testExpression becomes false. 71 | - If testExpression is false, the loop ends. 72 | 73 | Practice Program 74 | Ask for correct input continously 75 | */ -------------------------------------------------------------------------------- /08-Ternary-Operator.c: -------------------------------------------------------------------------------- 1 | /* 2 | Ternary Operator 3 | We use the ternary operator for decision making in place of 4 | longer if and else conditional statements. 5 | 6 | The ternary operator take three arguments: 7 | 8 | The first is a comparison argument 9 | The second is the result upon a true comparison 10 | The third is the result upon a false comparison 11 | 12 | The ternary operator could be said to be a shorthand 13 | way of writing an if-else statement. 14 | 15 | Syntax 16 | condition ? value_if_true : value_if_false 17 | 18 | Ternary operator can also be nested 19 | */ -------------------------------------------------------------------------------- /09-Nested-Loops.c: -------------------------------------------------------------------------------- 1 | /* 2 | Nested Loops 3 | A nested loop means a loop statement inside another 4 | loop statement. That is why nested loops are also 5 | called “loop inside loops“. We can define as many loops 6 | as we want inside of our loops. 7 | 8 | 9 | Syntax: 10 | 11 | for ( initialization; condition; increment ) { 12 | 13 | for ( initialization; condition; increment ) { 14 | 15 | // statement of inside loop 16 | } 17 | 18 | // statement of outer loop 19 | } 20 | 21 | Syntax: 22 | 23 | while(condition) { 24 | 25 | while(condition) { 26 | 27 | // statement of inside loop 28 | } 29 | 30 | // statement of outer loop 31 | } 32 | 33 | Practice Problem 34 | FizzBuzz 35 | */ 36 | 37 | /* 38 | int n = 6;// variable declaration 39 | //printf("Enter the value of n :"); 40 | // Displaying the n tables. 41 | for(int i=1;i<=n;i++) // outer loop 42 | { 43 | for(int j=1;j<=10;j++) // inner loop 44 | { 45 | printf("%d\t",(i*j)); // printing the value. 46 | } 47 | printf("\n"); 48 | } 49 | */ -------------------------------------------------------------------------------- /10-Break-and-Continue.c: -------------------------------------------------------------------------------- 1 | /* 2 | Break 3 | The break statement is used to exit out of a loop if a 4 | particular condition is met. 5 | 6 | Its syntax is: 7 | break; 8 | 9 | The break statement is almost always used 10 | with if...else statement inside the loop. 11 | 12 | Example 13 | int i; 14 | 15 | for (i = 0; i < 10; i++) { 16 | if (i == 4) { 17 | break; 18 | } 19 | printf("%d\n", i); 20 | } 21 | 22 | The above code terminates when i is 4. 23 | 24 | */ 25 | 26 | /* 27 | Continue 28 | The continue statement breaks one iteration 29 | (in the loop), if a specified condition occurs, 30 | and continues with the next iteration in the loop. 31 | 32 | Syntax: 33 | continue; 34 | 35 | This example skips the value of 4: 36 | 37 | Example 38 | int i; 39 | 40 | for (i = 0; i < 10; i++) { 41 | if (i == 4) { 42 | continue; 43 | } 44 | printf("%d\n", i); 45 | } 46 | 47 | */ -------------------------------------------------------------------------------- /11-Switch-Statement.c: -------------------------------------------------------------------------------- 1 | /* 2 | Switch Statement 3 | The switch statement allows us to execute one code 4 | block among many alternatives. 5 | 6 | You can do the same thing with the if...else..if ladder. 7 | However, the syntax of the switch statement is much easier 8 | to read and write. 9 | 10 | Syntax of switch...case 11 | 12 | switch (expression) 13 | ​{ 14 | case constant1: 15 | // statements 16 | break; 17 | 18 | case constant2: 19 | // statements 20 | break; 21 | . 22 | . 23 | . 24 | default: 25 | // default statements 26 | } 27 | 28 | Notes: 29 | 30 | If we do not use the break statement, all statements 31 | after the matching label are also executed. 32 | The default clause inside the switch statement is optional. 33 | 34 | Practice Program 35 | Create a calculator 36 | */ -------------------------------------------------------------------------------- /12-Scope-of-variables.c: -------------------------------------------------------------------------------- 1 | /* 2 | In C programming, the scope of a variable refers to the portion of 3 | the program where the variable is accessible and can be used. 4 | There are two types of variable scopes in C: 5 | 6 | 1. Global scope: Variables declared outside of all functions, at 7 | the top level of a source code file, are known as global variables. 8 | These variables are accessible throughout the entire program and 9 | can be used in any function. 10 | Example: 11 | #include 12 | 13 | int a = 10; // Global variable 14 | 15 | int main() { 16 | int b = 20; // Local variable 17 | printf("Value of a: %d\n", a); 18 | printf("Value of b: %d\n", b); 19 | return 0; 20 | } 21 | In this example, the variable a has global scope, which means it can 22 | be accessed from any function within the program. 23 | 24 | 25 | 2. Local scope: Variables declared within a function or a block are 26 | known as local variables. They are only accessible within the 27 | function or block in which they are declared and cannot be accessed 28 | outside of it. 29 | Example: 30 | #include 31 | 32 | int main() { 33 | int outBox = 10; // Local variable 34 | 35 | if (outBox == 10) 36 | { 37 | int roomBox = 20; // Local variable within the if block 38 | printf("Value of outBox: %d\n", outBox); 39 | printf("Value of roomBox: %d\n", roomBox); 40 | } 41 | // printf("Value of b: %d\n", roomBox); This line will cause an error, because the scope of b is limited to the if block 42 | return 0; 43 | } 44 | 45 | In this example, the variable roomBox has local scope, which means it can 46 | only be accessed within the if block where it is declared. If you 47 | try to access roomBox outside of the if block, the program will cause 48 | an error. 49 | 50 | It is important to understand the scope of a variable as it can 51 | affect how you can use the variable within your program and can 52 | also impact the behavior of your program. 53 | 54 | */ -------------------------------------------------------------------------------- /13-Variable-modifier.c: -------------------------------------------------------------------------------- 1 | /* 2 | In C programming, a variable modifier is a keyword used to specify 3 | the type of storage a variable should have. The following are the 4 | most commonly used variable modifiers in C: 5 | 6 | auto: By default, a variable is auto and can be used without 7 | specifying the keyword. 8 | Example: 9 | auto int a; 10 | 11 | register: This modifier suggests that the compiler should store the 12 | variable in a register, instead of memory. The compiler will decide 13 | whether or not to follow this suggestion. 14 | Example: 15 | register int a; 16 | 17 | static: This modifier tells the compiler to store the variable in 18 | static memory, instead of automatic memory. This means that the value 19 | of the variable will persist between function calls. 20 | Example: 21 | static int a; 22 | 23 | extern: This modifier declares a variable that is defined in another 24 | source file. It is used to access variables in another source file. 25 | Example: 26 | extern int a; 27 | 28 | volatile: This modifier is used to inform the compiler that the 29 | value of the variable may change in ways not specified by the code. 30 | This is useful for accessing hardware registers and memory-mapped I/O. 31 | Example: 32 | volatile int a; 33 | */ -------------------------------------------------------------------------------- /14-Functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | Functions 3 | A function is a block of code that performs a specific task. 4 | 5 | Suppose, you need to create a program to open a folder and 6 | delete files inside the folder, you can create three functions; 7 | 1. A function to create the folder 8 | 2. A function to open the folder 9 | 3. A function to delete the files 10 | 11 | With this example, we can say that the collection of 12 | functions creates a program. 13 | 14 | Why should we use functions 15 | 1. With functions, we won't have to write same code or logic 16 | over and over again in a program. 17 | 2. We can easily track the program, when we use functions. 18 | 3. We can call functions multiple times in a program, and so 19 | it is reuseable. 20 | 21 | Types of Function 22 | 1. Library Function - These are the built in functions in the 23 | header files, e.g printf, scanf. 24 | 25 | 2. User-defined Function - These are the functions created by the 26 | user. 27 | 28 | 29 | Creating a Function 30 | There are three things to consider when creating c function; 31 | 32 | 1. Function declaration - A function must be declared globally 33 | in a c program to tell the compiler about the function name, 34 | function parameters, and return type. 35 | syntax 36 | returntype functionName(arg1, arg2); 37 | 38 | 2. Function call - Function can be called from anywhere in 39 | the program. The parameter list must not differ in function 40 | calling and function declaration. We must pass the same 41 | number of arguments as it is declared in the function 42 | declaration. 43 | syntax 44 | functionName(arg1, arg2); 45 | 46 | 3. Function definition - It contains the actual statements 47 | which are to be executed. It is the most important aspect 48 | to which the control comes when the function is called. 49 | Here, we must notice that only one value can be returned 50 | from the function. 51 | Syntax 52 | returnType functionName(arg1, arg2) 53 | { 54 | body of the function; 55 | } 56 | 57 | Passing arguments to a function 58 | In programming, argument refers to the variable passed to 59 | the function. 60 | In the above examples; arg1 and arg2 are the arguments passed 61 | into the function. 62 | 63 | NOTE: Type of arguments and the number of arguments passed 64 | during a function call, must match that of the function 65 | definition. 66 | Example, if arg1 is of int type, then during the call, the 67 | argument to replace arg1 must be of int type. 68 | 69 | Call by Value and Call by Reference 70 | 1. Call by Value: 71 | In this method, a copy of the argument value is passed to the 72 | function, and any changes made to the argument within the function 73 | do not affect the original value outside the function. 74 | Example: 75 | #include 76 | 77 | void swap(int x, int y) { 78 | int temp; 79 | temp = x; 80 | x = y; 81 | y = temp; 82 | } 83 | 84 | int main() { 85 | int a = 100, b = 200; 86 | printf("Before swap: a = %d b = %d\n", a, b); 87 | swap(a, b); 88 | printf("After swap: a = %d b = %d\n", a, b); 89 | return 0; 90 | } 91 | 92 | Call by reference: 93 | In this method, a reference to the argument is passed to the 94 | function, and any changes made to the argument within the function 95 | affect the original value outside the function. 96 | Example: 97 | #include 98 | 99 | void swap(int *x, int *y) { 100 | int temp; 101 | temp = *x; 102 | *x = *y; 103 | *y = temp; 104 | } 105 | 106 | int main() { 107 | int a = 100, b = 200; 108 | printf("Before swap: a = %d b = %d\n", a, b); 109 | swap(&a, &b); 110 | printf("After swap: a = %d b = %d\n", a, b); 111 | return 0; 112 | } 113 | 114 | */ 115 | 116 | /* 117 | 118 | Applications of Functions 119 | Calculator: A calculator is an application that performs mathematical 120 | operations. Functions can be used to perform specific operations, such 121 | as addition, subtraction, multiplication, and division. 122 | 123 | Traffic Control System: A traffic control system can use functions to 124 | manage the flow of vehicles at intersections. For example, a function can 125 | be used to change the traffic light from red to green, or to display a 126 | warning sign when a pedestrian is crossing the road. 127 | 128 | Bank Management System: A bank management system can use functions to 129 | manage the operations of a bank, such as account creation, deposit, 130 | withdrawal, balance inquiry, and transaction history. 131 | 132 | Weather Forecast System: A weather forecast system can use functions to 133 | collect data from weather stations, process the data, and generate weather 134 | reports and forecasts. 135 | 136 | Medical Diagnosis System: A medical diagnosis system can use functions to 137 | collect patient information, perform tests, analyze results, and make 138 | recommendations for treatment. 139 | */ 140 | 141 | -------------------------------------------------------------------------------- /15-Recursion.c: -------------------------------------------------------------------------------- 1 | /* 2 | A recursive function is a function that calls itself until a certain 3 | condition is met. The condition is called the base case, and when it's 4 | met, the function stops calling itself and returns a result. 5 | 6 | Recursive functions are functions that call themselves. They are a way of 7 | solving problems by breaking them down into smaller problems and solving 8 | each small problem recursively until a base case is reached, at which 9 | point the solutions to the smaller problems can be combined to solve the 10 | original problem. Recursive functions can be used to solve many types of 11 | problems, including mathematical problems, data structure problems, and 12 | string manipulation problems, among others. 13 | 14 | For example, consider a problem of calculating the factorial of a number. 15 | Factorial of a number n is defined as n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1. 16 | 17 | Here's an implementation of the factorial function using recursion in C: 18 | #include 19 | 20 | int factorial(int n) { 21 | if (n == 0) return 1; // base case 22 | return n * factorial(n-1); // recursive case 23 | } 24 | 25 | int main() { 26 | int n; 27 | printf("Enter a number to find its factorial: "); 28 | scanf("%d", &n); 29 | printf("The factorial of %d is %d\n", n, factorial(n)); 30 | return 0; 31 | } 32 | 33 | In this example, the function factorial calls itself until the base 34 | case n == 0 is met. The base case returns 1, and for all other cases, 35 | the function returns n * factorial(n-1), where n-1 is the argument for 36 | the next call to factorial. 37 | */ -------------------------------------------------------------------------------- /16-Pointers.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidinmichael/c-programming-tutorial/385f299811ed74b3ed32e00370842e8c63751350/16-Pointers.c -------------------------------------------------------------------------------- /17-Arrays.c: -------------------------------------------------------------------------------- 1 | /* 2 | Definition of Array 3 | An array is a data structure containing data values of the same type. 4 | Note: A data structure is a format for organizing, storing and 5 | retrieving data. 6 | 7 | Understanding the definition of array 8 | arr = 5, 6, 6, 7 // This is correct, it holds values of same type 9 | arr = 'a', 'b', 'c' // This is correct, it holds values of same type 10 | arr = 5, 't', 4.5, 55 // This is wrong, it holds values of different 11 | data types. 12 | 13 | Introduction to one dimensional array 14 | One dimensional array is the simplest form an array, example; 15 | arr = 5, 6, 7, 8, 9 16 | 17 | How to declare and define one dimensional array 18 | syntax; 19 | data_type name_0f_array[no. of elements]; 20 | Example, to declare an array to hold 5 integers; 21 | int scores[5]; 22 | 23 | NOTE: The length of an array can be specified by any positive integer 24 | constant expression, examples; 25 | int arr[5]; 26 | int arr[4+2]; 27 | int arr[4*5]; 28 | int arr[8-2]; 29 | int arr[20/4]; 30 | The length of an array should not result to a float or double. 31 | 32 | How to access the array element 33 | To access the elements in an array, write; 34 | array_name[index] // Index refers to the position of the array, and 35 | it starts from 0, example; 36 | int scores[5] = {4, 5, 6, 7}; // The index of 4 is 0, and the index of 37 | 7 is 3. 38 | 39 | To access the number 5, 40 | scores[1] 41 | 42 | How to initialize one dimensional array 43 | Method 1: 44 | int arr[5] = {3, 4, 5, 6, 7}; // Here, the length is specified 45 | Method 2: 46 | int arr[] = {3, 4, 5, 6, 7}; // Here, the length is not specified, and 47 | so, it can store any length of values. 48 | 49 | NOTE: Do not enter values and exceed the limit specified 50 | 51 | Designated Initializers 52 | This is placing numbers at a designated position in the array. 53 | Example; 54 | int numbers[10] = {[0] = 1, [4] = 5, [7] = 8}; // Here, we have an 55 | array of size 10. The elements in the square brackets is the designator, 56 | which tells what position to assign the values, after doing this, 57 | the rest of the position is filled with zero. 58 | This is a great way to initialize an array elements. 59 | 60 | */ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Programming 2 | This is a full tutorial on C programming intended to teach the basics of c. 3 | It contains lessons on every topics, with practical codes to explain each concepts. 4 | 5 | ## Table of Content 6 | 00 - Introduction to C 7 | 01 - Datatypes 8 | 02 - Variables 9 | 03 - Format Specifiers 10 | 04 - Comments and Escape Sequence 11 | 05 - Operators in C 12 | 06 - Conditional Statements 13 | 07 - Loops in C 14 | 08 - Ternary Operator 15 | 09 - Nested Loops 16 | 10 - Break and Continue 17 | 11 - Switch Statement 18 | 12 - Scope of Variable 19 | 13 - Variable Modifier 20 | 14 - Functions 21 | 15 - Recursions 22 | 16 - Pointers 23 | 17 - Arrays -------------------------------------------------------------------------------- /TASKS/Namepair.c: -------------------------------------------------------------------------------- 1 | /* 2 | A program to group names of persons in 3s. 3 | */ -------------------------------------------------------------------------------- /TASKS/Namepairsample.c: -------------------------------------------------------------------------------- 1 | /* 2 | Start 3 | 1. Declare and Initialize an array of strings to hold names 4 | 2. Get the count of names in the array 5 | 3. Shuffle the names in the array 6 | 4. For each iteration of index i from 0 to name count divided by 3 7 | 5. Print the names at index i, i + 1, i + 2 8 | End 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | #define N 80 15 | #define groupSize 3 16 | 17 | const char *names[N] = { 18 | "Dukeson Ehigboria", "Bayere Samuel Tosin", "Francis Morkeh Mensah", 19 | "MICHAEL OMOTOSHO", "Justice Chigbo Obasi", "GABRIEL Rufai", "Oisereme Abulu", 20 | "Brigid Wambua", "Olajide Safiyyah Mojirayo", "OMIDIJI Oluwaseyi", 21 | "Nwoko Onyinye Favour", "Philip Nyelwa Kweba", "Victor Adeyemo", "Paschal Chinonso", 22 | "Fapetu Ayodele Abayomi", "HIRWA Jr", "Abdulhamid Sanusi", "Olayiwola Solomon", 23 | "Eberechi", "Joseph Akpan Nathaniel", "Malumbe Musowe", "Jamiu Musa", "Chinaza Ukwe", 24 | "Didier Shema Gatete", "Taiwo Ogunga", "Blessed", "Amarachi Anaesiuba", "Hanna Menetie Ojole", 25 | "Egbuta Godslove Ugochukwu", "Joshua Monday", "Blessing Eugene Chukwunwike", 26 | "DR_Mkelvo (Mwiyeria Kelvin)", "JOSHUA MBISE", "Sanni Oshioke Omoba", "Sunday Udhawuve", 27 | "Thomas Oselu", "Thomas Bulus", "Stacy Gakiria", "Christie Dike", "Owolabi Pius", 28 | "Mary Nzembi Mutuku", "Aghara Christiana", "Adam Abah", "Folakemi Oderanti", 29 | "Grace Titilayo David", "Eric Ogedegbe", "Ramla Mahad Diriye", "Igwegbe George Ifesinachi", 30 | "Chinemerem Chukwukere", "Abdulazeez Ibrahim", "Bright Okon", "OLUWAFEMI BUSARI", 31 | "Titilayo Oluwakemi Olojede", "Oluwadamilola Ogunbayo", "Michael Okpako", "Functional Ogunbode", 32 | "Tochi Onwukwe", "Nkanga Edo Monday", "Agunloye Olaniyi Nicolas", "Fasil Seifu Besir", 33 | "Mary-Queen Uchechukwu", "Uwemedimo Ekpewoh", "Joy Kuapa", "Ogunleye Sanmi", "Didier Shema Gatete", 34 | "Adedamola Coal", "OLOLADE Olakunle Olalekan", "Wisdom Osatemple", "Adekilekun Abdullahi", 35 | "Faith Obi", "Ibrahim Bolajoko Ibrahim", "Lawal Muhammadbashir", "Malumbe Musowe", 36 | "Aina Adewale Ibukunoluwa", "Fadilat Abubakar", "Olayinka Jumai Kolupo" 37 | }; 38 | 39 | void shuffle(const char *names[N], int n) 40 | { 41 | for(int i = n - 1; i >= 0; i--) 42 | { 43 | int j = rand() % (i + 1); 44 | const char *temp = names[i]; 45 | names[i] = names[j]; 46 | names[j] = temp; 47 | } 48 | } 49 | 50 | int main() { 51 | srand(time(NULL)); 52 | shuffle(names, N); 53 | 54 | for(int i = 0; i < N; i += groupSize) 55 | { 56 | printf("Group %d:\n", (i / groupSize) + 1); 57 | for(int j = 0; j < groupSize && i + j < N; j++) 58 | { 59 | printf("\t%s\n", names[i + j]); 60 | } 61 | printf("\n"); 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /TASKS/Namepairsample.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidinmichael/c-programming-tutorial/385f299811ed74b3ed32e00370842e8c63751350/TASKS/Namepairsample.exe -------------------------------------------------------------------------------- /TASKS/README.md: -------------------------------------------------------------------------------- 1 | this directory contains tasks on each of the lessons of c programming, attempt each of them before asking or checking the solutions. 2 | -------------------------------------------------------------------------------- /TASKS/Task 001.c: -------------------------------------------------------------------------------- 1 | /* Write your program after the comment 2 | Write a program that prints the alphabet in lowercase, followed by a new line. 3 | 4 | Print all the letters except q and e 5 | 6 | use printf 7 | */ 8 | -------------------------------------------------------------------------------- /TASKS/Task 002.c: -------------------------------------------------------------------------------- 1 | /* Write your program after the comment 2 | Write a program that asks and reads the score of a user 3 | If the score is less than 80, display to the user that they 4 | are not elgible to be enrolled. 5 | If the score is greater than or equal 80, they can be enrolled. 6 | */ -------------------------------------------------------------------------------- /TASKS/Task 003.c: -------------------------------------------------------------------------------- 1 | /* Write your program after the comment 2 | Write a program that reads input from user (Number) 3 | If the number is Odd, display 'userInput is Odd' 4 | If the number is even, display 'userInput is even' 5 | If the number is zero, display 'This is zero' 6 | 7 | NOTE: 'userInput' should be the number entered by the user. 8 | 9 | */ -------------------------------------------------------------------------------- /TASKS/Task 004.c: -------------------------------------------------------------------------------- 1 | /* Write your program after the comment 2 | Write a program that asks and reads the following input from a use; 3 | Your name 4 | Your age 5 | and then displays; Your name is 'name' and you are 'age' years old. 6 | 7 | Example; 8 | what is your name: David 9 | How old are you?: 65 10 | 11 | Output: 12 | Your name is David and you are 65 years old. 13 | 14 | */ -------------------------------------------------------------------------------- /TASKS/Task 005.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program in C to find the square of any number 3 | using the function. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task 006.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program in C to check a given number is even 3 | or odd using the function. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task 007.c: -------------------------------------------------------------------------------- 1 | /* 2 | write your solution after the comment 3 | Write a function that prints the alphabet, in uppercase, 4 | followed by a new line. 5 | */ -------------------------------------------------------------------------------- /TASKS/Task 008.c: -------------------------------------------------------------------------------- 1 | /*write your solution after the comment 2 | Write a function that prints the alphabet, in lowercase 3 | followed by a new line. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task 009.c: -------------------------------------------------------------------------------- 1 | /*write your solution after the comment 2 | Write a function that checks for lowercase character. 3 | That is, when passed in an arguments, it checks if the 4 | argument is lowercase or uppercase. 5 | */ -------------------------------------------------------------------------------- /TASKS/Task 010.c: -------------------------------------------------------------------------------- 1 | /*write your solution after the comment 2 | Write a function that computes the absolute 3 | value of an integer. 4 | Understand what an absolute value is, before attempting it. 5 | */ -------------------------------------------------------------------------------- /TASKS/Task 011.c: -------------------------------------------------------------------------------- 1 | /*write your solution after the comment 2 | Write a function that prints the 9 times table, 3 | starting with 0. 4 | Output should look like this; 5 | 9 x 0 = 0 6 | 9 x 1 = 9 7 | 9 x 2 = 18 8 | ... and so on till 12 9 | */ -------------------------------------------------------------------------------- /TASKS/Task 012.c: -------------------------------------------------------------------------------- 1 | /*Write your solution after the comment 2 | Write a function that adds two integers and prints 3 | the result. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task 013.c: -------------------------------------------------------------------------------- 1 | /*Write your solution after the comment 2 | Write a function that prints all natural numbers from n to 100, 3 | followed by a new line. 4 | That is n is your argument, it should print from any number 5 | passed as argument till 100. 6 | */ -------------------------------------------------------------------------------- /TASKS/Task 014.c: -------------------------------------------------------------------------------- 1 | /*Write your solution after the comment 2 | Write a function that prints 10 times the numbers, from 0 to 14, 3 | followed by a new line. 4 | Your output should look like this; 5 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 6 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 7 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 8 | ...up to 10 times 9 | */ -------------------------------------------------------------------------------- /TASKS/Task 015.c: -------------------------------------------------------------------------------- 1 | /*Write your solution after the comment 2 | Fizz-Buzz - Write a program that prints the numbers 3 | from 1 to 100, followed by a new line. But for multiples 4 | of three print Fizz instead of the number and for the 5 | multiples of five print Buzz. For numbers which are 6 | multiples of both three and five print FizzBuzz. 7 | Your output should like this; 8 | 1 2 fizz 4 buzz fizz 7 8 fizz... and so on. 9 | */ -------------------------------------------------------------------------------- /TASKS/Task 016.c: -------------------------------------------------------------------------------- 1 | /*Write your solution after the comment 2 | Write a recursive function that takes an integer as 3 | an argument and returns the product of all integers 4 | from 1 to that number. 5 | 6 | Example: 7 | functionName(6) 8 | 9 | Output: 10 | 720 // That is 1x2x3x4x5x6 = 720 11 | */ -------------------------------------------------------------------------------- /TASKS/Task 017.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a recursive function that takes an integer as 3 | an argument and returns the sum of all integers from 4 | 1 to that number. 5 | 6 | Example 7 | functionName(6); 8 | 9 | Output: 10 | 21 // That is; 1+2+3+4+5+6 = 21 11 | */ -------------------------------------------------------------------------------- /TASKS/Task 018.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a function that returns the value of x raised to the power of y. 3 | 4 | functionName(int x, int y); 5 | */ -------------------------------------------------------------------------------- /TASKS/Task 019.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a function to convert temperature from Celsius to 3 | Fahrenheit and vice versa. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task-20-Functions.md: -------------------------------------------------------------------------------- 1 | ## Tasks On Functions 2 | 1. Write a function to find the sum of two numbers 3 | 2. Write a function to find the largest of two numbers 4 | 3. Write a function to find the factorial of a number 5 | 4. Write a function to check if a number is even or odd 6 | 5. Write a function to find the GCD (greatest common divisor) of two numbers 7 | 6. Write a function to find the LCM (least common multiple) of two numbers 8 | 7. Write a function to find the area of a circle 9 | 8. Write a function to find the volume of a sphere 10 | 9. Write a function to find the Fibonacci series of a given number 11 | 10. Write a function to convert temperature from Celsius to Fahrenheit and vice versa. 12 | 11. Write a program that implements different mathematical functions such as finding the factorial of a number, calculating the power of a number, and finding the square root of a number. 13 | 12. Write a program that implements functions to perform string operations, such as concatenating two strings, finding the length of a string, and reversing a string. 14 | 13. Write a program that implements functions to perform operations on arrays, such as finding the largest and smallest elements in an array, sorting an array in ascending or descending order, and finding the average of all elements in an array. 15 | 14. Write a program that implements functions to perform operations on structures, such as finding the largest and smallest elements in a structure, sorting a structure in ascending or descending order, and finding the average of all elements in a structure. 16 | 15. Create a program to calculate the factorial of a number. 17 | 16. Write a program to find the largest and smallest number in an array. 18 | 17. Create a program to sort an array of numbers in ascending or descending order. 19 | 18. Write a program to reverse a string. 20 | 19. Create a program to check if a number is prime or not. 21 | 20. Write a program to find the sum of all elements in an array. 22 | 21. Create a program to find the average of a set of numbers. 23 | 22. Write a program to find the frequency of each character in a string. 24 | 23. Create a program to convert a decimal number to binary, octal, or hexadecimal. 25 | 24. Write a program to check if a string is a palindrome. 26 | 25. Write a program to implement the QuickSort algorithm. 27 | 26. Create a program to implement the MergeSort algorithm. 28 | 27. Write a program to find the Longest Common Subsequence (LCS) of two strings. 29 | 28. Create a program to find the shortest path in a graph using Dijkstra's algorithm. 30 | 29. Write a program to implement the Binary Search algorithm. 31 | 30. Create a program to find the minimum number of coins needed to make a certain amount of change. 32 | 31. Write a program to implement the Knapsack problem. 33 | 32. Create a program to implement the Prim's algorithm for finding the minimum spanning tree in a graph. 34 | 33. Write a program to implement the Tower of Hanoi puzzle. 35 | 34. Create a program to implement the Depth-First Search (DFS) algorithm for graph traversal. 36 | -------------------------------------------------------------------------------- /TASKS/Task-21-Pointers-Quiz.md: -------------------------------------------------------------------------------- 1 | What is the purpose of pointers in C programming languages? 2 | 3 | What is the difference between a pointer and a regular variable in C programming languages? 4 | 5 | How do you declare a pointer in C programming languages? 6 | 7 | How do you access the value stored at the memory address pointed by a pointer in C programming languages? 8 | 9 | What is a null pointer in C programming languages? 10 | 11 | How do you declare pointers to pointers in C programming languages? 12 | 13 | What is the relationship between pointers and arrays in C programming languages? 14 | 15 | What is dynamic memory allocation in C programming languages and how is it achieved using pointers? 16 | 17 | How do you deallocate dynamically allocated memory in C programming languages? -------------------------------------------------------------------------------- /TASKS/Task-22.c: -------------------------------------------------------------------------------- 1 | /* 2 | Create a program that declares a pointer to an integer, assigns an 3 | address to it, and outputs the value stored at the memory address 4 | pointed by the pointer. 5 | */ -------------------------------------------------------------------------------- /TASKS/Task-23.c: -------------------------------------------------------------------------------- 1 | /* 2 | Create a program that uses pointers to find the maximum value stored 3 | in an array. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task-24.c: -------------------------------------------------------------------------------- 1 | /* 2 | Create a program that demonstrates the use of pointers to pass 3 | information between functions. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task-25.c: -------------------------------------------------------------------------------- 1 | /* 2 | Declare two integer variables and swap their values using a pointer. 3 | */ -------------------------------------------------------------------------------- /TASKS/Task-26.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes an array of scores, and 3 | then outputs the array in reverse order. 4 | */ -------------------------------------------------------------------------------- /TASKS/Task-27.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes an array of scores and outputs the 3 | highest score and the lowest score 4 | */ -------------------------------------------------------------------------------- /TASKS/Task-28.c: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to take input of how many students, 3 | calculate the average marks of the class by using an array to store 4 | the marks of each student and then using a loop to calculate the 5 | sum and average of all the marks. 6 | */ -------------------------------------------------------------------------------- /TASKS/fruit_bowl.c: -------------------------------------------------------------------------------- 1 | /**Write your program after the comment 2 | *Fruit Bowl - You have a bowl on your counter with an even number of pieces of fruit in it. Half of them are bananas, and the other half are apples. You need 3 apples to make a pie. 3 | Task 4 | Your task is to evaluate the total number of pies that you can make with the apples that are in your bowl given to total amount of fruit in the bowl. 5 | Input Format 6 | An integer that represents the total amount of fruit in the bowl. 7 | Output Format 8 | An integer representing the total number of whole apple pies that you can make. 9 | Sample Input 10 | 26 11 | Sample Output 12 | 4 13 | 14 | * */ 15 | -------------------------------------------------------------------------------- /TASKS/popsicles.c: -------------------------------------------------------------------------------- 1 | /** Write your program after the comment 2 | *Popsicles - You have a box of popsicles and you want to give them all away to a group of brothers and sisters. If you have enough left in the box to give them each an even amount you should go for it! If not, they will fight over them, and you should eat them yourself later. 3 | Task 4 | Given the number of siblings that you are giving popsicles to, determine if you can give them each an even amount or if you shouldn't mention the popsicles at all. 5 | Input Format 6 | Two integer values, the first one represents the number of siblings, and the second one represents the number of popsicles that you have left in the box. 7 | Output Format 8 | A string that says 'give away' if you are giving them away, or 'eat them yourself' if you will be eating them yourself. 9 | Sample Input 10 | 3 9 11 | Sample Output 12 | give away 13 | 14 | * */ 15 | -------------------------------------------------------------------------------- /TASKS/thats_odd_and_even.c: -------------------------------------------------------------------------------- 1 | /** Write your program after the comment 2 | *Odd and Even - You want to take a list of numbers and find the sum of all of the even numbers in the list. Ignore any odd numbers. 3 | Task: 4 | Find the sum of all even integers and sum of odd integers in a list of numbers. 5 | Input Format: 6 | The first input denotes the length of the list (N). The next N lines contain the list elements as integers. 7 | 8 | * */ 9 | -------------------------------------------------------------------------------- /playground.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void deposit(float *balance, float amount) { 4 | *balance += amount; 5 | printf("Deposit successful! Your new balance is %.2f\n", *balance); 6 | } 7 | 8 | void withdrawal(float *balance, float amount) { 9 | if (*balance < amount) { 10 | printf("Insufficient balance!\n"); 11 | return; 12 | } 13 | *balance -= amount; 14 | printf("Withdrawal successful! Your new balance is %.2f\n", *balance); 15 | } 16 | 17 | void check_balance(float balance) { 18 | printf("Your current balance is %.2f\n", balance); 19 | } 20 | 21 | int main() { 22 | float balance = 1000.0; 23 | deposit(&balance, 500.0); 24 | check_balance(balance); 25 | withdrawal(&balance, 800.0); 26 | check_balance(balance); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /playground.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davidinmichael/c-programming-tutorial/385f299811ed74b3ed32e00370842e8c63751350/playground.exe --------------------------------------------------------------------------------